Class: Carson::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/carson/cli.rb

Class Method Summary collapse

Class Method Details

.build_parserObject



45
46
47
48
49
# File 'lib/carson/cli.rb', line 45

def self.build_parser
	OptionParser.new do |opts|
		opts.banner = "Usage: carson [setup|audit|sync|prune|prepare|inspect|onboard [repo_path]|refresh [repo_path]|offboard [repo_path]|template check|template apply|lint setup --source <path-or-git-url>|review gate|review sweep|govern [--dry-run] [--json] [--loop SECONDS]|housekeep|version]"
	end
end

.dispatch(parsed:, runtime:) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/carson/cli.rb', line 185

def self.dispatch( parsed:, runtime: )
	command = parsed.fetch( :command )
	return Runtime::EXIT_ERROR if command == :invalid

	case command
	when "setup"
		runtime.setup!
	when "audit"
		runtime.audit!
	when "sync"
		runtime.sync!
	when "prune"
		runtime.prune!
	when "prepare"
		runtime.prepare!
	when "inspect"
		runtime.inspect!
	when "onboard"
		runtime.onboard!
	when "refresh"
		runtime.refresh!
	when "offboard"
		runtime.offboard!
	when "template:check"
		runtime.template_check!
	when "template:apply"
		runtime.template_apply!
	when "lint:setup"
		runtime.lint_setup!(
			source: parsed.fetch( :source ),
			ref: parsed.fetch( :ref ),
			force: parsed.fetch( :force )
		)
	when "review:gate"
		runtime.review_gate!
	when "review:sweep"
		runtime.review_sweep!
	when "govern"
		runtime.govern!(
			dry_run: parsed.fetch( :dry_run, false ),
			json_output: parsed.fetch( :json, false ),
			loop_seconds: parsed.fetch( :loop_seconds, nil )
		)
	when "housekeep"
		runtime.housekeep!
	else
		runtime.send( :puts_line, "Unknown command: #{command}" )
		Runtime::EXIT_ERROR
	end
end

.parse_args(argv:, out:, err:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/carson/cli.rb', line 32

def self.parse_args( argv:, out:, err: )
	parser = build_parser
	preset = parse_preset_command( argv: argv, out: out, parser: parser )
	return preset unless preset.nil?

	command = argv.shift
	parse_command( command: command, argv: argv, parser: parser, err: err )
rescue OptionParser::ParseError => e
	err.puts "#{BADGE} #{e.message}"
	err.puts parser
	{ command: :invalid }
end

.parse_command(command:, argv:, parser:, err:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/carson/cli.rb', line 63

def self.parse_command( command:, argv:, parser:, err: )
	case command
	when "version"
		parser.parse!( argv )
		{ command: "version" }
	when "onboard", "refresh", "offboard"
		parse_repo_path_command( command: command, argv: argv, parser: parser, err: err )
	when "template"
		parse_named_subcommand( command: command, usage: "check|apply", argv: argv, parser: parser, err: err )
	when "lint"
		parse_lint_subcommand( argv: argv, parser: parser, err: err )
	when "review"
		parse_named_subcommand( command: command, usage: "gate|sweep", argv: argv, parser: parser, err: err )
	when "govern"
		parse_govern_subcommand( argv: argv, err: err )
	else
		parser.parse!( argv )
		{ command: command }
	end
end

.parse_govern_subcommand(argv:, err:) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/carson/cli.rb', line 152

def self.parse_govern_subcommand( argv:, err: )
	options = {
		dry_run: false,
		json: false,
		loop_seconds: nil
	}
	govern_parser = OptionParser.new do |opts|
		opts.banner = "Usage: carson govern [--dry-run] [--json] [--loop SECONDS]"
		opts.on( "--dry-run", "Run all checks but do not merge or dispatch" ) { options[ :dry_run ] = true }
		opts.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		opts.on( "--loop SECONDS", Integer, "Run continuously, sleeping SECONDS between cycles" ) do |s|
			err.puts( "#{BADGE} Error: --loop must be a positive integer" ) || ( return { command: :invalid } ) if s < 1
			options[ :loop_seconds ] = s
		end
	end
	govern_parser.parse!( argv )
	unless argv.empty?
		err.puts "#{BADGE} Unexpected arguments for govern: #{argv.join( ' ' )}"
		err.puts govern_parser
		return { command: :invalid }
	end
	{
		command: "govern",
		dry_run: options.fetch( :dry_run ),
		json: options.fetch( :json ),
		loop_seconds: options[ :loop_seconds ]
	}
rescue OptionParser::ParseError => e
	err.puts "#{BADGE} #{e.message}"
	err.puts govern_parser
	{ command: :invalid }
end

.parse_lint_subcommand(argv:, parser:, err:) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/carson/cli.rb', line 110

def self.parse_lint_subcommand( argv:, parser:, err: )
	action = argv.shift
	unless action == "setup"
		err.puts "#{BADGE} Missing or invalid subcommand for lint. Use: carson lint setup --source <path-or-git-url> [--ref <git-ref>] [--force]"
		err.puts parser
		return { command: :invalid }
	end

	options = {
		source: nil,
		ref: "main",
		force: false
	}
	lint_parser = OptionParser.new do |opts|
		opts.banner = "Usage: carson lint setup --source <path-or-git-url> [--ref <git-ref>] [--force]"
		opts.on( "--source SOURCE", "Source repository path or git URL that contains CODING/" ) { |value| options[ :source ] = value.to_s.strip }
		opts.on( "--ref REF", "Git ref used when --source is a git URL (default: main)" ) { |value| options[ :ref ] = value.to_s.strip }
		opts.on( "--force", "Overwrite existing files in ~/.carson/lint" ) { options[ :force ] = true }
	end
	lint_parser.parse!( argv )
	if options.fetch( :source ).to_s.empty?
		err.puts "#{BADGE} Missing required --source for lint setup."
		err.puts lint_parser
		return { command: :invalid }
	end
	unless argv.empty?
		err.puts "#{BADGE} Unexpected arguments for lint setup: #{argv.join( ' ' )}"
		err.puts lint_parser
		return { command: :invalid }
	end
	{
		command: "lint:setup",
		source: options.fetch( :source ),
		ref: options.fetch( :ref ),
		force: options.fetch( :force )
	}
rescue OptionParser::ParseError => e
	err.puts "#{BADGE} #{e.message}"
	err.puts lint_parser
	{ command: :invalid }
end

.parse_named_subcommand(command:, usage:, argv:, parser:, err:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/carson/cli.rb', line 99

def self.parse_named_subcommand( command:, usage:, argv:, parser:, err: )
	action = argv.shift
	parser.parse!( argv )
	if action.to_s.strip.empty?
		err.puts "#{BADGE} Missing subcommand for #{command}. Use: carson #{command} #{usage}"
		err.puts parser
		return { command: :invalid }
	end
	{ command: "#{command}:#{action}" }
end

.parse_preset_command(argv:, out:, parser:) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/carson/cli.rb', line 51

def self.parse_preset_command( argv:, out:, parser: )
	first = argv.first
	if [ "--help", "-h" ].include?( first )
		out.puts parser
		return { command: :help }
	end
	return { command: "version" } if [ "--version", "-v" ].include?( first )
	return { command: "audit" } if argv.empty?

	nil
end

.parse_repo_path_command(command:, argv:, parser:, err:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/carson/cli.rb', line 84

def self.parse_repo_path_command( command:, argv:, parser:, err: )
	parser.parse!( argv )
	if argv.length > 1
		err.puts "#{BADGE} Too many arguments for #{command}. Use: carson #{command} [repo_path]"
		err.puts parser
		return { command: :invalid }
	end

	repo_path = argv.first
	{
		command: command,
		repo_root: repo_path.to_s.strip.empty? ? nil : File.expand_path( repo_path )
	}
end

.start(argv:, repo_root:, tool_root:, out:, err:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/carson/cli.rb', line 5

def self.start( argv:, repo_root:, tool_root:, out:, err: )
	parsed = parse_args( argv: argv, out: out, err: err )
	command = parsed.fetch( :command )
	return Runtime::EXIT_OK if command == :help

	if command == "version"
		out.puts "#{BADGE} #{Carson::VERSION}"
		return Runtime::EXIT_OK
	end

	target_repo_root = parsed.fetch( :repo_root, nil )
	target_repo_root = repo_root if target_repo_root.to_s.strip.empty?
	unless Dir.exist?( target_repo_root )
		err.puts "#{BADGE} ERROR: repository path does not exist: #{target_repo_root}"
		return Runtime::EXIT_ERROR
	end

	runtime = Runtime.new( repo_root: target_repo_root, tool_root: tool_root, out: out, err: err )
	dispatch( parsed: parsed, runtime: runtime )
rescue ConfigError => e
	err.puts "#{BADGE} CONFIG ERROR: #{e.message}"
	Runtime::EXIT_ERROR
rescue StandardError => e
	err.puts "#{BADGE} ERROR: #{e.message}"
	Runtime::EXIT_ERROR
end