| 1 | # ABSTRACT: Kanban Assignment & Responsibility Registry | ||
| 2 | |||
| 3 | package App::karr; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 21 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 21 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 21 | ▶ | use MooX::Options; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 21 | ▶ | use Term::ANSIColor qw( colored ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | # Loaded without importing: this is a command class and runs no namespace::clean | ||
| 10 | # (MooX::Options forbids it), so `use ... qw( command_hint )` would compose | ||
| 11 | # command_hint as a method on the root. Called fully qualified below instead. | ||
| 12 | 21 | ▶ | use App::karr::Error (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use App::karr::Role::BoardAccess; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use App::karr::Role::Color; | ||
| 15 | use App::karr::Cmd::Board; | ||
| 16 | |||
| 17 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Color'; | ||
| 18 | |||
| 19 | |||
| 20 | # The --dir option is provided by App::karr::Role::BoardDiscovery (composed via | ||
| 21 | # BoardAccess), so it is a single, shared declaration usable both here on the | ||
| 22 | # root (`karr --dir PATH CMD`) and on every subcommand (`karr CMD --dir PATH`). | ||
| 23 | |||
| 24 | # Forwarded to the default board view so bare `karr --done` behaves like | ||
| 25 | # `karr board --done`. | ||
| 26 | option done => ( | ||
| 27 | is => 'ro', | ||
| 28 | doc => 'Include the board\'s final column in the default board view', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # The --no-color option is provided by App::karr::Role::Color (composed | ||
| 32 | # above), so it is a single, shared declaration usable both here on the root | ||
| 33 | # (`karr --no-color CMD`) and on the two renderers that colour their output | ||
| 34 | # (`karr board --no-color`, `karr dashboard --no-color`). | ||
| 35 | |||
| 36 | option version => ( | ||
| 37 | is => 'ro', | ||
| 38 | doc => 'Print the karr version and exit', | ||
| 39 | ); | ||
| 40 | |||
| 41 | # MooX::Cmd derives a command name from the class basename, so | ||
| 42 | # App::karr::Cmd::SetRefs is only ever spelled "setrefs" -- the documented | ||
| 43 | # dashed forms need registering as extra keys in the command table. | ||
| 44 | # | ||
| 45 | # Registering them here rather than rewriting $ARGV[0] in bin/karr is what makes | ||
| 46 | # them reachable with a root option in front (ticket #71): MooX::Cmd looks the | ||
| 47 | # command up with a first_index over this very table across the WHOLE argv, so | ||
| 48 | # `karr --dir PATH get-refs REF` leaves the alias at index 2, where the old | ||
| 49 | # position-0-only rewrite never saw it. Leaving argv untouched also keeps a | ||
| 50 | # payload that merely spells an alias intact -- `karr set-refs REF set-refs` | ||
| 51 | # stores "set-refs", it does not store "setrefs" -- because only the token | ||
| 52 | # MooX::Cmd itself dispatches on is ever consulted. | ||
| 53 | my %COMMAND_ALIASES = ( | ||
| 54 | 'set-refs' => 'setrefs', | ||
| 55 | 'get-refs' => 'getrefs', | ||
| 56 | 'agent-name' => 'agentname', | ||
| 57 | 'view' => 'show', | ||
| 58 | ); | ||
| 59 | |||
| 60 | around _build_command_commands => sub { | ||
| 61 | my ($orig, $self, @args) = @_; | ||
| 62 | my $commands = $orig->($self, @args); | ||
| 63 | for my $alias (keys %COMMAND_ALIASES) { | ||
| 64 | my $name = $COMMAND_ALIASES{$alias}; | ||
| 65 | $commands->{$alias} = $commands->{$name} if $commands->{$name}; | ||
| 66 | } | ||
| 67 | return $commands; | ||
| 68 | }; | ||
| 69 | |||
| 70 | my @COMMANDS = ( | ||
| 71 | [ init => 'Initialize a new karr board' ], | ||
| 72 | [ create => 'Create a new task' ], | ||
| 73 | [ list => 'List and filter tasks' ], | ||
| 74 | [ show => 'Show full task details' ], | ||
| 75 | [ board => 'Show board summary' ], | ||
| 76 | [ dashboard => 'Multi-board overview of boards under a directory' ], | ||
| 77 | [ move => 'Change task status' ], | ||
| 78 | [ edit => 'Modify task fields' ], | ||
| 79 | [ delete => 'Delete a task' ], | ||
| 80 | [ pick => 'Claim the next available task' ], | ||
| 81 | [ unlock => 'Show or break pick locks' ], | ||
| 82 | [ archive => 'Archive a task (soft-delete)' ], | ||
| 83 | [ handoff => 'Hand off a task for review' ], | ||
| 84 | [ needs => 'Report or resolve cross-board dependencies' ], | ||
| 85 | [ destroy => 'Delete the entire refs/karr/* board' ], | ||
| 86 | [ config => 'View or modify board config' ], | ||
| 87 | [ disable => 'Disable automated agent runs on this board' ], | ||
| 88 | [ enable => 'Re-enable automated agent runs on this board' ], | ||
| 89 | [ context => 'Generate board context summary' ], | ||
| 90 | [ log => 'Show activity log' ], | ||
| 91 | [ metrics => 'Show flow metrics' ], | ||
| 92 | [ backup => 'Export refs/karr/* as YAML' ], | ||
| 93 | [ restore => 'Replace refs/karr/* from YAML' ], | ||
| 94 | [ materialize => 'Write refs/karr/* out as a tasks/ file view' ], | ||
| 95 | [ import => 'Import a tasks/ file view into refs/karr/*' ], | ||
| 96 | [ repair => 'Migrate a 0.402-or-earlier board off double-encoded UTF-8' ], | ||
| 97 | [ sync => 'Sync board with remote' ], | ||
| 98 | [ 'agent-name' => 'Print the claim name for this checkout' ], | ||
| 99 | [ skill => 'Install/update agent skills' ], | ||
| 100 | [ 'set-refs' => 'Store helper payloads in a Git ref' ], | ||
| 101 | [ 'get-refs' => 'Fetch and print helper payloads from a Git ref' ], | ||
| 102 | [ completion => 'Generate shell completion scripts' ], | ||
| 103 | ); | ||
| 104 | |||
| 105 | # The command table `karr completion` generates from: every command with its | ||
| 106 | # description, aliases included. Completion needs the same table help prints, | ||
| 107 | # so it is exposed here rather than re-derived in the command. | ||
| 108 | sub command_table { | ||
| 109 | my %desc = map { $_->[0] => $_->[1] } @COMMANDS; | ||
| 110 | my @out = @COMMANDS; | ||
| 111 | for my $alias (keys %COMMAND_ALIASES) { | ||
| 112 | push @out, [ $COMMAND_ALIASES{$alias}, $desc{$alias} ]; | ||
| 113 | } | ||
| 114 | return @out; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | sub _print_help { | ||
| 119 | my ($self_or_class, $code) = @_; | ||
| 120 | $code //= 0; | ||
| 121 | |||
| 122 | my $out = ''; | ||
| 123 | $out .= colored("karr", 'bold') . " - Kanban Assignment & Responsibility Registry\n\n"; | ||
| 124 | $out .= colored("USAGE:", 'bold') . " karr [--dir PATH] <command> [options]\n\n"; | ||
| 125 | $out .= colored("COMMANDS:", 'bold') . "\n"; | ||
| 126 | |||
| 127 | my $max = 0; | ||
| 128 | for (@COMMANDS) { $max = length($_->[0]) if length($_->[0]) > $max } | ||
| 129 | |||
| 130 | # Pad on the VISIBLE width, then colour. sprintf's %-*s counts the ANSI | ||
| 131 | # escapes colored() wraps around the name, and those alone already exceed | ||
| 132 | # $max, so a "%-*s" over the coloured string never pads at all and the | ||
| 133 | # descriptions come out ragged. Padding by hand off the bare command name | ||
| 134 | # is correct whether or not colored() actually emits escapes (it returns | ||
| 135 | # the text untouched under NO_COLOR/ANSI_COLORS_DISABLED). | ||
| 136 | for my $cmd (@COMMANDS) { | ||
| 137 | $out .= sprintf " %s%s %s\n", | ||
| 138 | colored($cmd->[0], 'cyan'), | ||
| 139 | ' ' x ($max - length $cmd->[0]), | ||
| 140 | $cmd->[1]; | ||
| 141 | } | ||
| 142 | |||
| 143 | $out .= "\n" . colored("OPTIONS:", 'bold') . "\n"; | ||
| 144 | $out .= " --dir PATH Starting path for Git repository discovery\n"; | ||
| 145 | $out .= " --done Bare karr: include the board's final column (karr board --done)\n"; | ||
| 146 | $out .= " --no-color Disable colour output for this invocation\n"; | ||
| 147 | $out .= " --version Print the karr version and exit\n"; | ||
| 148 | $out .= " --json JSON output (most commands)\n"; | ||
| 149 | # Named in full rather than "(list, board)": --compact is declared by | ||
| 150 | # App::karr::Role::CompactOutput, which exactly these nine commands compose, | ||
| 151 | # and anywhere else it is an unknown option that exits 2 (#254). The old | ||
| 152 | # parenthesis named two of them and read like a shortened list. | ||
| 153 | $out .= " --compact Compact output (board, config, context, dashboard,\n"; | ||
| 154 | $out .= " list, log, metrics, pick, show)\n"; | ||
| 155 | $out .= "\n" . colored("EXAMPLES:", 'bold') . "\n"; | ||
| 156 | $out .= " karr init --name \"My Project\"\n"; | ||
| 157 | $out .= " karr create --title \"Fix login bug\" --priority high\n"; | ||
| 158 | $out .= " karr list --status todo,in-progress\n"; | ||
| 159 | $out .= " karr move 1 in-progress --claim agent-fox\n"; | ||
| 160 | $out .= " karr pick --claim agent-fox --move in-progress\n"; | ||
| 161 | $out .= " karr backup > karr-backup.yml\n"; | ||
| 162 | $out .= " karr restore --yes < karr-backup.yml\n"; | ||
| 163 | $out .= " karr set-refs superpowers/spec/1234.md draft ready\n"; | ||
| 164 | $out .= " karr board\n"; | ||
| 165 | $out .= "\nRun " . colored("karr <command> --help", 'bold') . " for command-specific options.\n"; | ||
| 166 | |||
| 167 | # Exit-code contract (ADR 0002): a positive code here is a usage/option-parse | ||
| 168 | # error from MooX::Options (unknown option, bad value on the root command), so | ||
| 169 | # normalize it to 2. Help requests (-h/--help) arrive with code 0 -> exit 0. | ||
| 170 | # A negative code means "print, do not exit" and is left untouched. | ||
| 171 | $code = 2 if $code > 0; | ||
| 172 | |||
| 173 | # The root reaches this instead of App::karr::Role::ExitCodes' options_usage | ||
| 174 | # wrapper (the `around` below hands it $code and never calls $orig), so the | ||
| 175 | # reordering of ticket k263 is asked for here by name: the diagnostic | ||
| 176 | # MooX::Options already wrote is buffered, and this puts it back AFTER the | ||
| 177 | # block above with the invocation that would have worked under it, then exits. | ||
| 178 | # It returns 0 when there is nothing to move -- a help request, or a call | ||
| 179 | # that did not come out of option parsing at all -- and has then printed | ||
| 180 | # whatever was buffered unchanged, which is what the two lines below expect. | ||
| 181 | $self_or_class->_usage_error_last( $out, $code ); | ||
| 182 | |||
| 183 | if ($code > 0) { warn $out } else { print $out } | ||
| 184 | exit $code if $code >= 0; | ||
| 185 | } | ||
| 186 | |||
| 187 | around options_usage => sub { $_[1]->_print_help($_[2]) }; | ||
| 188 | around options_help => sub { $_[1]->_print_help($_[2]) }; | ||
| 189 | around options_short_usage => sub { $_[1]->_print_help($_[2]) }; | ||
| 190 | |||
| 191 | sub execute { | ||
| 192 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 193 | |||
| 194 | # `karr --version` answers before anything else: no board, no repository, | ||
| 195 | # no subcommand is needed for it. | ||
| 196 | if ($self->version) { | ||
| 197 | print "karr $VERSION\n"; | ||
| 198 | exit 0; | ||
| 199 | } | ||
| 200 | |||
| 201 | # A leftover positional here means MooX::Cmd could not dispatch it to any | ||
| 202 | # App::karr::Cmd::* subcommand: it is an unknown command, not a request for | ||
| 203 | # the default board view. MooX::Cmd echoes already-parsed option flags AND | ||
| 204 | # the values they consumed (e.g. `--done`, or `--dir PATH` in space form) | ||
| 205 | # back into $args_ref, so run the leftover argv through the option-aware | ||
| 206 | # positional_args extractor rather than a raw non-dash grep -- otherwise a | ||
| 207 | # space-form option value such as the `--dir PATH` path is misread as an | ||
| 208 | # unknown bare command. Bare `karr` and `karr --done` legitimately fall | ||
| 209 | # through to the board summary below. | ||
| 210 | my ($unknown) = $self->positional_args($args_ref); | ||
| 211 | if (defined $unknown) { | ||
| 212 | # The way out was prose ("Run 'karr --help' ...") where it could be a line to | ||
| 213 | # copy (ticket k264). The "Unknown command:" marker MUST stay at the start of | ||
| 214 | # the first line -- App::karr::Error::is_usage_error and bin/karr classify the | ||
| 215 | # exit code on it (ADR 0002, exit 2) -- so the hint goes on the line after it, | ||
| 216 | # last, the way every k263 suggestion does. `--help` is a real token, not a | ||
| 217 | # placeholder, so the line is printed rather than withheld. | ||
| 218 | die "Unknown command: $unknown\n" . App::karr::Error::command_hint('--help') . "\n"; | ||
| 219 | } | ||
| 220 | |||
| 221 | # Default action: show board summary. The default Board is constructed | ||
| 222 | # directly (not dispatched by MooX::Cmd), so it has no command_chain to adopt | ||
| 223 | # --dir from; forward the root's own --dir explicitly so bare | ||
| 224 | # `karr --dir PATH` targets PATH rather than silently falling back to cwd. | ||
| 225 | # | ||
| 226 | # Board's own errors reach the CLI unchanged. This used to run in an eval that | ||
| 227 | # rewrote anything matching /No karr board found/ into that same sentence -- | ||
| 228 | # a no-op while the sentence was all there was to say, and a downgrade the | ||
| 229 | # moment it was not: bare `karr` in a fresh clone must say that refs/karr/* | ||
| 230 | # are merely unfetched and name 'karr sync', which is precisely the wording | ||
| 231 | # that rewrite would have thrown away (#135). | ||
| 232 | my %board_args = ( | ||
| 233 | done => $self->done, | ||
| 234 | ); | ||
| 235 | $board_args{dir} = $self->dir if $self->has_dir; | ||
| 236 | # The default Board is constructed directly (no command_chain to adopt | ||
| 237 | # --no-color from), so forward the root's own decision explicitly. | ||
| 238 | $board_args{color} = 0 if defined $self->color && !$self->color; | ||
| 239 | App::karr::Cmd::Board->new(%board_args)->execute($args_ref, $chain_ref); | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | |||
| 244 | __END__ | ||