| 1 | # ABSTRACT: Export the ref-backed karr board as YAML | ||
| 2 | |||
| 3 | package App::karr::Cmd::Backup; | ||
| 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 backup [--output PATH]', | |
| 9 | 1 | ); | |
| 10 | 1 | ▶ | use Path::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use App::karr::Encoding qw( yaml_dump ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use App::karr::Error qw( user_error clean_error command_hint ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use App::karr::Role::BoardDiscovery; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use App::karr::Role::CliArgs; | ||
| 15 | use App::karr::Role::SyncLifecycle; | ||
| 16 | |||
| 17 | with 'App::karr::Role::BoardDiscovery'; | ||
| 18 | with 'App::karr::Role::SyncLifecycle'; | ||
| 19 | with 'App::karr::Role::CliArgs'; | ||
| 20 | |||
| 21 | |||
| 22 | option output => ( | ||
| 23 | is => 'ro', | ||
| 24 | format => 's', | ||
| 25 | doc => 'Write YAML snapshot to a file instead of stdout', | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub execute { | ||
| 29 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 30 | |||
| 31 | # store honours --dir (both call forms) and dies loudly if the target is | ||
| 32 | # not a Git repository, instead of hardcoding the current directory. | ||
| 33 | my $store = $self->store; | ||
| 34 | |||
| 35 | # Backup is read-only: take the retrying pull half of the sync lifecycle, | ||
| 36 | # then mark the guard done so this read path never pushes on exit or on die. | ||
| 37 | my $guard = $self->sync_before; | ||
| 38 | $guard->done; | ||
| 39 | |||
| 40 | # The one spelling of this sentence, shared with require_board and with | ||
| 41 | # destroy/materialize/repair: the way out is a command on its own last line | ||
| 42 | # (ticket k263). | ||
| 43 | die "No karr board found:\n" . command_hint('init') . "\n" | ||
| 44 | unless $store->has_board_refs; | ||
| 45 | |||
| 46 | # Characters all the way: spew_utf8 encodes for the --output file, and the | ||
| 47 | # CLI's :encoding(UTF-8) layer encodes for stdout. A Dump() here would emit | ||
| 48 | # octets and both paths would encode them a second time (ticket #53). | ||
| 49 | my $yaml = yaml_dump( $store->snapshot ); | ||
| 50 | |||
| 51 | if ( $self->output ) { | ||
| 52 | my $file = path( $self->output ); | ||
| 53 | # An --output karr cannot write is the user's path, not karr's: Path::Tiny | ||
| 54 | # would otherwise report this file and line at them (#77). | ||
| 55 | eval { $file->parent->mkpath; $file->spew_utf8($yaml); 1 } | ||
| 56 | or user_error( "Could not write $file: ", clean_error($@) ); | ||
| 57 | print STDERR "Wrote backup to $file\n"; | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | print $yaml; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ | ||