locport 1.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/bin/locport +5 -0
  4. data/lib/locport.rb +190 -0
  5. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 60fe13278bf753c5ca419b16ed83d731d1234c8418cde3fad2219e072c58c933
4
+ data.tar.gz: 195ea9c9149cf05f67f67648ebccf8bad93665e18984bbffb6af97efcd804507
5
+ SHA512:
6
+ metadata.gz: '0246848221d73ee34faec98b8da66622ef74364dc7f96c9831b6f0c763f6b8ca4b19c056c65158a00c41763defac07838e3bbceb80c608b3724625ad39e1b567'
7
+ data.tar.gz: 9b4e16a6b15447a1ab4d1aaeee8170e0f9381c3819008f45ac2c17fa8ed1636d60dd6e87ea4c2a228138864d00afd242d1ef071765b3c53b1e4ca3cd54efe46a
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Robert Starsi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/bin/locport ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "locport"
4
+
5
+ Locport::Main.start(ARGV)
data/lib/locport.rb ADDED
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "fileutils"
5
+ require "pathname"
6
+
7
+ APP_NAME = "locport"
8
+ DATA_FILE = "projects"
9
+ DOTFILE = ".localhost"
10
+ PORT_RANGE = (30_000..60_000)
11
+
12
+ module Locport
13
+ class Main < Thor
14
+ default_task :list
15
+
16
+ def self.exit_on_failure?
17
+ true
18
+ end
19
+
20
+ desc "index [PATH]", "Index a project"
21
+ def index(path = Dir.pwd, silent: false)
22
+ project_path = Pathname.new(path).cleanpath
23
+ dotfile_path = project_path.join(DOTFILE)
24
+
25
+ unless File.exist?(dotfile_path)
26
+ say_error "#{dotfile_path} file doesn't exist", :red
27
+ say_error "You can create it from within that directory with `locport add`"
28
+ exit 1
29
+ end
30
+
31
+ append_to_projects project_path.to_s
32
+
33
+ unless silent
34
+ say "Indexing ", :green
35
+ say project_path
36
+ end
37
+ end
38
+
39
+ desc "add [HOST[:PORT]]", "Add a new host to .localhost file. \
40
+ Without arguments current directory name will be used and random port number assigned."
41
+ def add(host = "#{File.basename(Dir.pwd)}.localhost")
42
+ host_with_port, port = ensure_port(host.strip)
43
+
44
+ # if taken_by_path = port_taken?(port)
45
+ # say_error "Port #{port} is already taken by #{taken_by_path}"
46
+ # exit 1
47
+ # end
48
+
49
+ append_to_dotfile host_with_port
50
+ index silent: true
51
+
52
+ say "#{host_with_port} ", :bold
53
+ say "added ", :green
54
+ say "to #{DOTFILE}"
55
+ rescue HostAlreadyAddedError => e
56
+ say_error e.message, :red
57
+ exit 1
58
+ end
59
+
60
+ desc "list", "List indexed projects, hosts and ports"
61
+ def list
62
+ table_data = []
63
+ used_ports = []
64
+ used_hosts = []
65
+ conflicts_found = false
66
+
67
+ projects.each do |(dir, host, port)|
68
+ table_data << [ dir.sub(Dir.home, "~"), "http://#{host}:#{port}" ]
69
+
70
+ if used_ports.include?(port)
71
+ conflicts_found = true
72
+ table_data << [ "", "╰ Port used before" ]
73
+ else
74
+ used_ports << port
75
+ end
76
+
77
+ if used_hosts.include?(host)
78
+ conflicts_found = true
79
+ table_data << [ "", "╰ Host used before" ]
80
+ else
81
+ used_hosts << host
82
+ end
83
+ end
84
+
85
+ print_table [ [ "Project", "URL" ] ] + table_data, borders: true
86
+
87
+ if conflicts_found
88
+ say "Conflicts found!", :red
89
+ exit 1
90
+ else
91
+ say "All hosts and ports are unique ✓", :green
92
+ end
93
+ end
94
+
95
+ desc "info", "Display tool information"
96
+ def info
97
+ say "Projects index: #{projects_file_path}"
98
+ end
99
+
100
+ class HostAlreadyAddedError < StandardError; end
101
+
102
+ private
103
+ def projects
104
+ @projects ||= load_projects
105
+ end
106
+
107
+ def load_projects
108
+ result = []
109
+ @used_ports = []
110
+ @used_hosts = []
111
+
112
+ File.read(projects_file_path).lines.each do |project_path|
113
+ project_path = project_path.strip
114
+ dotfile_path = Pathname.new(project_path).join(DOTFILE)
115
+ next unless File.exist?(dotfile_path)
116
+
117
+ hosts = File.read(dotfile_path).lines
118
+
119
+ hosts.each do |host_with_port|
120
+ host, port = host_with_port.strip.split(":")
121
+ result << [ project_path, host, port ]
122
+
123
+ unless @used_ports.include?(port)
124
+ @used_ports << port
125
+ end
126
+
127
+ unless @used_hosts.include?(host)
128
+ @used_hosts << host
129
+ end
130
+ end
131
+ end
132
+
133
+ result
134
+ end
135
+
136
+ def ensure_port(host)
137
+ if host =~ /:([\d]+)$/
138
+ [ host, $1.to_i ]
139
+ else
140
+ port = rand PORT_RANGE
141
+ [ "#{host}:#{port}", port ]
142
+ end
143
+ end
144
+
145
+ def storage_base_dir
146
+ if Gem.win_platform?
147
+ ENV["APPDATA"] || File.join(Dir.home, "AppData", "Roaming")
148
+ else
149
+ ENV["XDG_DATA_HOME"] || File.join(Dir.home, ".local", "share")
150
+ end
151
+ end
152
+
153
+ def storage_dir
154
+ File.join(storage_base_dir, APP_NAME)
155
+ end
156
+
157
+ def projects_file_path
158
+ FileUtils.mkdir_p(storage_dir)
159
+ File.join(storage_dir, DATA_FILE)
160
+ end
161
+
162
+ def append_to_projects(line)
163
+ path_present = File.exist?(projects_file_path) && File.read(projects_file_path).lines.any? do
164
+ it.strip == line
165
+ end
166
+
167
+ return if path_present
168
+
169
+ File.open(projects_file_path, "a") do |f|
170
+ f.puts(line)
171
+ end
172
+ end
173
+
174
+ 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
+ File.open(DOTFILE, "a") do |f|
186
+ f.puts(line)
187
+ end
188
+ end
189
+ end
190
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locport
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Starsi
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: thor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.3'
26
+ description: Overview of localhost ports used across projects. Prevent conflicts.
27
+ email: klevo@klevo.sk
28
+ executables:
29
+ - locport
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - bin/locport
35
+ - lib/locport.rb
36
+ homepage: https://github.com/klevo/locport
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.7.1
55
+ specification_version: 4
56
+ summary: localhost port management
57
+ test_files: []