port_map 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9144c963820f3ad67ddaa22b88da961e2089a5d
4
- data.tar.gz: 580bdec31873ecad6109a1c005dc510c8e6e14f6
3
+ metadata.gz: b0a71849c6e5d17a906c31dce8eb840887d6e1ff
4
+ data.tar.gz: 62feadd3c75d9d8c9dfbc07f0b80efd4f60cf47e
5
5
  SHA512:
6
- metadata.gz: b96382df0a479e97c2bbcf4a3fbd8ff082d205432dc5d6717ff1aeb9ddd83d2d5496fed1c1801c94b593c3f210ad21d7945b1bf98bf3c01b62e1decaa329de07
7
- data.tar.gz: cd928903c386ee429b1ce128201891a5368af59c37e2548ba0f1910d9a8cebfec30216957dc20eca5dd35e2a56e92baa4f9edc77257f42e485137d014db9d0b0
6
+ metadata.gz: ad87c4e5279b4481874e1e8285f2292093811a050d0927a00a76e6c5b306b9b935eae20168985b77b124dca71fc1cb026291a80ec67acc228b2c84fa530c1519
7
+ data.tar.gz: b5e971e62d173e8d4cecf3b7cf14791d30fe40a3d34a38c1f780e37bf4d33928941b981b75642d1ea7b7adcec118808f8e462de57d1cca297ea05a0cd33accb7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- port_map (0.1.0)
4
+ port_map (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -50,4 +50,4 @@ DEPENDENCIES
50
50
  rspec (~> 3.4.0)
51
51
 
52
52
  BUNDLED WITH
53
- 1.10.6
53
+ 1.11.2
data/README.md CHANGED
@@ -95,10 +95,10 @@ Installing this gem provides four executable commands:
95
95
  ### `port_map`
96
96
  - `port_map <command>`
97
97
  1. Adjusts/Adds a dynamic port number on command (assuming command takes `-p <number>` or `--port <number>`).
98
- 2. Executes `create_port_map <dynamic_port_number>`
99
- 1. Uses a `.port_map.conf` nginx server configuration (with a `$PORT` placeholder present), for generated nginx configuration.
100
- 2. Using a name specified in the `PORT_MAP_NAME` environment variable.
101
- 1. Using a name specified from the current directory.
98
+ 2. Executes `create_port_map <dynamic_port_number>`, which goes through the following in order until a name can be determined).
99
+ 1. Uses a `.port_map.conf` nginx server configuration (which has a server name and `$PORT` placeholder present), this then becomes the nginx configuration.
100
+ 2. Using a name specified in the `PORT_MAP_NAME` environment variable, this name is then used to generate a nginx configuration.
101
+ 3. Using a name specified from the current directory, this name is then used to generate a nginx configuration.
102
102
  3. Updates `/etc/hosts` with new entry `127.0.0.1 determined_name.dev #port_map`.
103
103
  4. Reloads nginx -- `sudo nginx -s reload`.
104
104
  5. Executes `<command>`.
data/bin/create_port_map CHANGED
@@ -7,6 +7,10 @@ unless (1..65535).cover?(port.to_i)
7
7
  raise 'a valid port number must be provided as the first argument'
8
8
  end
9
9
 
10
+ if PortMap::Utilities.port_taken?(port.to_i)
11
+ raise 'port number is already taken'
12
+ end
13
+
10
14
  server_conf = nil
11
15
  if File.exist?(PortMap::NginxConf::PORT_MAP_CONF_FILENAME)
12
16
  server_conf = PortMap::NginxConf.from_file(port, File.new(PortMap::NginxConf::PORT_MAP_CONF_FILENAME))
data/bin/remove_port_map CHANGED
@@ -3,6 +3,10 @@ require 'port_map'
3
3
 
4
4
  name = ARGV[0] || Dir.pwd.split('/').last
5
5
 
6
+ if File.exist?(PortMap::NginxConf::PORT_MAP_CONF_FILENAME)
7
+ name = PortMap::NginxConf.from_file('', File.new(PortMap::NginxConf::PORT_MAP_CONF_FILENAME)).name
8
+ end
9
+
6
10
  port_map = PortMap::Mappings.all.detect do |element|
7
11
  element[:name] == name
8
12
  end
@@ -21,7 +21,7 @@ module PortMap
21
21
  def self.contents
22
22
  File.readlines(HOSTS_FILENAME).reject do |line|
23
23
  line.strip.match(/#{PORT_MAP_TRACK_COMMENT}$/)
24
- end.join
24
+ end.join.strip
25
25
  end
26
26
  end
27
27
  end
@@ -7,7 +7,6 @@ module PortMap
7
7
  ZSH_CMD_STRING = "zsh -c 'source ~/.zshrc > /dev/null; PATH=%{path}; setopt aliases; eval %{cmd}'"
8
8
 
9
9
  STARTING_PORT_NUMBER = 20000
10
- PORT_NUMBER_INCREMENT = 100
11
10
 
12
11
  def self.shell_cmd_wrapper(cmd)
13
12
  case ENV.fetch('SHELL')
@@ -35,9 +34,16 @@ module PortMap
35
34
  def self.next_empty_port
36
35
  highest_port = PortMap::Mappings.all.map do |port_map|
37
36
  port_map[:locations].detect { |location| location[:name] == '/' }[:proxy_pass].split(':').last.to_i
38
- end.sort.reverse.first
37
+ end.sort.reverse.first || STARTING_PORT_NUMBER - 1
39
38
 
40
- highest_port.nil? ? STARTING_PORT_NUMBER : highest_port + PORT_NUMBER_INCREMENT
39
+ highest_port += 1
40
+ highest_port += 1 while port_taken?(highest_port)
41
+
42
+ highest_port
43
+ end
44
+
45
+ def self.port_taken?(port)
46
+ system("lsof -i:#{port}", out: '/dev/null')
41
47
  end
42
48
  end
43
49
  end
@@ -1,3 +1,3 @@
1
1
  module PortMap
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
Binary file
@@ -70,57 +70,36 @@ RSpec.describe PortMap::Utilities do
70
70
  it 'returns STARTING_PORT_NUMBER' do
71
71
  expect(described_class.next_empty_port).to eq(described_class::STARTING_PORT_NUMBER)
72
72
  end
73
- end
74
-
75
- context 'one port mapping exist' do
76
- context 'with one location' do
77
- let(:previous_highest_port_number) { 30000 }
78
- let(:port_mappings) do
79
- [
80
- {
81
- name: 'admin',
82
- nginx_conf: '/usr/local/etc/nginx/servers/admin.port_map.conf',
83
- server_name: 'admin.dev',
84
- locations: [
85
- {
86
- name: '/',
87
- proxy_pass: "http://127.0.0.1:#{previous_highest_port_number}"
88
- }
89
- ]
90
- }
91
- ]
92
- end
93
73
 
94
- it 'returns STARTING_PORT_NUMBER' do
95
- expect(described_class.next_empty_port).to eq(previous_highest_port_number + described_class::PORT_NUMBER_INCREMENT)
74
+ context 'next port taken by external process' do
75
+ it 'returns 1 port higher than the STARTING_PORT_NUMBER' do
76
+ allow(described_class).to receive(:port_taken?).and_return(true, false)
77
+ expect(described_class.next_empty_port).to eq(described_class::STARTING_PORT_NUMBER + 1)
96
78
  end
97
79
  end
80
+ end
98
81
 
99
- context 'with multiple locations' do
100
- let(:previous_highest_port_number) { 30000 }
101
- let(:port_mappings) do
102
- [
103
- {
104
- name: 'admin',
105
- nginx_conf: '/usr/local/etc/nginx/servers/admin.port_map.conf',
106
- server_name: 'admin.dev',
107
- locations: [
108
- {
109
- name: '/',
110
- proxy_pass: "http://127.0.0.1:#{previous_highest_port_number}"
111
- },
112
- {
113
- name: '/test',
114
- proxy_pass: 'http://test.dev'
115
- }
116
- ]
117
- }
118
- ]
119
- end
82
+ context 'one port mapping exist' do
83
+ let(:previous_highest_port_number) { 30000 }
84
+ let(:port_mappings) do
85
+ [
86
+ {
87
+ name: 'admin',
88
+ nginx_conf: '/usr/local/etc/nginx/servers/admin.port_map.conf',
89
+ server_name: 'admin.dev',
90
+ locations: [
91
+ {
92
+ name: '/',
93
+ proxy_pass: "http://127.0.0.1:#{previous_highest_port_number}"
94
+ }
95
+ ]
96
+ }
97
+ ]
98
+ end
120
99
 
121
- it 'returns STARTING_PORT_NUMBER' do
122
- expect(described_class.next_empty_port).to eq(previous_highest_port_number + described_class::PORT_NUMBER_INCREMENT)
123
- end
100
+ it 'returns 1 port higher than the highest' do
101
+ allow(described_class).to receive(:port_taken?).and_return(false)
102
+ expect(described_class.next_empty_port).to eq(previous_highest_port_number + 1)
124
103
  end
125
104
  end
126
105
 
@@ -135,7 +114,7 @@ RSpec.describe PortMap::Utilities do
135
114
  locations: [
136
115
  {
137
116
  name: '/',
138
- proxy_pass: 'http://127.0.0.1:20720'
117
+ proxy_pass: "http://127.0.0.1:#{previous_highest_port_number - 1}"
139
118
  }
140
119
  ]
141
120
  },
@@ -153,8 +132,9 @@ RSpec.describe PortMap::Utilities do
153
132
  ]
154
133
  end
155
134
 
156
- it 'returns STARTING_PORT_NUMBER' do
157
- expect(described_class.next_empty_port).to eq(previous_highest_port_number + described_class::PORT_NUMBER_INCREMENT)
135
+ it 'returns 1 port higher than the highest' do
136
+ allow(described_class).to receive(:port_taken?).and_return(false)
137
+ expect(described_class.next_empty_port).to eq(previous_highest_port_number + 1)
158
138
  end
159
139
  end
160
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: port_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Jalbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-08 00:00:00.000000000 Z
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler