Class: Carson::CLI
- Inherits:
-
Object
- Object
- Carson::CLI
- Defined in:
- lib/carson/cli.rb
Class Method Summary collapse
- .build_parser ⇒ Object
- .dispatch(parsed:, runtime:) ⇒ Object
- .parse_args(argv:, out:, err:) ⇒ Object
- .parse_command(command:, argv:, parser:, err:) ⇒ Object
- .parse_govern_subcommand(argv:, err:) ⇒ Object
- .parse_named_subcommand(command:, usage:, argv:, parser:, err:) ⇒ Object
- .parse_preset_command(argv:, out:, parser:) ⇒ Object
- .parse_prune_command(argv:, parser:, err:) ⇒ Object
- .parse_refresh_command(argv:, parser:, err:) ⇒ Object
- .parse_repo_path_command(command:, argv:, parser:, err:) ⇒ Object
- .parse_setup_command(argv:, parser:, err:) ⇒ Object
- .parse_status_command(argv:, err:) ⇒ Object
- .parse_template_subcommand(argv:, parser:, err:) ⇒ Object
- .parse_worktree_subcommand(argv:, parser:, err:) ⇒ Object
- .start(argv:, repo_root:, tool_root:, out:, err:) ⇒ Object
Class Method Details
.build_parser ⇒ Object
54 55 56 57 58 |
# File 'lib/carson/cli.rb', line 54 def self.build_parser OptionParser.new do |opts| opts. = "Usage: carson [status [--json]|setup [--remote NAME] [--main-branch NAME] [--workflow STYLE] [--merge METHOD] [--canonical PATH]|audit|sync|prune [--all]|worktree remove <name-or-path>|onboard [repo_path]|refresh [--all|repo_path]|offboard [repo_path]|template check|template apply|review gate|review sweep|govern [--dry-run] [--json] [--loop SECONDS]|version]" end end |
.dispatch(parsed:, runtime:) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/carson/cli.rb', line 275 def self.dispatch( parsed:, runtime: ) command = parsed.fetch( :command ) return Runtime::EXIT_ERROR if command == :invalid case command when "status" runtime.status!( json_output: parsed.fetch( :json, false ) ) when "setup" runtime.setup!( cli_choices: parsed.fetch( :cli_choices, {} ) ) when "audit" runtime.audit! when "sync" runtime.sync! when "prune" runtime.prune! when "prune:all" runtime.prune_all! when "worktree:remove" runtime.worktree_remove!( worktree_path: parsed.fetch( :worktree_path ), force: parsed.fetch( :force, false ) ) when "onboard" runtime.onboard! when "refresh" runtime.refresh! when "refresh:all" runtime.refresh_all! when "offboard" runtime.offboard! when "template:check" runtime.template_check! when "template:apply" runtime.template_apply!( push_prep: parsed.fetch( :push_prep, false ) ) 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 ) ) else runtime.send( :puts_line, "Unknown command: #{command}" ) Runtime::EXIT_ERROR end end |
.parse_args(argv:, out:, err:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/carson/cli.rb', line 39 def self.parse_args( argv:, out:, err: ) verbose = argv.delete( "--verbose" ) ? true : false parser = build_parser preset = parse_preset_command( argv: argv, out: out, parser: parser ) return preset.merge( verbose: verbose ) unless preset.nil? command = argv.shift result = parse_command( command: command, argv: argv, parser: parser, err: err ) result.merge( verbose: verbose ) rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" err.puts parser { command: :invalid } end |
.parse_command(command:, argv:, parser:, err:) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/carson/cli.rb', line 72 def self.parse_command( command:, argv:, parser:, err: ) case command when "version" parser.parse!( argv ) { command: "version" } when "setup" parse_setup_command( argv: argv, parser: parser, err: err ) when "onboard", "offboard" parse_repo_path_command( command: command, argv: argv, parser: parser, err: err ) when "refresh" parse_refresh_command( argv: argv, parser: parser, err: err ) when "template" parse_template_subcommand( argv: argv, parser: parser, err: err ) when "prune" parse_prune_command( argv: argv, parser: parser, err: err ) when "worktree" parse_worktree_subcommand( argv: argv, parser: parser, err: err ) when "review" parse_named_subcommand( command: command, usage: "gate|sweep", argv: argv, parser: parser, err: err ) when "status" parse_status_command( argv: argv, 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
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/carson/cli.rb', line 242 def self.parse_govern_subcommand( argv:, err: ) = { dry_run: false, json: false, loop_seconds: nil } govern_parser = OptionParser.new do |opts| opts. = "Usage: carson govern [--dry-run] [--json] [--loop SECONDS]" opts.on( "--dry-run", "Run all checks but do not merge or dispatch" ) { [ :dry_run ] = true } opts.on( "--json", "Machine-readable JSON output" ) { [ :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 [ :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: .fetch( :dry_run ), json: .fetch( :json ), loop_seconds: [ :loop_seconds ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" err.puts govern_parser { command: :invalid } end |
.parse_named_subcommand(command:, usage:, argv:, parser:, err:) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/carson/cli.rb', line 193 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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/carson/cli.rb', line 60 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_prune_command(argv:, parser:, err:) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/carson/cli.rb', line 163 def self.parse_prune_command( argv:, parser:, err: ) all_flag = argv.delete( "--all" ) ? true : false parser.parse!( argv ) return { command: "prune:all" } if all_flag { command: "prune" } end |
.parse_refresh_command(argv:, parser:, err:) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/carson/cli.rb', line 138 def self.parse_refresh_command( argv:, parser:, err: ) all_flag = argv.delete( "--all" ) ? true : false parser.parse!( argv ) if all_flag && !argv.empty? err.puts "#{BADGE} --all and repo_path are mutually exclusive. Use: carson refresh --all OR carson refresh [repo_path]" err.puts parser return { command: :invalid } end return { command: "refresh:all" } if all_flag if argv.length > 1 err.puts "#{BADGE} Too many arguments for refresh. Use: carson refresh [repo_path]" err.puts parser return { command: :invalid } end repo_path = argv.first { command: "refresh", repo_root: repo_path.to_s.strip.empty? ? nil : File.( repo_path ) } end |
.parse_repo_path_command(command:, argv:, parser:, err:) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/carson/cli.rb', line 123 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.( repo_path ) } end |
.parse_setup_command(argv:, parser:, err:) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/carson/cli.rb', line 101 def self.parse_setup_command( argv:, parser:, err: ) = {} setup_parser = OptionParser.new do |opts| opts. = "Usage: carson setup [--remote NAME] [--main-branch NAME] [--workflow STYLE] [--merge METHOD] [--canonical PATH]" opts.on( "--remote NAME", "Git remote name" ) { |v| [ "git.remote" ] = v } opts.on( "--main-branch NAME", "Main branch name" ) { |v| [ "git.main_branch" ] = v } opts.on( "--workflow STYLE", "Workflow style (branch or trunk)" ) { |v| [ "workflow.style" ] = v } opts.on( "--merge METHOD", "Merge method (squash, rebase, or merge)" ) { |v| [ "govern.merge.method" ] = v } opts.on( "--canonical PATH", "Canonical template directory path" ) { |v| [ "template.canonical" ] = v } end setup_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for setup: #{argv.join( ' ' )}" err.puts setup_parser return { command: :invalid } end { command: "setup", cli_choices: } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_status_command(argv:, err:) ⇒ Object
233 234 235 236 237 238 239 240 |
# File 'lib/carson/cli.rb', line 233 def self.parse_status_command( argv:, err: ) json_flag = argv.delete( "--json" ) ? true : false unless argv.empty? err.puts "#{BADGE} Unexpected arguments for status: #{argv.join( ' ' )}" return { command: :invalid } end { command: "status", json: json_flag } end |
.parse_template_subcommand(argv:, parser:, err:) ⇒ Object
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 |
# File 'lib/carson/cli.rb', line 204 def self.parse_template_subcommand( argv:, parser:, err: ) action = argv.shift if action.to_s.strip.empty? err.puts "#{BADGE} Missing subcommand for template. Use: carson template check|apply" err.puts parser return { command: :invalid } end return { command: "template:#{action}" } unless action == "apply" = { push_prep: false } apply_parser = OptionParser.new do |opts| opts. = "Usage: carson template apply [--push-prep]" opts.on( "--push-prep", "Apply templates and auto-commit any managed file changes (used by pre-push hook)" ) do [ :push_prep ] = true end end apply_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for template apply: #{argv.join( ' ' )}" err.puts apply_parser return { command: :invalid } end { command: "template:apply", push_prep: .fetch( :push_prep ) } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_worktree_subcommand(argv:, parser:, err:) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/carson/cli.rb', line 170 def self.parse_worktree_subcommand( argv:, parser:, err: ) action = argv.shift if action.to_s.strip.empty? err.puts "#{BADGE} Missing subcommand for worktree. Use: carson worktree remove <name-or-path>" err.puts parser return { command: :invalid } end case action when "remove" force = argv.delete( "--force" ) ? true : false worktree_path = argv.shift if worktree_path.to_s.strip.empty? err.puts "#{BADGE} Missing path for worktree remove. Use: carson worktree remove <name-or-path>" return { command: :invalid } end { command: "worktree:remove", worktree_path: worktree_path, force: force } else err.puts "#{BADGE} Unknown worktree subcommand: #{action}. Use: carson worktree remove <name-or-path>" { command: :invalid } end 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 31 32 33 34 35 36 37 |
# 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 if %w[refresh:all prune:all].include?( command ) verbose = parsed.fetch( :verbose, false ) runtime = Runtime.new( repo_root: repo_root, tool_root: tool_root, out: out, err: err, verbose: verbose ) return dispatch( parsed: parsed, runtime: runtime ) 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 verbose = parsed.fetch( :verbose, false ) runtime = Runtime.new( repo_root: target_repo_root, tool_root: tool_root, out: out, err: err, verbose: verbose ) dispatch( parsed: parsed, runtime: runtime ) rescue ConfigError => e err.puts "#{BADGE} CONFIG ERROR: #{e.}" Runtime::EXIT_ERROR rescue StandardError => e err.puts "#{BADGE} ERROR: #{e.}" Runtime::EXIT_ERROR end |