Module: PortMap::Utilities

Defined in:
lib/port_map/utilities.rb

Constant Summary collapse

BASH_SHELL =
'/bin/bash'.freeze
ZSH_SHELL =
'/bin/zsh'.freeze
BASH_CMD_STRING =
"bash -c 'source ~/.bashrc > /dev/null; PATH=%{path}; shopt -s expand_aliases; %{cmd}"
ZSH_CMD_STRING =
"zsh -c 'source ~/.zshrc > /dev/null; PATH=%{path}; setopt aliases; eval %{cmd}'"
STARTING_PORT_NUMBER =
20000

Class Method Summary collapse

Class Method Details

.apply_open_port_option(port, cmd) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/port_map/utilities.rb', line 22

def self.apply_open_port_option(port, cmd)
  port_option_flag = '--port'

  if cmd.sub!(/-p\s+(\d+)/, '')
    port_option_flag = '-p'
  elsif cmd.sub!(/--port\s+(\d+)/, '')
    port_option_flag = '--port'
  end

  [cmd.strip, port_option_flag, port].join(' ')
end

.next_empty_portObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/port_map/utilities.rb', line 34

def self.next_empty_port
  highest_port = PortMap::Mappings.all.map do |port_map|
    port_map[:locations].detect { |location| location[:name] == '/' }[:proxy_pass].split(':').last.to_i
  end.sort.reverse.first || STARTING_PORT_NUMBER - 1

  highest_port += 1
  highest_port += 1 while port_taken?(highest_port)

  highest_port
end

.port_taken?(port) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/port_map/utilities.rb', line 45

def self.port_taken?(port)
  system("lsof -i:#{port}", out: '/dev/null')
end

.shell_cmd_wrapper(cmd) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/port_map/utilities.rb', line 11

def self.shell_cmd_wrapper(cmd)
  case ENV.fetch('SHELL')
  when ZSH_SHELL
    ZSH_CMD_STRING % { path: ENV.fetch('PATH'), cmd: cmd }
  when BASH_SHELL
    BASH_CMD_STRING % { path: ENV.fetch('PATH'), cmd: cmd }
  else
    cmd
  end
end