File Coverage

blib/lib/Riji/CLI/NewEntry.pm
Criterion Covered Total %
statement 39 40 97.5
branch 5 10 50.0
condition 4 9 44.4
subroutine 8 8 100.0
pod 0 1 0.0
total 56 68 82.3


line stmt bran cond sub pod time code
1             package Riji::CLI::NewEntry;
2 1     1   218734 use feature ':5.10';
  1         6  
  1         161  
3 1     1   7 use strict;
  1         3  
  1         20  
4 1     1   5 use warnings;
  1         2  
  1         40  
5              
6 1     1   7 use IPC::Cmd ();
  1         9  
  1         30  
7 1     1   6 use Path::Tiny;
  1         2  
  1         61  
8 1     1   581 use Time::Piece;
  1         10795  
  1         6  
9              
10 1     1   595 use Riji;
  1         4  
  1         369  
11              
12             sub run {
13 1     1 0 2965 my ($class, @argv) = @_;
14 1         15 my $subtitle = shift @argv;
15 1 50 33     28 die "subtitle: $subtitle is not valid\n" if $subtitle && $subtitle =~ /[^-_a-zA-Z0-9.]/;
16              
17 1         76 my $app = Riji->new;
18              
19 1         3604 my $now = localtime;
20 1         207 my $dir = $app->model('Blog')->entry_path;
21 1 50       214 $dir->mkpath unless $dir->exists;
22 1         96 my $date_str = $now->strftime('%Y-%m-%d');
23 1         345 my $file_format = "$dir/$date_str-%s.md";
24 1         29 my $file;
25 1 50       14 if ($subtitle) {
26 0         0 $file = path(sprintf $file_format, $subtitle);
27             }
28             else {
29 1         10 my $seq = 1;
30 1   66     27 $file = path(sprintf $file_format, sprintf('%02d', $seq++)) while !$file || -e $file;
31             }
32              
33 1 50       138 $file->spew(<<'...') unless -e $file;
34             tags: blah
35             ---
36             # title
37             ...
38              
39 1         1195 my $editor = $ENV{EDITOR};
40 1   33     15 $editor = $editor && IPC::Cmd::can_run($editor);
41              
42 1 50       18 exec $editor, "$file" if $editor;
43 1         8 say "$file is created. Edit it!";
44             }
45              
46             1;