repo-tender 0.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +257 -0
- data/bin/repo-tender +11 -0
- data/lib/repo_tender/cli/config.rb +66 -0
- data/lib/repo_tender/cli/daemon.rb +347 -0
- data/lib/repo_tender/cli/options.rb +21 -0
- data/lib/repo_tender/cli/org.rb +186 -0
- data/lib/repo_tender/cli/repo.rb +170 -0
- data/lib/repo_tender/cli/status.rb +76 -0
- data/lib/repo_tender/cli/sync.rb +149 -0
- data/lib/repo_tender/cli.rb +136 -0
- data/lib/repo_tender/config/contract.rb +53 -0
- data/lib/repo_tender/config/duration.rb +79 -0
- data/lib/repo_tender/config/model.rb +48 -0
- data/lib/repo_tender/config/store.rb +134 -0
- data/lib/repo_tender/forge/client.rb +31 -0
- data/lib/repo_tender/forge/github.rb +96 -0
- data/lib/repo_tender/launchd/agent.rb +195 -0
- data/lib/repo_tender/launchd/plist.rb +129 -0
- data/lib/repo_tender/log_rotator.rb +46 -0
- data/lib/repo_tender/paths.rb +72 -0
- data/lib/repo_tender/scm/client.rb +87 -0
- data/lib/repo_tender/scm/git.rb +232 -0
- data/lib/repo_tender/scm/status.rb +24 -0
- data/lib/repo_tender/shell.rb +90 -0
- data/lib/repo_tender/state/lock.rb +59 -0
- data/lib/repo_tender/state/store.rb +140 -0
- data/lib/repo_tender/sync/engine.rb +464 -0
- data/lib/repo_tender/sync/repo_plan.rb +215 -0
- data/lib/repo_tender/ui/interactive_reporter.rb +280 -0
- data/lib/repo_tender/ui/json_reporter.rb +39 -0
- data/lib/repo_tender/ui/mode.rb +68 -0
- data/lib/repo_tender/ui/plain_reporter.rb +53 -0
- data/lib/repo_tender/ui/reporter.rb +48 -0
- data/lib/repo_tender/version.rb +5 -0
- data/lib/repo_tender.rb +37 -0
- data/repo-tender.gemspec +47 -0
- metadata +226 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pastel"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "dry/monads"
|
|
6
|
+
require "repo_tender/cli"
|
|
7
|
+
require "repo_tender/ui/mode"
|
|
8
|
+
require "repo_tender/cli/options"
|
|
9
|
+
require "repo_tender/launchd/agent"
|
|
10
|
+
require "repo_tender/launchd/plist"
|
|
11
|
+
|
|
12
|
+
module RepoTender
|
|
13
|
+
module CLI
|
|
14
|
+
# `daemon` command group: install / uninstall / start / stop
|
|
15
|
+
# / restart / status. Installs a per-user launchd agent
|
|
16
|
+
# (`gui/<UID>`) that fires `repo-tender sync` on a
|
|
17
|
+
# `StartInterval`. The launchctl side is exercised ONLY
|
|
18
|
+
# through an injected command runner (slice-4 gates G2–G4);
|
|
19
|
+
# the live domain is proven by the manual real-Mac checklist
|
|
20
|
+
# in docs/gates/slice-4.md, not by these tests.
|
|
21
|
+
module Daemon
|
|
22
|
+
module Helpers
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
# Build a `Launchd::Agent` wired against the CLI's env
|
|
26
|
+
# seam (`CLI.env`). Tests inject a temp HOME (so
|
|
27
|
+
# `paths.launch_agents_dir` is under the temp HOME, not
|
|
28
|
+
# the real one) and a `runner:` via `Launchd::Agent.new`
|
|
29
|
+
# — the daemon commands don't accept a runner flag, so
|
|
30
|
+
# tests must use a real `Agent` (whose default `runner`
|
|
31
|
+
# only fires in an ambient Async::Task) OR stub the
|
|
32
|
+
# `Launchd::Agent` class.
|
|
33
|
+
def make_agent(uid: Process.uid, label: Launchd::Agent::DEFAULT_LABEL, runner: nil)
|
|
34
|
+
if runner
|
|
35
|
+
Launchd::Agent.new(runner: runner, uid: uid, label: label)
|
|
36
|
+
else
|
|
37
|
+
Launchd::Agent.new(uid: uid, label: label)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Resolve the on-disk plist path for the agent label,
|
|
42
|
+
# rooted at the env's HOME-resolved `LaunchAgents/`
|
|
43
|
+
# (per slice-4 gate G3).
|
|
44
|
+
def plist_path(paths, label)
|
|
45
|
+
File.join(paths.launch_agents_dir, "#{label}.plist")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Build the plist XML by looking up the absolute paths
|
|
49
|
+
# the launchd runtime needs (mise, ruby, the bin
|
|
50
|
+
# script, the repo root, the mise.toml). These come
|
|
51
|
+
# from a few places:
|
|
52
|
+
# * `mise_bin` — `which mise` (we shell out at
|
|
53
|
+
# install-time; the result is baked into the plist
|
|
54
|
+
# as an absolute path so launchd's empty PATH
|
|
55
|
+
# doesn't matter).
|
|
56
|
+
# * `ruby_bin` — `mise exec -- which ruby` (the
|
|
57
|
+
# toolchain-resolved ruby; pinned via mise.toml).
|
|
58
|
+
# * `bin_path` — `RbConfig.ruby` + the script path
|
|
59
|
+
# (we use `__dir__` of this file's caller; for the
|
|
60
|
+
# gem install, this is `<gem>/bin/repo-tender`).
|
|
61
|
+
#
|
|
62
|
+
# In tests, we inject these via the `Resolve` object
|
|
63
|
+
# (see below) — never call out to the shell.
|
|
64
|
+
def build_plist(resolve:, config:, paths:, label:)
|
|
65
|
+
Launchd::Plist.call(
|
|
66
|
+
label: label,
|
|
67
|
+
refresh_interval: config.refresh_interval,
|
|
68
|
+
log_dir: paths.log_dir,
|
|
69
|
+
repo_root: resolve.repo_root,
|
|
70
|
+
mise_toml: resolve.mise_toml,
|
|
71
|
+
mise_bin: resolve.mise_bin,
|
|
72
|
+
ruby_bin: resolve.ruby_bin,
|
|
73
|
+
bin_path: resolve.bin_path
|
|
74
|
+
)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def format_failure(f) = f.is_a?(Hash) ? f.inspect : f.to_s
|
|
78
|
+
|
|
79
|
+
def fail_with(cmd, msg)
|
|
80
|
+
cmd.send(:err).puts msg
|
|
81
|
+
RepoTender::CLI.record_outcome(Outcome.new(exit_code: 1, message: msg))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Bundle of resolved absolute paths the plist needs.
|
|
85
|
+
# Production code resolves these via the shell (see
|
|
86
|
+
# `Resolve.detect`); tests construct one directly with
|
|
87
|
+
# known absolute paths.
|
|
88
|
+
Resolve = Data.define(:repo_root, :mise_toml, :mise_bin, :ruby_bin, :bin_path) do
|
|
89
|
+
def initialize(repo_root:, mise_toml:, mise_bin:, ruby_bin:, bin_path:)
|
|
90
|
+
super
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
class Install < Dry::CLI::Command
|
|
96
|
+
include Helpers
|
|
97
|
+
include GlobalOptions
|
|
98
|
+
|
|
99
|
+
desc "Install the per-user launchd agent (writes the plist + bootstrap)"
|
|
100
|
+
|
|
101
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
102
|
+
paths = CLI.make_paths
|
|
103
|
+
paths.ensure!
|
|
104
|
+
config = Config::Store.load(paths.config_file).success
|
|
105
|
+
|
|
106
|
+
mode = UI::Mode.resolve(
|
|
107
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
108
|
+
env: CLI.env,
|
|
109
|
+
out: out
|
|
110
|
+
)
|
|
111
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
112
|
+
|
|
113
|
+
label = Launchd::Agent::DEFAULT_LABEL
|
|
114
|
+
pp = plist_path(paths, label)
|
|
115
|
+
|
|
116
|
+
resolve = Resolve.detect(repo_root: Dir.pwd)
|
|
117
|
+
xml = build_plist(resolve: resolve, config: config, paths: paths, label: label)
|
|
118
|
+
FileUtils.mkdir_p(File.dirname(pp))
|
|
119
|
+
File.write(pp, xml)
|
|
120
|
+
|
|
121
|
+
agent = make_agent
|
|
122
|
+
result = agent.install(pp)
|
|
123
|
+
if result.failure?
|
|
124
|
+
return fail_with(self, "bootstrap failed: #{format_failure(result.failure)}")
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
out.puts pastel.green("installed: #{pp}")
|
|
128
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
class Uninstall < Dry::CLI::Command
|
|
133
|
+
include Helpers
|
|
134
|
+
include GlobalOptions
|
|
135
|
+
|
|
136
|
+
desc "Uninstall the per-user launchd agent (bootout + remove the plist)"
|
|
137
|
+
|
|
138
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
139
|
+
mode = UI::Mode.resolve(
|
|
140
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
141
|
+
env: CLI.env,
|
|
142
|
+
out: out
|
|
143
|
+
)
|
|
144
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
145
|
+
|
|
146
|
+
paths = CLI.make_paths
|
|
147
|
+
label = Launchd::Agent::DEFAULT_LABEL
|
|
148
|
+
pp = plist_path(paths, label)
|
|
149
|
+
|
|
150
|
+
agent = make_agent
|
|
151
|
+
result = agent.uninstall
|
|
152
|
+
# CF5 (Slice 5): the Agent maps a not-loaded bootout
|
|
153
|
+
# (status 3 / "No such process") to Success — the
|
|
154
|
+
# common case at a 6h refresh interval. A non-benign
|
|
155
|
+
# bootout Failure (e.g. status 1 "Operation not
|
|
156
|
+
# permitted") still surfaces here as a real failure
|
|
157
|
+
# the operator needs to see. We still remove the
|
|
158
|
+
# plist regardless (idempotent uninstall, Slice-4
|
|
159
|
+
# gate G3): the bootout is the only best-effort step.
|
|
160
|
+
if result.failure?
|
|
161
|
+
err.puts "bootout reported: #{format_failure(result.failure)}"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
if File.exist?(pp)
|
|
165
|
+
File.delete(pp)
|
|
166
|
+
out.puts pastel.green("removed plist: #{pp}")
|
|
167
|
+
else
|
|
168
|
+
out.puts pastel.yellow("plist not present: #{pp}")
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
class Start < Dry::CLI::Command
|
|
176
|
+
include Helpers
|
|
177
|
+
include GlobalOptions
|
|
178
|
+
|
|
179
|
+
desc "Start the agent (bootstrap + enable)"
|
|
180
|
+
|
|
181
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
182
|
+
mode = UI::Mode.resolve(
|
|
183
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
184
|
+
env: CLI.env,
|
|
185
|
+
out: out
|
|
186
|
+
)
|
|
187
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
188
|
+
|
|
189
|
+
paths = CLI.make_paths
|
|
190
|
+
label = Launchd::Agent::DEFAULT_LABEL
|
|
191
|
+
pp = plist_path(paths, label)
|
|
192
|
+
|
|
193
|
+
agent = make_agent
|
|
194
|
+
result = agent.start(pp)
|
|
195
|
+
if result.failure?
|
|
196
|
+
return fail_with(self, "start failed: #{format_failure(result.failure)}")
|
|
197
|
+
end
|
|
198
|
+
out.puts pastel.green("started: #{label}")
|
|
199
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
class Stop < Dry::CLI::Command
|
|
204
|
+
include Helpers
|
|
205
|
+
include GlobalOptions
|
|
206
|
+
|
|
207
|
+
desc "Stop the agent (bootout + disable)"
|
|
208
|
+
|
|
209
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
210
|
+
mode = UI::Mode.resolve(
|
|
211
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
212
|
+
env: CLI.env,
|
|
213
|
+
out: out
|
|
214
|
+
)
|
|
215
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
216
|
+
|
|
217
|
+
label = Launchd::Agent::DEFAULT_LABEL
|
|
218
|
+
agent = make_agent
|
|
219
|
+
result = agent.stop
|
|
220
|
+
if result.failure?
|
|
221
|
+
return fail_with(self, "stop failed: #{format_failure(result.failure)}")
|
|
222
|
+
end
|
|
223
|
+
out.puts pastel.green("stopped: #{label}")
|
|
224
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
class Restart < Dry::CLI::Command
|
|
229
|
+
include Helpers
|
|
230
|
+
include GlobalOptions
|
|
231
|
+
|
|
232
|
+
desc "Restart the agent (kickstart -k)"
|
|
233
|
+
|
|
234
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
235
|
+
mode = UI::Mode.resolve(
|
|
236
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
237
|
+
env: CLI.env,
|
|
238
|
+
out: out
|
|
239
|
+
)
|
|
240
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
241
|
+
|
|
242
|
+
label = Launchd::Agent::DEFAULT_LABEL
|
|
243
|
+
agent = make_agent
|
|
244
|
+
result = agent.restart
|
|
245
|
+
if result.failure?
|
|
246
|
+
return fail_with(self, "restart failed: #{format_failure(result.failure)}")
|
|
247
|
+
end
|
|
248
|
+
out.puts pastel.green("restarted: #{label}")
|
|
249
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
class Status < Dry::CLI::Command
|
|
254
|
+
include Helpers
|
|
255
|
+
include GlobalOptions
|
|
256
|
+
|
|
257
|
+
desc "Print the agent's loaded/running/last-exit state"
|
|
258
|
+
|
|
259
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
260
|
+
mode = UI::Mode.resolve(
|
|
261
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
262
|
+
env: CLI.env,
|
|
263
|
+
out: out
|
|
264
|
+
)
|
|
265
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
266
|
+
|
|
267
|
+
label = Launchd::Agent::DEFAULT_LABEL
|
|
268
|
+
agent = make_agent
|
|
269
|
+
result = agent.status
|
|
270
|
+
if result.failure?
|
|
271
|
+
return fail_with(self, "status failed: #{format_failure(result.failure)}")
|
|
272
|
+
end
|
|
273
|
+
s = result.success
|
|
274
|
+
out.puts pastel.cyan("label: #{label}")
|
|
275
|
+
out.puts pastel.cyan("loaded: #{s[:loaded]}")
|
|
276
|
+
out.puts pastel.cyan("running: #{s[:running]}")
|
|
277
|
+
out.puts pastel.cyan("pid: #{s[:pid].inspect}")
|
|
278
|
+
out.puts pastel.cyan("last_exit: #{s[:last_exit].inspect}")
|
|
279
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Detect the runtime paths the plist needs. The repo-tender
|
|
287
|
+
# install path matters because the plist stores an absolute
|
|
288
|
+
# `bin_path` — that is the script launchd invokes. We resolve
|
|
289
|
+
# `bin_path` from the on-disk gem layout if we can, else fall
|
|
290
|
+
# back to the directory the daemon command was run from.
|
|
291
|
+
class RepoTender::CLI::Daemon::Helpers::Resolve
|
|
292
|
+
# @param repo_root [String] absolute path of the working directory (where mise.toml is expected)
|
|
293
|
+
# @return [Resolve]
|
|
294
|
+
def self.detect(repo_root:)
|
|
295
|
+
mise_bin = detect_mise_bin
|
|
296
|
+
ruby_bin = detect_ruby_bin(repo_root, mise_bin)
|
|
297
|
+
bin_path = detect_bin_path(repo_root)
|
|
298
|
+
mise_toml = File.join(repo_root, "mise.toml")
|
|
299
|
+
new(repo_root: repo_root, mise_toml: mise_toml, mise_bin: mise_bin, ruby_bin: ruby_bin, bin_path: bin_path)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def self.detect_mise_bin
|
|
303
|
+
path = ENV["REPO_TENDER_MISE_BIN"]
|
|
304
|
+
return path if path && !path.empty?
|
|
305
|
+
require "open3"
|
|
306
|
+
out, _e, st = Open3.capture3("which", "mise")
|
|
307
|
+
st.success? ? out.strip : "/opt/homebrew/bin/mise"
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def self.detect_ruby_bin(repo_root, mise_bin)
|
|
311
|
+
path = ENV["REPO_TENDER_RUBY_BIN"]
|
|
312
|
+
return path if path && !path.empty?
|
|
313
|
+
# `mise exec -- which ruby` — but we avoid spawning in tests;
|
|
314
|
+
# production path goes through here.
|
|
315
|
+
require "open3"
|
|
316
|
+
out, _e, st = Open3.capture3(mise_bin, "exec", "--", "which", "ruby", chdir: repo_root)
|
|
317
|
+
return out.strip if st.success? && !out.strip.empty?
|
|
318
|
+
# Fall back to the system ruby (last resort).
|
|
319
|
+
out, _e, st = Open3.capture3("which", "ruby")
|
|
320
|
+
st.success? ? out.strip : "/usr/bin/ruby"
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def self.detect_bin_path(repo_root)
|
|
324
|
+
path = ENV["REPO_TENDER_BIN_PATH"]
|
|
325
|
+
return path if path && !path.empty?
|
|
326
|
+
# Prefer the on-disk dev bin at `<repo_root>/bin/repo-tender`
|
|
327
|
+
# — it's what the human runs during testing, and the gem is
|
|
328
|
+
# typically not `gem install`ed in a source checkout.
|
|
329
|
+
dev = File.join(repo_root, "bin", "repo-tender")
|
|
330
|
+
return dev if File.exist?(dev)
|
|
331
|
+
# Next, an installed binary on PATH.
|
|
332
|
+
require "open3"
|
|
333
|
+
out, _e, st = Open3.capture3("which", "repo-tender")
|
|
334
|
+
return out.strip if st.success? && !out.strip.empty?
|
|
335
|
+
# Last resort: the installed gem's bin (raises if not installed).
|
|
336
|
+
Gem.bin_path("repo-tender", "repo-tender")
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
RepoTender::CLI::Registry.register "daemon" do |prefix|
|
|
341
|
+
prefix.register "install", RepoTender::CLI::Daemon::Install
|
|
342
|
+
prefix.register "uninstall", RepoTender::CLI::Daemon::Uninstall
|
|
343
|
+
prefix.register "start", RepoTender::CLI::Daemon::Start
|
|
344
|
+
prefix.register "stop", RepoTender::CLI::Daemon::Stop
|
|
345
|
+
prefix.register "restart", RepoTender::CLI::Daemon::Restart
|
|
346
|
+
prefix.register "status", RepoTender::CLI::Daemon::Status
|
|
347
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RepoTender
|
|
4
|
+
module CLI
|
|
5
|
+
# Shared output mode flags. Include in any command class to add:
|
|
6
|
+
# --plain force plain text output (one line per event, ANSI-free)
|
|
7
|
+
# --json force JSON output (one object per event line, 12-factor)
|
|
8
|
+
# --no-color disable ANSI color
|
|
9
|
+
# --quiet/-q suppress non-essential human output
|
|
10
|
+
#
|
|
11
|
+
# Resolved via UI::Mode.resolve in the command's #call.
|
|
12
|
+
module GlobalOptions
|
|
13
|
+
def self.included(base)
|
|
14
|
+
base.option :plain, type: :flag, desc: "Plain text output (one line per event, no color)"
|
|
15
|
+
base.option :json, type: :flag, desc: "JSON output (one object per event line, 12-factor)"
|
|
16
|
+
base.option :no_color, type: :flag, desc: "Disable color output"
|
|
17
|
+
base.option :quiet, type: :flag, aliases: ["-q"], desc: "Suppress non-essential output"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pastel"
|
|
4
|
+
require "dry/monads"
|
|
5
|
+
require "repo_tender/cli"
|
|
6
|
+
require "repo_tender/ui/mode"
|
|
7
|
+
require "repo_tender/cli/options"
|
|
8
|
+
|
|
9
|
+
module RepoTender
|
|
10
|
+
module CLI
|
|
11
|
+
# `org` command group: add / remove / list tracked orgs.
|
|
12
|
+
# Same shape as `repo` but against `Config::OrgRef` (host + name
|
|
13
|
+
# + two bool flags). The host defaults to "github.com" per
|
|
14
|
+
# PRD §3.1 when omitted (so `org add socketry` works as well as
|
|
15
|
+
# `org add github.com/socketry`).
|
|
16
|
+
module Org
|
|
17
|
+
module Helpers
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
# Parse an org ref string. Accepts "github.com/socketry" or
|
|
21
|
+
# bare "socketry" (defaults host to github.com). Anything
|
|
22
|
+
# with 3+ parts is rejected (org has only host + name).
|
|
23
|
+
def parse_ref(name, include_archived: false, include_forks: false)
|
|
24
|
+
parts = name.to_s.split("/")
|
|
25
|
+
case parts.length
|
|
26
|
+
when 1
|
|
27
|
+
org = parts[0]
|
|
28
|
+
return Dry::Monads::Failure("invalid org reference: empty name in #{name.inspect}") if org.empty?
|
|
29
|
+
Dry::Monads::Success(Config::OrgRef.new(
|
|
30
|
+
host: Config::DEFAULT_HOST,
|
|
31
|
+
name: org,
|
|
32
|
+
include_archived: include_archived,
|
|
33
|
+
include_forks: include_forks
|
|
34
|
+
))
|
|
35
|
+
when 2
|
|
36
|
+
host, n = parts
|
|
37
|
+
return Dry::Monads::Failure("invalid org reference: empty host in #{name.inspect}") if host.empty?
|
|
38
|
+
return Dry::Monads::Failure("invalid org reference: empty name in #{name.inspect}") if n.empty?
|
|
39
|
+
Dry::Monads::Success(Config::OrgRef.new(
|
|
40
|
+
host: host,
|
|
41
|
+
name: n,
|
|
42
|
+
include_archived: include_archived,
|
|
43
|
+
include_forks: include_forks
|
|
44
|
+
))
|
|
45
|
+
else
|
|
46
|
+
Dry::Monads::Failure("invalid org reference: #{name.inspect} (expected \"<name>\" or \"<host>/<name>\")")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def same_org?(a, b) = a.host == b.host && a.name == b.name
|
|
51
|
+
def format_ref(o) = "#{o.host}/#{o.name}"
|
|
52
|
+
def format_failure(f) = f.is_a?(Hash) ? f.inspect : f.to_s
|
|
53
|
+
|
|
54
|
+
def fail_with(cmd, msg)
|
|
55
|
+
cmd.send(:err).puts msg
|
|
56
|
+
RepoTender::CLI.record_outcome(Outcome.new(exit_code: 1, message: msg))
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class Add < Dry::CLI::Command
|
|
61
|
+
include Helpers
|
|
62
|
+
include GlobalOptions
|
|
63
|
+
|
|
64
|
+
desc "Add a tracked org (idempotent on host/name)"
|
|
65
|
+
argument :name, required: true,
|
|
66
|
+
desc: "Org identity as <name> or <host>/<name> (host defaults to github.com)"
|
|
67
|
+
option :include_archived, type: :boolean, default: false,
|
|
68
|
+
desc: "Include archived repos when expanding the org"
|
|
69
|
+
option :include_forks, type: :boolean, default: false,
|
|
70
|
+
desc: "Include forks when expanding the org"
|
|
71
|
+
|
|
72
|
+
def call(name:, include_archived: false, include_forks: false, plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
73
|
+
mode = UI::Mode.resolve(
|
|
74
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
75
|
+
env: CLI.env,
|
|
76
|
+
out: out
|
|
77
|
+
)
|
|
78
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
79
|
+
|
|
80
|
+
parsed = parse_ref(name,
|
|
81
|
+
include_archived: include_archived,
|
|
82
|
+
include_forks: include_forks)
|
|
83
|
+
return fail_with(self, parsed.failure) if parsed.failure?
|
|
84
|
+
|
|
85
|
+
new_ref = parsed.success
|
|
86
|
+
paths = CLI.make_paths
|
|
87
|
+
config = Config::Store.load(paths.config_file).success
|
|
88
|
+
|
|
89
|
+
if config.orgs.any? { |o| same_org?(o, new_ref) }
|
|
90
|
+
out.puts pastel.yellow("already tracked: #{format_ref(new_ref)}" \
|
|
91
|
+
" (include_archived=#{new_ref.include_archived}, include_forks=#{new_ref.include_forks})")
|
|
92
|
+
return CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
result = Config::Store.update(paths.config_file) do |c|
|
|
96
|
+
Config::Store.with(c, orgs: c.orgs + [new_ref])
|
|
97
|
+
end
|
|
98
|
+
if result.failure?
|
|
99
|
+
return fail_with(self, "failed to update config: #{format_failure(result.failure)}")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
out.puts pastel.green("added: #{format_ref(new_ref)}" \
|
|
103
|
+
" (include_archived=#{new_ref.include_archived}, include_forks=#{new_ref.include_forks})")
|
|
104
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
class Remove < Dry::CLI::Command
|
|
109
|
+
include Helpers
|
|
110
|
+
include GlobalOptions
|
|
111
|
+
|
|
112
|
+
desc "Remove a tracked org (host/name)"
|
|
113
|
+
argument :name, required: true,
|
|
114
|
+
desc: "Org identity as <name> or <host>/<name> (host defaults to github.com)"
|
|
115
|
+
|
|
116
|
+
def call(name:, plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
117
|
+
# The flags don't affect the identity match for remove; we
|
|
118
|
+
# match on (host, name) only. This matches the user's
|
|
119
|
+
# expectation that "remove" targets the org, not the flag
|
|
120
|
+
# combination they added it with.
|
|
121
|
+
mode = UI::Mode.resolve(
|
|
122
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
123
|
+
env: CLI.env,
|
|
124
|
+
out: out
|
|
125
|
+
)
|
|
126
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
127
|
+
|
|
128
|
+
parsed = parse_ref(name)
|
|
129
|
+
return fail_with(self, parsed.failure) if parsed.failure?
|
|
130
|
+
|
|
131
|
+
target = parsed.success
|
|
132
|
+
paths = CLI.make_paths
|
|
133
|
+
config = Config::Store.load(paths.config_file).success
|
|
134
|
+
|
|
135
|
+
kept = config.orgs.reject { |o| same_org?(o, target) }
|
|
136
|
+
if kept.size == config.orgs.size
|
|
137
|
+
return fail_with(self, "not tracked: #{format_ref(target)}")
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
result = Config::Store.update(paths.config_file) do |c|
|
|
141
|
+
Config::Store.with(c, orgs: kept)
|
|
142
|
+
end
|
|
143
|
+
if result.failure?
|
|
144
|
+
return fail_with(self, "failed to update config: #{format_failure(result.failure)}")
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
out.puts pastel.green("removed: #{format_ref(target)}")
|
|
148
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
class List < Dry::CLI::Command
|
|
153
|
+
include GlobalOptions
|
|
154
|
+
|
|
155
|
+
desc "List tracked orgs"
|
|
156
|
+
|
|
157
|
+
def call(plain: nil, json: nil, no_color: nil, quiet: nil, **)
|
|
158
|
+
mode = UI::Mode.resolve(
|
|
159
|
+
flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
|
|
160
|
+
env: CLI.env,
|
|
161
|
+
out: out
|
|
162
|
+
)
|
|
163
|
+
pastel = Pastel.new(enabled: mode.color)
|
|
164
|
+
|
|
165
|
+
paths = CLI.make_paths
|
|
166
|
+
config = Config::Store.load(paths.config_file).success
|
|
167
|
+
if config.orgs.empty?
|
|
168
|
+
out.puts pastel.dim("(no tracked orgs)")
|
|
169
|
+
else
|
|
170
|
+
config.orgs.each do |o|
|
|
171
|
+
out.puts pastel.cyan("#{o.host}/#{o.name}" \
|
|
172
|
+
" (include_archived=#{o.include_archived}, include_forks=#{o.include_forks})")
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
CLI.record_outcome(Outcome.new(exit_code: 0))
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
RepoTender::CLI::Registry.register "org" do |prefix|
|
|
183
|
+
prefix.register "add", RepoTender::CLI::Org::Add
|
|
184
|
+
prefix.register "remove", RepoTender::CLI::Org::Remove
|
|
185
|
+
prefix.register "list", RepoTender::CLI::Org::List
|
|
186
|
+
end
|