| 1 | # ABSTRACT: Restore the ref-backed karr board from YAML | ||
| 2 | |||
| 3 | package App::karr::Cmd::Restore; | ||
| 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 restore --yes [--input PATH]', | |
| 9 | 1 | ); | |
| 10 | 1 | ▶ | use Path::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use App::karr::Encoding qw( yaml_load from_octets ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use App::karr::Error qw( user_error clean_error ); |
|
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 input => ( | ||
| 23 | is => 'ro', | ||
| 24 | format => 's', | ||
| 25 | doc => 'Read YAML snapshot from a file instead of stdin', | ||
| 26 | ); | ||
| 27 | |||
| 28 | option yes => ( | ||
| 29 | is => 'ro', | ||
| 30 | doc => 'Acknowledge destructive replacement of refs/karr/*', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub execute { | ||
| 34 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 35 | |||
| 36 | die "Ref restore is destructive and replaces all refs/karr/*. Re-run with --yes.\n" | ||
| 37 | unless $self->yes; | ||
| 38 | |||
| 39 | # store honours --dir (both call forms) and dies loudly if the target is | ||
| 40 | # not a Git repository, instead of hardcoding the current directory. | ||
| 41 | my $store = $self->store; | ||
| 42 | |||
| 43 | # Restore rewrites refs/karr/*: run the full sync lifecycle so the pull and | ||
| 44 | # the mirror-back push both retry, and the guard insures the push on a crash. | ||
| 45 | $self->sync_before; | ||
| 46 | |||
| 47 | my $payload = $self->_load_payload; | ||
| 48 | my $snapshot = eval { yaml_load($payload) }; | ||
| 49 | die "Backup payload is not valid YAML\n" if $@; | ||
| 50 | die "Backup payload must be a hash document\n" unless ref $snapshot eq 'HASH'; | ||
| 51 | die "Backup payload version 1 is required\n" | ||
| 52 | unless ($snapshot->{version} // '') eq '1'; | ||
| 53 | die "Backup payload must contain a refs hash\n" | ||
| 54 | unless ref $snapshot->{refs} eq 'HASH'; | ||
| 55 | |||
| 56 | $store->restore_snapshot($snapshot); | ||
| 57 | |||
| 58 | $self->sync_after; | ||
| 59 | |||
| 60 | print STDERR "Restored refs/karr/* from snapshot\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _load_payload { | ||
| 64 | my ($self) = @_; | ||
| 65 | |||
| 66 | if ( $self->input ) { | ||
| 67 | # An unreadable --input is the user's path, not karr's: Path::Tiny's own | ||
| 68 | # error would hand them this file and line instead (#77). | ||
| 69 | my $content = eval { path( $self->input )->slurp_utf8 }; | ||
| 70 | defined $content | ||
| 71 | or user_error( "Could not read ", $self->input, ": ", clean_error($@) ); | ||
| 72 | return $content; | ||
| 73 | } | ||
| 74 | |||
| 75 | # STDIN is the one input edge App::karr::Encoding leaves without a PerlIO | ||
| 76 | # layer, precisely so this decode is explicit and happens exactly once. | ||
| 77 | binmode STDIN, ':raw'; | ||
| 78 | my $content = do { local $/; <STDIN> }; | ||
| 79 | die "No backup payload received on stdin\n" | ||
| 80 | unless defined $content && length $content; | ||
| 81 | return from_octets($content); | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; | ||
| 85 | |||
| 86 | __END__ | ||