JobLog/Command/edit.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 139 CC 15 SCAR 41.7
CC
15
Cov
0%
execute
156.0
validate
12.0
_error_report
6.0
CRAP 64.9
1 package App::JobLog::Command::edit;
2 $App::JobLog::Command::edit::VERSION = '1.042';
3 # ABSTRACT: edit the log
4  
5 use App::JobLog -command;
6 use Modern::Perl;
7 use Class::Autouse qw{
8 App::JobLog::Log
9 App::JobLog::Log::Line
10 Digest::MD5
11 FileHandle
12 };
13 use autouse 'File::Temp' => qw(tempfile);
14 use autouse 'File::Copy' => qw(copy);
15 use autouse 'App::JobLog::Config' => qw(editor log);
16 use autouse 'Getopt::Long::Descriptive' => qw(prog_name);
17 use autouse 'App::JobLog::TimeGrammar' => qw(parse);
18 use autouse 'App::JobLog::Time' => qw(now);
19  
20 sub execute {
21 my ( $self, $opt, $args ) = @_;
22 if ( $opt->close || $opt->validate ) {
23 eval {
24 my $log = App::JobLog::Log->new;
25 if ( $opt->close ) {
26 my $time = join ' ', @$args;
27 my ($s) = parse($time);
28 $self->usage_error(
29 'you may only insert closing times prior to present')
30 unless $s < now;
31 my ( $e, $i ) = $log->find_previous($s);
32 $self->usage_error('log does not contain appropriate event')
33 unless $e;
34 $self->usage_error('no open event at this time')
35 unless $e->is_open;
36 $log->insert( $i + 1,
37 App::JobLog::Log::Line->new( time => $s, done => 1 ) );
38 }
39 if ( $opt->validate ) {
40 my $errors = $log->validate;
41 _error_report($errors);
42 }
43 };
44 $self->usage_error($@) if $@;
45 }
46 elsif ( my $editor = editor ) {
47 if ( my $log = log ) {
48 my ( $fh, $fn ) = tempfile;
49 binmode $fh;
50 copy( $log, $fh );
51 $fh->close;
52 $fh = FileHandle->new($log);
53 my $md5 = Digest::MD5->new;
54 my $md51 = $md5->addfile($fh)->hexdigest;
55 system "$editor $log";
56 $fh = FileHandle->new($log);
57 my $md52 = $md5->reset->addfile($fh)->hexdigest;
58  
59 if ( $md51 ne $md52 ) {
60 $fh = FileHandle->new( "$log.bak", 'w' );
61 copy( $fn, $fh );
62 $fh->close;
63 say "saved backup log in $log.bak";
64 my $errors = App::JobLog::Log->new->validate;
65 _error_report($errors);
66 }
67 else {
68 unlink $fn;
69 }
70 }
71 else {
72 say 'nothing in log to edit';
73 }
74 }
75 else {
76 $self->usage_error('no editor specified') unless $opt->close;
77 }
78 }
79  
80 sub usage_desc {
81 '%c ' . __PACKAGE__->name . ' [--validate] [-c <date and time>]';
82 }
83  
84 sub abstract { 'open a text editor to edit the log' }
85  
86 sub full_description {
87 <<END;
88 Close an open task or open a text editor to edit the log.
89  
90 Closing an open task is the only edit you'll commonly have to make (it's
91 easy to forget to close the last task of the day). Fortunately, it is the easiest
92 edit to perform. You simply type
93  
94 @{[prog_name]} @{[__PACKAGE__->name]} --close yesterday at 8:00 pm
95  
96 for example and @{[prog_name]} will insert the appropriate line if it can do so.
97 If it can't because there is no open task at the time specified, it will emit a warning
98 instead.
99  
100 The date and time parsing is handled by the same code used by the @{[App::JobLog::Command::summary->name]} command,
101 so what works for one works for the other. One generally does not specify hours and such
102 for summaries, but @{[prog_name]} will understand most common natural language time expressions.
103  
104 If you need to do more extensive editing of the log this command will open a text editor
105 for you and confirm the validity of the log after you save, commenting out
106 ill-formed lines and printing a warning. This command requires the you
107 to have set editor configuration parameter to specify a text.
108 The text editor must be invokable like so,
109  
110 <editor> <file to edit>
111
112 That is, you must be able to specify the file to edit as an argument. If the editor
113 requires any additional arguments or options you must provide those via the
114 environment variable.
115 END
116 }
117  
118 sub options {
119 return (
120 [
121 'close|close-task|c' =>
122 'add a "DONE" line to the log at the specified moment'
123 ],
124 [ 'validate|v' => 'check log for errors, commenting out any found' ],
125 );
126 }
127  
128 sub _error_report {
129 my $errors = shift;
130  
131 if ($errors) {
132 say "errors found: $errors";
133 say 'Error messages have been inserted into the log. Please edit.';
134 }
135 else {
136 say 'log is valid';
137 }
138 }
139  
140 sub validate {
141 my ( $self, $opt, $args ) = @_;
142  
143 if ( $opt->close ) {
144 $self->usage_error('no time expression provided') unless @$args;
145 }
146 }
147  
148 1;
149  
150 __END__