| 1 | package App::JobLog::Command::modify; | ||
| 2 | $App::JobLog::Command::modify::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: modify last logged event | ||
| 4 | |||
| 5 | use App::JobLog -command; | ||
| 6 | use Modern::Perl; | ||
| 7 | use Class::Autouse qw(App::JobLog::Log); | ||
| 8 | no if $] >= 5.018, warnings => "experimental::smartmatch"; | ||
| 9 | |||
| 10 | sub execute { | ||
| 11 | my ( $self, $opt, $args ) = @_; | ||
| 12 | |||
| 13 | my $log = App::JobLog::Log->new; | ||
| 14 | my ( $e, $i ) = $log->last_event; | ||
| 15 | $self->usage_error('empty log') unless $e; | ||
| 16 | my $ll = $e->data; | ||
| 17 | if ( $opt->clear_tags ) { | ||
| 18 | $ll->tags = []; | ||
| 19 | } | ||
| 20 | elsif ( $opt->untag ) { | ||
| 21 | my %tags = map { $_ => 1 } @{ $ll->tags }; | ||
| 22 | delete $tags{$_} for @{ $opt->untag }; | ||
| 23 | $ll->tags = [ sort keys %tags ]; | ||
| 24 | } | ||
| 25 | if ( $opt->tag ) { | ||
| 26 | my %tags = map { $_ => 1 } @{ $ll->tags }; | ||
| 27 | $tags{$_} = 1 for @{ $opt->tag }; | ||
| 28 | $ll->tags = [ sort keys %tags ]; | ||
| 29 | } | ||
| 30 | my $description = join ' ', @$args; | ||
| 31 | for ( $opt->desc || '' ) { | ||
| 32 | when ('replace_description') { | ||
| 33 | $ll->description = [$description]; | ||
| 34 | } | ||
| 35 | when ('add_description') { | ||
| 36 | push @{ $ll->description }, $description; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | $log->replace( $i, $ll ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub usage_desc { '%c ' . __PACKAGE__->name . ' %o [<description>]' } | ||
| 43 | |||
| 44 | sub abstract { 'add details to last event' } | ||
| 45 | |||
| 46 | sub options { | ||
| 47 | return ( | ||
| 48 | [ | ||
| 49 | desc => hidden => { | ||
| 50 | one_of => [ | ||
| 51 | [ "add-description|a" => "add some descriptive text" ], | ||
| 52 | [ | ||
| 53 | "replace-description|r" => "replace current description" | ||
| 54 | ], | ||
| 55 | ] | ||
| 56 | } | ||
| 57 | ], | ||
| 58 | [ "tag|t=s@", "add tag; e.g., -t foo -t bar" ], | ||
| 59 | [ "untag|u=s@", "remove tag; e.g., -u foo -u bar" ], | ||
| 60 | [ "clear-tags|c", "remove all tags" ], | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub validate { | ||
| 65 | my ( $self, $opt, $args ) = @_; | ||
| 66 | |||
| 67 | my $has_modification = grep { $_ } @{$opt}{qw(desc tag untag clear_tags)}; | ||
| 68 | $self->usage_error('no modification specified') unless $has_modification; | ||
| 69 | |||
| 70 | if ( $opt->desc ) { | ||
| 71 | $self->usage_error('no description provided') unless @$args; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | __END__ |