| 1 | # ABSTRACT: Fetch helper payloads from a Git ref | ||
| 2 | |||
| 3 | package App::karr::Cmd::GetRefs; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use MooX::Options ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | usage_string => 'USAGE: karr get-refs REF', | |
| 9 | 1 | ); | |
| 10 | 0 | ▶ | use App::karr::Git; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use App::karr::Role::CliArgs; | ||
| 12 | use App::karr::Role::ExitCodes; | ||
| 13 | |||
| 14 | # Unknown option / bad option value exits 2, not 1 (ADR 0002 exit-code | ||
| 15 | # contract). This board-less command has no BoardDiscovery to inherit it from. | ||
| 16 | with 'App::karr::Role::CliArgs', 'App::karr::Role::ExitCodes'; | ||
| 17 | |||
| 18 | # Declared locally rather than inherited from App::karr::Role::BoardDiscovery: | ||
| 19 | # this command deliberately has no board, it only needs the discovery seed to | ||
| 20 | # find the repository the helper ref lives in. Both documented placements now | ||
| 21 | # work (ticket #71): `karr get-refs REF --dir PATH` binds this option, while | ||
| 22 | # `karr --dir PATH get-refs REF` leaves --dir on the root and is adopted from | ||
| 23 | # the MooX::Cmd command_chain in execute(). Declaring it with format=s is also | ||
| 24 | # what lets positional_args skip `--dir PATH` instead of reading the path as | ||
| 25 | # the ref. | ||
| 26 | option dir => ( | ||
| 27 | is => 'ro', | ||
| 28 | format => 's', | ||
| 29 | doc => 'Path used as the starting point for Git repository discovery', | ||
| 30 | predicate => 1, | ||
| 31 | ); | ||
| 32 | |||
| 33 | |||
| 34 | sub execute { | ||
| 35 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 36 | my ($ref_input) = $self->positional_args($args_ref); | ||
| 37 | die "Usage: karr get-refs REF\n" unless defined $ref_input; | ||
| 38 | |||
| 39 | my $repo_dir = '.'; | ||
| 40 | if ($self->has_dir) { | ||
| 41 | $repo_dir = $self->dir; | ||
| 42 | } | ||
| 43 | elsif ($chain_ref && @$chain_ref) { | ||
| 44 | my $root = $chain_ref->[0]; | ||
| 45 | if ($root && $root->can('has_dir') && $root->has_dir) { | ||
| 46 | $repo_dir = $root->dir; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | my $git = App::karr::Git->new(dir => $repo_dir); | ||
| 51 | die "Not a git repository.\n" unless $git->is_repo; | ||
| 52 | |||
| 53 | my $ref = $git->validate_helper_ref($ref_input); | ||
| 54 | $git->pull_ref($ref) or die "Failed to fetch $ref\n"; | ||
| 55 | |||
| 56 | # read_ref cannot tell "absent" from "present but empty" -- it answers '' for | ||
| 57 | # both -- so ask before reading. Without this the command reported success and | ||
| 58 | # printed one empty line for a ref that does not exist, which turns the | ||
| 59 | # documented `karr get-refs spec.md > spec.md` shape into silent data loss | ||
| 60 | # (ticket #74). A runtime failure, so exit 1 per ADR 0002. Checked after | ||
| 61 | # pull_ref so a ref that only exists on the remote is fetched first. | ||
| 62 | die "Ref $ref not found\n" unless $git->ref_exists($ref); | ||
| 63 | |||
| 64 | my $content = $git->read_ref($ref); | ||
| 65 | print STDERR "Fetched $ref\n"; | ||
| 66 | print $content; | ||
| 67 | print "\n" unless $content =~ /\n\z/; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | |||
| 72 | __END__ | ||