| 1 | # ABSTRACT: Import a tasks/ file view back into the ref-backed board | ||
| 2 | |||
| 3 | package App::karr::Cmd::Import; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo; | ||
| 6 | use MooX::Cmd; | ||
| 7 | use MooX::Options ( | ||
| 8 | usage_string => 'USAGE: karr import --yes [--dir PATH] [--json]', | ||
| 9 | ); | ||
| 10 | use App::karr::Role::BoardAccess; | ||
| 11 | use App::karr::Role::Output; | ||
| 12 | use App::karr::Error qw( command_hint ); | ||
| 13 | |||
| 14 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output'; | ||
| 15 | |||
| 16 | |||
| 17 | option yes => ( | ||
| 18 | is => 'ro', | ||
| 19 | doc => 'Acknowledge destructive replacement of refs from the file view', | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub execute { | ||
| 23 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 24 | |||
| 25 | $self->check_positional_args($args_ref, 0); | ||
| 26 | |||
| 27 | # "Re-run with --yes" left the caller to reassemble the command line; the line | ||
| 28 | # itself costs nothing and survives a `tail -n` (ticket k263). | ||
| 29 | die "Importing the file view replaces refs/karr/tasks/* and config from the tasks/ directory:\n" | ||
| 30 | . command_hint( 'import', '--yes' ) . "\n" | ||
| 31 | unless $self->yes; | ||
| 32 | |||
| 33 | # Guard against wiping every task ref when there is no view to import: a | ||
| 34 | # missing tasks/ directory would make serialize_from delete all task refs. | ||
| 35 | my $store = $self->store; | ||
| 36 | my $board_dir = $self->git_root; | ||
| 37 | my $tasks_dir = $board_dir->child('tasks'); | ||
| 38 | die "No materialized task view found at $board_dir (no tasks/ directory).\n" | ||
| 39 | . "Place a tasks/ directory there before importing, or write the view first:\n" | ||
| 40 | . command_hint( 'materialize' ) . "\n" | ||
| 41 | unless $tasks_dir->exists; | ||
| 42 | |||
| 43 | # Ticket #50: the directory existing is not the same as there being a view. | ||
| 44 | # serialize_from prunes every ref the view does not mention, so an empty | ||
| 45 | # tasks/ imported zero tasks, deleted the whole board and reported success. | ||
| 46 | # Empty input means "nothing to import", never "delete everything". | ||
| 47 | my @cards = $tasks_dir->children(qr/\.md$/); | ||
| 48 | # Two suggestions, because there are two intents behind an empty view and no | ||
| 49 | # way to tell them apart from here: refresh what the refs actually hold, or | ||
| 50 | # take cards off the board on purpose. Both go last, in the order a caller | ||
| 51 | # who did not mean to delete anything would want them. | ||
| 52 | die "No task cards found in $tasks_dir -- the file view is empty.\n" | ||
| 53 | . "Importing it would delete every task on the board. Refresh the view, or\n" | ||
| 54 | . "remove tasks deliberately:\n" | ||
| 55 | . command_hint( 'materialize' ) . "\n" | ||
| 56 | . command_hint( 'delete', 'ID' ) . "\n" | ||
| 57 | unless @cards; | ||
| 58 | |||
| 59 | # Writing command: pull before, push after, with SyncGuard insurance. | ||
| 60 | $self->sync_before; | ||
| 61 | $store->serialize_from( $board_dir->stringify ); | ||
| 62 | $self->sync_after; | ||
| 63 | |||
| 64 | my @tasks = $store->load_tasks; | ||
| 65 | |||
| 66 | # The imported set is a task collection (like `list`), so --json always emits | ||
| 67 | # an array -- never a bare object for a one-task board. | ||
| 68 | return $self->print_json([ map { $_->to_json_hash } @tasks ]) if $self->json; | ||
| 69 | |||
| 70 | printf STDERR "Imported %d task(s) from %s into refs/karr/*\n", scalar @tasks, $board_dir; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ |