File Coverage

blib/lib/App/RunCron/CLI.pm
Criterion Covered Total %
statement 47 47 100.0
branch 9 12 75.0
condition 2 3 66.6
subroutine 11 11 100.0
pod 0 3 0.0
total 69 76 90.7


line stmt bran cond sub pod time code
1             package App::RunCron::CLI;
2 1     1   79964 use strict;
  1         3  
  1         34  
3 1     1   6 use warnings;
  1         2  
  1         25  
4 1     1   6 use utf8;
  1         1  
  1         7  
5              
6 1     1   1110 use Getopt::Long;
  1         11620  
  1         9  
7 1     1   1374 use Pod::Usage;
  1         71062  
  1         171  
8 1     1   1279 use Time::Piece;
  1         12665  
  1         7  
9 1     1   1315 use YAML::Tiny ();
  1         6461  
  1         34  
10              
11 1     1   653 use App::RunCron;
  1         3  
  1         328  
12              
13             sub new {
14 3     3 0 9568 my ($class, @argv) = @_;
15              
16 3         13 local @ARGV = @argv;
17 3         67 my $p = Getopt::Long::Parser->new(
18             config => [qw/posix_default no_ignore_case bundling auto_help/],
19             );
20 3 50       365 $p->getoptions(\my %opt, qw/
21             logfile=s
22             timestamp
23             print
24             reporter=s
25             error_reporter=s
26             common_reporter=s
27             tag|t=s
28             config|c=s
29             /) or pod2usage(1);
30              
31 3         1751 $opt{command} = [@ARGV];
32 3         8 for my $rep (qw/reporter error_reporter/){
33 6 50       19 $opt{$rep} = ucfirst $opt{$rep} if $opt{$rep};
34             }
35 3         17 $class->new_with_options(%opt);
36             }
37              
38             sub new_with_options {
39 3     3 0 11 my ($class, %opt) = @_;
40              
41 3 100       30 if ($opt{logfile}) {
42 2         13 my $now = localtime;
43 2         263 $opt{logfile} = $now->strftime($opt{logfile});
44             }
45              
46 3 100 66     204 if (!$opt{config} && -e 'runcron.yml') {
47 2         6 $opt{config} = 'runcron.yml';
48             }
49 3 50       9 if ($opt{config}) {
50 3         8 my $config_file = $opt{config};
51 3         4 my $conf = eval { YAML::Tiny::LoadFile($config_file) };
  3         14  
52 3 100       6615 if ($@) {
53 1         98 warn "Bad config: $config_file: $@";
54             }
55             else {
56 2         20 %opt = (
57             %$conf,
58             %opt,
59             );
60             }
61             }
62              
63             bless {
64 3         31 runner => App::RunCron->new(%opt),
65             }, $class;
66             }
67              
68             sub run {
69 1     1 0 3047 shift->{runner}->run
70             }
71              
72             1;