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
— dispatch —.
- .parse_args(argv:, out:, err:) ⇒ Object
-
.parse_audit_command(argv:, err:) ⇒ Object
— audit —.
- .parse_command(command:, argv:, err:) ⇒ Object
-
.parse_deliver_command(argv:, err:) ⇒ Object
— deliver —.
-
.parse_govern_subcommand(argv:, err:) ⇒ Object
— govern —.
-
.parse_housekeep_command(argv:, err:) ⇒ Object
— housekeep —.
- .parse_offboard_command(argv:, err:) ⇒ Object
-
.parse_onboard_command(argv:, err:) ⇒ Object
— onboard / offboard —.
- .parse_preset_command(argv:, out:, parser:) ⇒ Object
-
.parse_prune_command(argv:, err:) ⇒ Object
— prune —.
-
.parse_refresh_command(argv:, err:) ⇒ Object
— refresh —.
-
.parse_repos_command(argv:, err:) ⇒ Object
— repos —.
-
.parse_review_subcommand(argv:, err:) ⇒ Object
— review —.
-
.parse_setup_command(argv:, err:) ⇒ Object
— setup —.
-
.parse_status_command(argv:, err:) ⇒ Object
— status —.
-
.parse_sync_command(argv:, err:) ⇒ Object
— sync —.
-
.parse_template_subcommand(argv:, err:) ⇒ Object
— template —.
-
.parse_worktree_subcommand(argv:, err:) ⇒ Object
— worktree —.
- .start(argv:, repo_root:, tool_root:, out:, err:) ⇒ Object
Class Method Details
.build_parser ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/carson/cli.rb', line 54 def self.build_parser OptionParser.new do |opts| opts. = "Usage: carson <command> [options]" opts.separator "" opts.separator "Repository governance and workflow automation for coding agents." opts.separator "" opts.separator "Commands:" opts.separator " status Show repository state (branch, PRs, worktrees)" opts.separator " setup Initialise Carson configuration" opts.separator " audit Run pre-commit health checks" opts.separator " sync Sync local main with remote" opts.separator " deliver Push, create PR, and optionally merge" opts.separator " prune Remove stale local branches" opts.separator " worktree Manage isolated coding worktrees" opts.separator " housekeep Sync, reap worktrees, and prune branches" opts.separator " repos List governed repositories" opts.separator " onboard Register a repository for governance" opts.separator " offboard Remove a repository from governance" opts.separator " refresh Re-install hooks and configuration" opts.separator " template Manage canonical template files" opts.separator " review Manage PR review workflow" opts.separator " govern Portfolio-level PR triage loop" opts.separator " version Show Carson version" opts.separator "" opts.separator "Run `carson <command> --help` for details on a specific command." end end |
.dispatch(parsed:, runtime:) ⇒ Object
— dispatch —
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 |
# File 'lib/carson/cli.rb', line 679 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!( json_output: parsed.fetch( :json, false ) ) when "sync" runtime.sync!( json_output: parsed.fetch( :json, false ) ) when "prune" runtime.prune!( json_output: parsed.fetch( :json, false ) ) when "prune:all" runtime.prune_all! when "worktree:create" runtime.worktree_create!( name: parsed.fetch( :worktree_name ), json_output: parsed.fetch( :json, false ) ) when "worktree:remove" runtime.worktree_remove!( worktree_path: parsed.fetch( :worktree_path ), force: parsed.fetch( :force, false ), json_output: parsed.fetch( :json, 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 "deliver" runtime.deliver!( merge: parsed.fetch( :merge, false ), title: parsed.fetch( :title, nil ), body_file: parsed.fetch( :body_file, nil ), json_output: parsed.fetch( :json, false ) ) when "review:gate" runtime.review_gate! when "review:sweep" runtime.review_sweep! when "repos" runtime.repos!( json_output: parsed.fetch( :json, false ) ) when "housekeep" runtime.housekeep!( json_output: parsed.fetch( :json, false ) ) when "housekeep:target" runtime.housekeep_target!( target: parsed.fetch( :target ), json_output: parsed.fetch( :json, false ) ) when "housekeep:all" runtime.housekeep_all!( json_output: parsed.fetch( :json, false ) ) 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, err: err ) result.merge( verbose: verbose ) rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" err.puts parser { command: :invalid } end |
.parse_audit_command(argv:, err:) ⇒ Object
— audit —
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/carson/cli.rb', line 440 def self.parse_audit_command( argv:, err: ) = { json: false } audit_parser = OptionParser.new do |opts| opts. = "Usage: carson audit [--json]" opts.separator "" opts.separator "Run pre-commit health checks on the repository." opts.separator "Validates hooks, main-branch sync, PR status, and CI baseline." opts.separator "Exits with a non-zero status when policy violations are found." opts.separator "" opts.separator "Options:" opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson audit Check repository health (also the default command)" opts.separator " carson audit --json Structured output for agent consumption" end audit_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for audit: #{argv.join( ' ' )}" return { command: :invalid } end { command: "audit", json: [ :json ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_command(command:, argv:, err:) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/carson/cli.rb', line 94 def self.parse_command( command:, argv:, err: ) case command when "version" { command: "version" } when "setup" parse_setup_command( argv: argv, err: err ) when "onboard" parse_onboard_command( argv: argv, err: err ) when "offboard" parse_offboard_command( argv: argv, err: err ) when "refresh" parse_refresh_command( argv: argv, err: err ) when "template" parse_template_subcommand( argv: argv, err: err ) when "prune" parse_prune_command( argv: argv, err: err ) when "worktree" parse_worktree_subcommand( argv: argv, err: err ) when "repos" parse_repos_command( argv: argv, err: err ) when "housekeep" parse_housekeep_command( argv: argv, err: err ) when "review" parse_review_subcommand( argv: argv, err: err ) when "audit" parse_audit_command( argv: argv, err: err ) when "sync" parse_sync_command( argv: argv, err: err ) when "status" parse_status_command( argv: argv, err: err ) when "deliver" parse_deliver_command( argv: argv, err: err ) when "govern" parse_govern_subcommand( argv: argv, err: err ) else { command: command } end end |
.parse_deliver_command(argv:, err:) ⇒ Object
— deliver —
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/carson/cli.rb', line 525 def self.parse_deliver_command( argv:, err: ) = { merge: false, json: false, title: nil, body_file: nil } deliver_parser = OptionParser.new do |opts| opts. = "Usage: carson deliver [--merge] [--json] [--title TITLE] [--body-file PATH]" opts.separator "" opts.separator "Push the current branch, create a pull request, and optionally merge." opts.separator "Collapses the manual push → PR → merge flow into a single command." opts.separator "" opts.separator "Options:" opts.on( "--merge", "Also merge the PR if CI passes" ) { [ :merge ] = true } opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.on( "--title TITLE", "PR title (defaults to branch name)" ) { |v| [ :title ] = v } opts.on( "--body-file PATH", "File containing PR body text" ) { |v| [ :body_file ] = v } opts.separator "" opts.separator "Examples:" opts.separator " carson deliver Push and open a PR" opts.separator " carson deliver --merge Push, open a PR, and merge if CI passes" end deliver_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for deliver: #{argv.join( ' ' )}" err.puts deliver_parser return { command: :invalid } end { command: "deliver", merge: .fetch( :merge ), json: .fetch( :json ), title: [ :title ], body_file: [ :body_file ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_govern_subcommand(argv:, err:) ⇒ Object
— govern —
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
# File 'lib/carson/cli.rb', line 633 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.separator "" opts.separator "Portfolio-level PR triage loop." opts.separator "Scans governed repositories, classifies open PRs, and takes action" opts.separator "(merge, request review, or report). Runs once by default." opts.separator "" opts.separator "Options:" 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 opts.separator "" opts.separator "Examples:" opts.separator " carson govern Triage all governed repos once" opts.separator " carson govern --dry-run Preview actions without applying them" opts.separator " carson govern --loop 300 Run continuously every 5 minutes" 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_housekeep_command(argv:, err:) ⇒ Object
— housekeep —
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/carson/cli.rb', line 591 def self.parse_housekeep_command( argv:, err: ) = { all: false, json: false } housekeep_parser = OptionParser.new do |opts| opts. = "Usage: carson housekeep [REPO] [--all] [--json]" opts.separator "" opts.separator "Run housekeeping: sync main, reap dead worktrees, and prune stale branches." opts.separator "Defaults to the current repository." opts.separator "" opts.separator "Options:" opts.on( "--all", "Housekeep all governed repositories" ) { [ :all ] = true } opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson housekeep Housekeep the current repository" opts.separator " carson housekeep nexus Housekeep a named governed repo" opts.separator " carson housekeep --all Housekeep all governed repos" end housekeep_parser.parse!( argv ) if [ :all ] && !argv.empty? err.puts "#{BADGE} --all and repo target are mutually exclusive. Use: carson housekeep --all OR carson housekeep [repo]" return { command: :invalid } end return { command: "housekeep:all", json: [ :json ] } if [ :all ] if argv.length > 1 err.puts "#{BADGE} Too many arguments for housekeep. Use: carson housekeep [repo]" return { command: :invalid } end target = argv.shift return { command: "housekeep:target", target: target, json: [ :json ] } if target { command: "housekeep", json: [ :json ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_offboard_command(argv:, err:) ⇒ Object
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 |
# File 'lib/carson/cli.rb', line 198 def self.parse_offboard_command( argv:, err: ) offboard_parser = OptionParser.new do |opts| opts. = "Usage: carson offboard [REPO_PATH]" opts.separator "" opts.separator "Remove a repository from Carson governance." opts.separator "Unregisters the repo from Carson's portfolio and removes hooks." opts.separator "Defaults to the current directory if no path is given." opts.separator "" opts.separator "Examples:" opts.separator " carson offboard Offboard the current repository" end offboard_parser.parse!( argv ) if argv.length > 1 err.puts "#{BADGE} Too many arguments for offboard. Use: carson offboard [repo_path]" err.puts offboard_parser return { command: :invalid } end repo_path = argv.first { command: "offboard", repo_root: repo_path.to_s.strip.empty? ? nil : File.( repo_path ) } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_onboard_command(argv:, err:) ⇒ Object
— onboard / offboard —
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/carson/cli.rb', line 170 def self.parse_onboard_command( argv:, err: ) onboard_parser = OptionParser.new do |opts| opts. = "Usage: carson onboard [REPO_PATH]" opts.separator "" opts.separator "Register a repository for Carson governance." opts.separator "Detects the remote, installs hooks, applies templates, and runs initial audit." opts.separator "Defaults to the current directory if no path is given." opts.separator "" opts.separator "Examples:" opts.separator " carson onboard Onboard the current repository" opts.separator " carson onboard ~/Dev/app Onboard a specific repository" end onboard_parser.parse!( argv ) if argv.length > 1 err.puts "#{BADGE} Too many arguments for onboard. Use: carson onboard [repo_path]" err.puts onboard_parser return { command: :invalid } end repo_path = argv.first { command: "onboard", repo_root: repo_path.to_s.strip.empty? ? nil : File.( repo_path ) } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_preset_command(argv:, out:, parser:) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/carson/cli.rb', line 82 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:, err:) ⇒ Object
— prune —
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/carson/cli.rb', line 270 def self.parse_prune_command( argv:, err: ) = { all: false, json: false } prune_parser = OptionParser.new do |opts| opts. = "Usage: carson prune [--all] [--json]" opts.separator "" opts.separator "Remove stale local branches." opts.separator "Cleans up branches gone from the remote, orphan branches with merged PRs," opts.separator "and absorbed branches whose content is already on main." opts.separator "" opts.separator "Options:" opts.on( "--all", "Prune all governed repositories" ) { [ :all ] = true } opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson prune Clean up stale branches in this repo" opts.separator " carson prune --all Clean up across all governed repos" end prune_parser.parse!( argv ) return { command: "prune:all", json: [ :json ] } if [ :all ] { command: "prune", json: [ :json ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_refresh_command(argv:, err:) ⇒ Object
— refresh —
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 |
# File 'lib/carson/cli.rb', line 227 def self.parse_refresh_command( argv:, err: ) = { all: false } refresh_parser = OptionParser.new do |opts| opts. = "Usage: carson refresh [--all] [REPO_PATH]" opts.separator "" opts.separator "Re-install Carson hooks and configuration for a repository." opts.separator "Defaults to the current directory. Use --all to refresh all governed repos." opts.separator "" opts.separator "Options:" opts.on( "--all", "Refresh all governed repositories" ) { [ :all ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson refresh Refresh the current repository" opts.separator " carson refresh --all Refresh all governed repos" end refresh_parser.parse!( argv ) if [ :all ] && !argv.empty? err.puts "#{BADGE} --all and repo_path are mutually exclusive. Use: carson refresh --all OR carson refresh [repo_path]" err.puts refresh_parser return { command: :invalid } end return { command: "refresh:all" } if [ :all ] if argv.length > 1 err.puts "#{BADGE} Too many arguments for refresh. Use: carson refresh [repo_path]" err.puts refresh_parser return { command: :invalid } end repo_path = argv.first { command: "refresh", repo_root: repo_path.to_s.strip.empty? ? nil : File.( repo_path ) } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_repos_command(argv:, err:) ⇒ Object
— repos —
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/carson/cli.rb', line 563 def self.parse_repos_command( argv:, err: ) = { json: false } repos_parser = OptionParser.new do |opts| opts. = "Usage: carson repos [--json]" opts.separator "" opts.separator "List all repositories governed by Carson." opts.separator "Shows the portfolio of repos registered via carson onboard." opts.separator "" opts.separator "Options:" opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson repos List governed repositories" opts.separator " carson repos --json Structured output for agent consumption" end repos_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for repos: #{argv.join( ' ' )}" return { command: :invalid } end { command: "repos", json: [ :json ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_review_subcommand(argv:, err:) ⇒ Object
— review —
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/carson/cli.rb', line 353 def self.parse_review_subcommand( argv:, err: ) review_parser = OptionParser.new do |opts| opts. = "Usage: carson review <gate|sweep>" opts.separator "" opts.separator "Manage PR review workflow." opts.separator "" opts.separator "Subcommands:" opts.separator " gate Check if review requirements are met for merge" opts.separator " sweep Scan and resolve pending review threads" opts.separator "" opts.separator "Examples:" opts.separator " carson review gate Check merge readiness" opts.separator " carson review sweep Resolve pending review threads" end review_parser.parse!( argv ) action = argv.shift if action.to_s.strip.empty? err.puts "#{BADGE} Missing subcommand for review. Use: carson review gate|sweep" err.puts review_parser return { command: :invalid } end { command: "review:#{action}" } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_setup_command(argv:, err:) ⇒ Object
— setup —
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/carson/cli.rb', line 135 def self.parse_setup_command( argv:, err: ) = {} setup_parser = OptionParser.new do |opts| opts. = "Usage: carson setup [--remote NAME] [--main-branch NAME] [--workflow STYLE] [--merge METHOD] [--canonical PATH]" opts.separator "" opts.separator "Initialise Carson configuration for the current repository." opts.separator "Detects git remote, main branch, and workflow style, then writes .carson.yml." opts.separator "Pass flags to override detected values." opts.separator "" opts.separator "Options:" 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 } opts.separator "" opts.separator "Examples:" opts.separator " carson setup Auto-detect and write config" opts.separator " carson setup --remote github Use 'github' as the git remote" opts.separator " carson setup --merge squash Set squash as the merge method" 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
— status —
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/carson/cli.rb', line 497 def self.parse_status_command( argv:, err: ) = { json: false } status_parser = OptionParser.new do |opts| opts. = "Usage: carson status [--json]" opts.separator "" opts.separator "Show the current state of the repository." opts.separator "Reports branch, worktrees, open PRs, stale branches, and version." opts.separator "" opts.separator "Options:" opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson status Quick overview of repository state" opts.separator " carson status --json Structured output for agent consumption" end status_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for status: #{argv.join( ' ' )}" return { command: :invalid } end { command: "status", json: [ :json ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_sync_command(argv:, err:) ⇒ Object
— sync —
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/carson/cli.rb', line 469 def self.parse_sync_command( argv:, err: ) = { json: false } sync_parser = OptionParser.new do |opts| opts. = "Usage: carson sync [--json]" opts.separator "" opts.separator "Sync the local main branch with the remote." opts.separator "Fetches and fast-forwards main without switching branches." opts.separator "" opts.separator "Options:" opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson sync Pull latest changes from remote main" opts.separator " carson sync --json Structured output for agent consumption" end sync_parser.parse!( argv ) unless argv.empty? err.puts "#{BADGE} Unexpected arguments for sync: #{argv.join( ' ' )}" return { command: :invalid } end { command: "sync", json: [ :json ] } rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } end |
.parse_template_subcommand(argv:, err:) ⇒ Object
— template —
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/carson/cli.rb', line 383 def self.parse_template_subcommand( argv:, err: ) # Handle parent-level help or missing subcommand. if argv.empty? || [ "--help", "-h" ].include?( argv.first ) template_parser = OptionParser.new do |opts| opts. = "Usage: carson template <check|apply> [options]" opts.separator "" opts.separator "Manage canonical template files (CI workflows, lint configs)." opts.separator "" opts.separator "Subcommands:" opts.separator " check Show template drift without making changes" opts.separator " apply [--push-prep] Sync templates into the repository" opts.separator "" opts.separator "Examples:" opts.separator " carson template check Check for template drift" opts.separator " carson template apply Apply canonical templates" end if argv.empty? err.puts "#{BADGE} Missing subcommand for template. Use: carson template check|apply" err.puts template_parser return { command: :invalid } end # Let OptionParser handle --help (prints and exits). template_parser.parse!( argv ) return { command: :help } end action = argv.shift return { command: "template:#{action}" } unless action == "apply" = { push_prep: false } apply_parser = OptionParser.new do |opts| opts. = "Usage: carson template apply [--push-prep]" opts.separator "" opts.separator "Sync canonical template files (CI workflows, lint configs) into the repository." opts.separator "Copies managed files from the configured canonical directory." opts.separator "" opts.separator "Options:" 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:, err:) ⇒ Object
— worktree —
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/carson/cli.rb', line 297 def self.parse_worktree_subcommand( argv:, err: ) = { json: false, force: false } worktree_parser = OptionParser.new do |opts| opts. = "Usage: carson worktree <create|remove> <name> [options]" opts.separator "" opts.separator "Manage isolated worktrees for coding agents." opts.separator "Create auto-syncs main before branching. Remove guards against" opts.separator "unpushed commits and CWD-inside-worktree by default." opts.separator "" opts.separator "Subcommands:" opts.separator " create <name> Create a new worktree with a fresh branch" opts.separator " remove <name> [--force] Remove a worktree (--force skips safety checks)" opts.separator "" opts.separator "Options:" opts.on( "--json", "Machine-readable JSON output" ) { [ :json ] = true } opts.on( "--force", "Skip safety checks on remove" ) { [ :force ] = true } opts.separator "" opts.separator "Examples:" opts.separator " carson worktree create feature-x Create an isolated worktree" opts.separator " carson worktree remove feature-x Remove after work is pushed" end worktree_parser.parse!( argv ) action = argv.shift if action.to_s.strip.empty? err.puts "#{BADGE} Missing subcommand for worktree. Use: carson worktree create|remove <name>" err.puts worktree_parser return { command: :invalid } end case action when "create" name = argv.shift if name.to_s.strip.empty? err.puts "#{BADGE} Missing name for worktree create. Use: carson worktree create <name>" return { command: :invalid } end { command: "worktree:create", worktree_name: name, json: [ :json ] } when "remove" 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 ], json: [ :json ] } else err.puts "#{BADGE} Unknown worktree subcommand: #{action}. Use: carson worktree create|remove <name>" { command: :invalid } end rescue OptionParser::ParseError => e err.puts "#{BADGE} #{e.}" { command: :invalid } 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[repos refresh:all prune:all housekeep:all housekeep:target].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 |