locport 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/locport.rb +38 -24
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 738deef26583d7f21e5ecf9a62ac7a364b648c13fa3d1d86db9bbbad322d3e43
|
|
4
|
+
data.tar.gz: ea1d8b3019ba4715ba1d41e79d2e0267b5912a09346bd72de4cb8ee7027a70eb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4a7e5a9f63a9d2722e060d1065d414168739a483444c4af7cdd0c1d1362edd3ce15c9759760c07140630e3d523c31d1644b2b2a257d4f2feb34272de1d389273
|
|
7
|
+
data.tar.gz: ec1eed26d60f600ed3b7e04fe31551d6453306fc145e0f677b9d2ccfc67757177ab1e00ba43ac0e4503712d9393ec23a483293ec9e8d6a4dcffe8d3d95fb4106
|
data/lib/locport.rb
CHANGED
|
@@ -40,11 +40,21 @@ module Locport
|
|
|
40
40
|
Without arguments current directory name will be used and random port number assigned."
|
|
41
41
|
def add(host = "#{File.basename(Dir.pwd)}.localhost")
|
|
42
42
|
host_with_port, port = ensure_port(host.strip)
|
|
43
|
+
host = host_with_port.split(":").first
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if used_ports.include?(port)
|
|
46
|
+
say_error "Port #{port} is "
|
|
47
|
+
say_error "already used. ", :red
|
|
48
|
+
say_error "See `locport list`"
|
|
49
|
+
exit 1
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if used_hosts.include?(host)
|
|
53
|
+
say_error "Host '#{host}' is "
|
|
54
|
+
say_error "already used. ", :red
|
|
55
|
+
say_error "See `locport list`"
|
|
56
|
+
exit 1
|
|
57
|
+
end
|
|
48
58
|
|
|
49
59
|
append_to_dotfile host_with_port
|
|
50
60
|
index silent: true
|
|
@@ -52,9 +62,6 @@ module Locport
|
|
|
52
62
|
say "#{host_with_port} ", :bold
|
|
53
63
|
say "added ", :green
|
|
54
64
|
say "to #{DOTFILE}"
|
|
55
|
-
rescue HostAlreadyAddedError => e
|
|
56
|
-
say_error e.message, :red
|
|
57
|
-
exit 1
|
|
58
65
|
end
|
|
59
66
|
|
|
60
67
|
desc "list", "List indexed projects, hosts and ports"
|
|
@@ -97,15 +104,24 @@ module Locport
|
|
|
97
104
|
say "Projects index: #{projects_file_path}"
|
|
98
105
|
end
|
|
99
106
|
|
|
100
|
-
class HostAlreadyAddedError < StandardError; end
|
|
101
|
-
|
|
102
107
|
private
|
|
103
108
|
def projects
|
|
104
|
-
|
|
109
|
+
load_projects
|
|
110
|
+
@projects
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def used_ports
|
|
114
|
+
load_projects
|
|
115
|
+
@used_ports
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def used_hosts
|
|
119
|
+
load_projects
|
|
120
|
+
@used_hosts
|
|
105
121
|
end
|
|
106
122
|
|
|
107
123
|
def load_projects
|
|
108
|
-
|
|
124
|
+
@projects = []
|
|
109
125
|
@used_ports = []
|
|
110
126
|
@used_hosts = []
|
|
111
127
|
|
|
@@ -118,7 +134,8 @@ module Locport
|
|
|
118
134
|
|
|
119
135
|
hosts.each do |host_with_port|
|
|
120
136
|
host, port = host_with_port.strip.split(":")
|
|
121
|
-
|
|
137
|
+
port = port.to_i
|
|
138
|
+
@projects << [ project_path, host, port ]
|
|
122
139
|
|
|
123
140
|
unless @used_ports.include?(port)
|
|
124
141
|
@used_ports << port
|
|
@@ -130,18 +147,25 @@ module Locport
|
|
|
130
147
|
end
|
|
131
148
|
end
|
|
132
149
|
|
|
133
|
-
|
|
150
|
+
@projects
|
|
134
151
|
end
|
|
135
152
|
|
|
136
153
|
def ensure_port(host)
|
|
137
154
|
if host =~ /:([\d]+)$/
|
|
138
155
|
[ host, $1.to_i ]
|
|
139
156
|
else
|
|
140
|
-
port =
|
|
157
|
+
port = find_unused_port
|
|
141
158
|
[ "#{host}:#{port}", port ]
|
|
142
159
|
end
|
|
143
160
|
end
|
|
144
161
|
|
|
162
|
+
def find_unused_port
|
|
163
|
+
loop do
|
|
164
|
+
port = rand PORT_RANGE
|
|
165
|
+
return port unless used_ports.include?(port)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
145
169
|
def storage_base_dir
|
|
146
170
|
if Gem.win_platform?
|
|
147
171
|
ENV["APPDATA"] || File.join(Dir.home, "AppData", "Roaming")
|
|
@@ -172,16 +196,6 @@ module Locport
|
|
|
172
196
|
end
|
|
173
197
|
|
|
174
198
|
def append_to_dotfile(line)
|
|
175
|
-
host = line.split(":").first
|
|
176
|
-
|
|
177
|
-
host_present = File.exist?(DOTFILE) && File.read(DOTFILE).lines.any? do
|
|
178
|
-
it.split(":").first == host
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
if host_present
|
|
182
|
-
raise HostAlreadyAddedError, "#{host} is already present in #{DOTFILE}"
|
|
183
|
-
end
|
|
184
|
-
|
|
185
199
|
File.open(DOTFILE, "a") do |f|
|
|
186
200
|
f.puts(line)
|
|
187
201
|
end
|