| 1 | package App::JobLog::Command::add; | ||
| 2 | $App::JobLog::Command::add::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: log an event | ||
| 4 | |||
| 5 | use App::JobLog -command; | ||
| 6 | use Modern::Perl; | ||
| 7 | use autouse 'Getopt::Long::Descriptive' => qw(prog_name); | ||
| 8 | use autouse 'App::JobLog::Time' => qw(now); | ||
| 9 | use Class::Autouse qw(App::JobLog::Log); | ||
| 10 | |||
| 11 | sub execute { | ||
| 12 | my ( $self, $opt, $args ) = @_; | ||
| 13 | my $tags = $opt->tag; | ||
| 14 | unless ($tags) { | ||
| 15 | $tags = [] if $opt->clear_tags; | ||
| 16 | } | ||
| 17 | my $log = App::JobLog::Log->new; | ||
| 18 | my ($last) = $log->last_event; | ||
| 19 | my $is_ongoing = $last && $last->is_open; | ||
| 20 | $log->append_event( | ||
| 21 | $tags ? ( tags => $tags ) : (), | ||
| 22 | description => [ join ' ', @$args ], | ||
| 23 | time => now | ||
| 24 | ); | ||
| 25 | if ( $is_ongoing && _different_day( $last->start, now ) ) { | ||
| 26 | say 'Event spans midnight. Perhaps you failed to close the last event.'; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _different_day { | ||
| 31 | my ( $d1, $d2 ) = @_; | ||
| 32 | return !( $d1->year == $d2->year | ||
| 33 | && $d1->month == $d2->month | ||
| 34 | && $d1->day == $d2->day ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub usage_desc { '%c ' . __PACKAGE__->name . ' <description of event>' } | ||
| 38 | |||
| 39 | sub abstract { 'log an event' } | ||
| 40 | |||
| 41 | sub full_description { | ||
| 42 | <<END; | ||
| 43 | Log an event. E.g., | ||
| 44 | |||
| 45 | @{[prog_name($0)]} @{[__PACKAGE__->name]} munging the widget | ||
| 46 | |||
| 47 | All arguments that are not parameter values are concatenated as a description | ||
| 48 | of the event. Logging an event simultaneously marks the end of the previous | ||
| 49 | event. Events may be tagged to mark such things as client, grant, or | ||
| 50 | project. | ||
| 51 | END | ||
| 52 | } | ||
| 53 | |||
| 54 | sub options { | ||
| 55 | return ( | ||
| 56 | [ | ||
| 57 | 'tag|t=s@', | ||
| 58 | 'tag the event; multiple tags are acceptable; e.g., -t foo -t bar -t quux', | ||
| 59 | ], | ||
| 60 | [ | ||
| 61 | 'clear-tags|T', | ||
| 62 | 'inherit no tags from preceding event; ' | ||
| 63 | . 'this is equivalent to -t ""; ' | ||
| 64 | . 'this option has no effect if any tag is specified', | ||
| 65 | ], | ||
| 66 | |||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub validate { | ||
| 71 | my ( $self, $opt, $args ) = @_; | ||
| 72 | |||
| 73 | $self->usage_error('no description provided') unless @$args; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ |