blib/lib/App/karr/Cmd/SetRefs.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/App/karr/Cmd/Destroy.pm ↑ Summary blib/lib/App/karr/CrossBoard.pm »
1 # ABSTRACT: Store helper payloads in a Git ref
2  
3 package App::karr::Cmd::SetRefs;
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 set-refs REF CONTENT... (CONTENT omitted: read stdin)',
9 1 );
10 1 ▶use App::karr::Encoding qw( from_octets );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use App::karr::Git;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use App::karr::Role::CliArgs;
13 use App::karr::Role::ExitCodes;
14  
15 # Unknown option / bad option value exits 2, not 1 (ADR 0002 exit-code
16 # contract). This board-less command has no BoardDiscovery to inherit it from.
17 with 'App::karr::Role::CliArgs', 'App::karr::Role::ExitCodes';
18  
19 # Declared locally rather than inherited from App::karr::Role::BoardDiscovery:
20 # this command deliberately has no board, it only needs the discovery seed to
21 # find the repository the helper ref is written into. Both documented
22 # placements now work (ticket #71): `karr set-refs REF TEXT --dir PATH` binds
23 # this option, while `karr --dir PATH set-refs REF TEXT` leaves --dir on the
24 # root and is adopted from the MooX::Cmd command_chain in execute(). Declaring
25 # it with format=s is also what keeps `--dir PATH` out of the joined payload.
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, @content_parts) = $self->positional_args($args_ref);
37 die "Usage: karr set-refs REF CONTENT...\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 # for_write: a helper ref may be read from every namespace it may not be
54 # written to. refs/karr-foundation/chain/ and .../log/ are karr-foundation's
55 # own structured state, and this command writes last-writer-wins with its
56 # arguments joined by a space, which is not how a chain step is updated.
57 my $ref = $git->validate_helper_ref( $ref_input, for_write => 1 );
58  
59 # Arguments join with a space -- one line, which is what a hint or a status
60 # word is. A document is not that shape, and handing one over the obvious way
61 # (a heredoc, an unquoted paste) used to collapse every newline into a space
62 # and store the result without a word (#195). Reading stdin when there is
63 # nothing to join is the addition that costs no existing caller anything:
64 # that argv shape was a usage error before, so no invocation that worked
65 # changes meaning -- whereas joining with newlines instead would silently
66 # rewrite the payload of every multi-word call, including the one this
67 # command's own SYNOPSIS, the README and the packaged skill all teach.
68 my $content = @content_parts
69 ? join( ' ', @content_parts )
70 : $self->_payload_from_stdin;
71  
72 $git->write_ref($ref, $content) or die "Failed to write $ref\n";
73 $git->push_ref($ref) or die "Failed to push $ref\n";
74  
75 print STDERR "Stored $ref\n";
76 }
77  
78 # The payload as it arrives on stdin, as characters.
79 #
80 # STDIN is the one input edge App::karr::Encoding leaves without a PerlIO layer
81 # (App::karr::Cmd::Restore reads it the same way), so the decode is explicit and
82 # happens exactly once.
83 sub _payload_from_stdin {
84 my ($self) = @_;
85  
86 # A terminal has nothing queued and would just sit there with no prompt, so
87 # the shape that was a usage error before stays one instead of becoming a
88 # hang.
89 die "Usage: karr set-refs REF CONTENT...\n" if -t STDIN;
90  
91 binmode STDIN, ':raw';
92 my $content = do { local $/; <STDIN> };
93  
94 # An empty stdin is not an empty payload: `karr set-refs REF < /dev/null`, or
95 # a generator upstream that produced nothing, is a mistake, and storing '' for
96 # it would report success. The deliberate way to store an empty payload is
97 # still `karr set-refs REF ""`. A runtime failure (exit 1) rather than a usage
98 # error, on the same reading as App::karr::Cmd::Restore's empty stdin: the
99 # invocation was right, what arrived on the pipe was not.
100 die "No payload on stdin. Pass CONTENT as arguments or pipe a payload in.\n"
101 unless defined $content && length $content;
102  
103 return from_octets($content);
104 }
105  
106 1;
107  
108 __END__
« blib/lib/App/karr/Cmd/Destroy.pm ↑ Summary blib/lib/App/karr/CrossBoard.pm »