lib/CLI/Driver/CommonRole.pm

filter: Statementstmt 46.4% 143 / 308 Branchbran 2.9% 5 / 170 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 78.8% 41 / 52 Podpod n/a totaltotal 31.0% 189 / 608 CC 104 SCAR 43.5
CC
104
Cov
28%
system
214.5
_verbose
174.5
fatal
88.9
CRAP 77.5
« lib/CLI/Driver/Action.pm ↑ Summary lib/CLI/Driver/ArgParserRole.pm »
1 package CLI::Driver::CommonRole;
2  
3 17 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Capture::Tiny 'capture';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Time::localtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #########################################################################################
13  
14 17 has verbosity => (
15 is => 'rw',
16 isa => 'Num',
17 lazy => 1,
18 builder => '_build_verbosity',
19 );
20  
21 #########################################################################################
22  
23 17 ▶method chdir (Str $dir) {
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine chdir: not called CC=7 SCAR=33.5
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 1 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::Str($dir) or _failed_check(9, "Str", $dir, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
24  
25 0 $self->verbose3("chdir($dir)");
26 0 ▶ chdir($dir) or confess "failed to chdir to $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::chdir $dir or confess("failed to chdir to ${dir}: $!") 0 0 0
27 }
28  
29 17 ▶method die (Str $str) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine die: called CC=6 SCAR=18.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 1 0 1
if @_ != 1 0 1 1
$#_ >= 0 ? : 1 0 1
if ($#_ >= 0) 1 0 1
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 1 1
30  
31 1 chomp $str;
32 1 die "[ERROR] $str\n";
33 }
34  
35 17 ▶method fatal (Str $str, Num $frames? = 0) {
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine fatal: not called CC=12 SCAR=44.9
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ < 1 0 0 0
if @_ > 2 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames) or _failed_check(12, "Num", $frames, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/6
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
!ll&&!rl&&r
000
defined $frames and not ref $frames
!ll&&!rl&&r
000
MC/DC0%0/3
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
defined $frames not ref $frames &Scalar::Util::looks_like_number($frames)
Truth table0%0/4
A and not(B) and C
A= defined $framesB= not ref $framesC= &Scalar::Util::looks_like_number($frames)
ABCresult
0XX0
10X0
1100
1111
36  
37 0 chomp $str;
38  
39 0 my $caller = '';
40 0 ▶ if ($frames) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frames) 0 0 0
41 0 $caller = sprintf " %s", ( caller($frames) )[3];
42 }
43  
44 0 printf STDERR "[FATAL%s] $str\n", $caller;
45 0 exit 1;
46 }
47  
48 17 ▶method localdatetime ($time = time) {
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine localdatetime: not called CC=4 SCAR=24.1
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ > 1 0 0 0
$#_ >= 0 ? : 0 0 0
49  
50 0 my $l = localtime($time);
51  
52 0 my $str = sprintf(
53 '%04d-%02d-%02d %02d:%02d:%02d',
54 $l->year + 1900,
55 $l->mon + 1,
56 $l->mday, $l->hour, $l->min, $l->sec
57 );
58  
59 0 return $str;
60 }
61  
62 method system (Str :$cmd,
63 Bool :$confess_on_err = 1,
64 17 ▶ Bool :$capture = 0) {
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine system: not called CC=20 SCAR=53.7
Branch: 12 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 0 0
if ($slice_length % 2 != 0) 0 0 0
if ($i <= $#_) 0 0 0
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 0 0
exists $_{'cmd'} ? : 0 0 0
if (exists $_{"cmd"}) 0 0 0
Type::Tiny::XS::Str($cmd) or _failed_check(9, "Str", $cmd, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
exists $_{'confess_on_err'} ? : 0 0 0
Type::Tiny::XS::Bool($confess_on_err) or _failed_check(3, "Bool", $confess_on_err, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
exists $_{'capture'} ? : 0 0 0
Type::Tiny::XS::Bool($capture) or _failed_check(3, "Bool", $capture, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
65  
66 0 $self->verbose($cmd);
67  
68 0 ▶ if ($capture) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capture) { } 0 0 0
69 my ( $stdout, $stderr, $exit ) = capture {
70 0 ▶ system($cmd);
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 };
72  
73 0 ▶ if ( $exit and $confess_on_err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit and $confess_on_err) 0 0 0
Condition0%0/3
$exit and $confess_on_err
!ll&&!rl&&r
000
MC/DC0%0/2
$exit and $confess_on_err
$exit $confess_on_err
Truth table0%0/3
A and B
A= $exitB= $confess_on_err
ABresult
0X0
100
111
74 0 confess $stderr;
75 }
76  
77 0 return ( $stdout, $stderr, $exit );
78 }
79 else {
80 0 system($cmd);
81 0 my $exit = $? >> 8;
82  
83 0 ▶ if ( $exit and $confess_on_err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit and $confess_on_err) 0 0 0
Condition0%0/3
$exit and $confess_on_err
!ll&&!rl&&r
000
MC/DC0%0/2
$exit and $confess_on_err
$exit $confess_on_err
Truth table0%0/3
A and B
A= $exitB= $confess_on_err
ABresult
0X0
100
111
84 0 confess "last command failed with exit code $exit";
85 }
86  
87 0 return $exit;
88 }
89 }
90  
91 17 ▶method verbose (Str $str, Num $frames? = 1) {
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine verbose: not called CC=11 SCAR=42.3
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ < 1 0 0 0
if @_ > 2 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames) or _failed_check(12, "Num", $frames, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/6
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
!ll&&!rl&&r
000
defined $frames and not ref $frames
!ll&&!rl&&r
000
MC/DC0%0/3
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
defined $frames not ref $frames &Scalar::Util::looks_like_number($frames)
Truth table0%0/4
A and not(B) and C
A= defined $framesB= not ref $framesC= &Scalar::Util::looks_like_number($frames)
ABCresult
0XX0
10X0
1100
1111
92  
93 0 $self->_verbose( 1, $str, $frames + 1 );
94 }
95  
96 17 ▶method verbose2 (Str $str, Num $frames? = 1) {
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine verbose2: not called CC=11 SCAR=42.3
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ < 1 0 0 0
if @_ > 2 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames) or _failed_check(12, "Num", $frames, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/6
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
!ll&&!rl&&r
000
defined $frames and not ref $frames
!ll&&!rl&&r
000
MC/DC0%0/3
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
defined $frames not ref $frames &Scalar::Util::looks_like_number($frames)
Truth table0%0/4
A and not(B) and C
A= defined $framesB= not ref $framesC= &Scalar::Util::looks_like_number($frames)
ABCresult
0XX0
10X0
1100
1111
97  
98 0 $self->_verbose( 2, $str, $frames + 1 );
99 }
100  
101 17 ▶method verbose3 (Str $str, Num $frames? = 1) {
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine verbose3: not called CC=11 SCAR=42.3
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ < 1 0 0 0
if @_ > 2 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames) or _failed_check(12, "Num", $frames, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/6
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
!ll&&!rl&&r
000
defined $frames and not ref $frames
!ll&&!rl&&r
000
MC/DC0%0/3
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
defined $frames not ref $frames &Scalar::Util::looks_like_number($frames)
Truth table0%0/4
A and not(B) and C
A= defined $framesB= not ref $framesC= &Scalar::Util::looks_like_number($frames)
ABCresult
0XX0
10X0
1100
1111
102  
103 0 $self->_verbose( 3, $str, $frames + 1 );
104 }
105  
106 17 ▶method warn (Str $str, Num $frames? = 1) {
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine BEGIN: called CC=1 SCAR=3.9
Subroutine warn: not called CC=12 SCAR=44.8
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ < 1 0 0 0
if @_ > 2 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames) or _failed_check(12, "Num", $frames, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/6
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
!ll&&!rl&&r
000
defined $frames and not ref $frames
!ll&&!rl&&r
000
MC/DC0%0/3
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
defined $frames not ref $frames &Scalar::Util::looks_like_number($frames)
Truth table0%0/4
A and not(B) and C
A= defined $framesB= not ref $framesC= &Scalar::Util::looks_like_number($frames)
ABCresult
0XX0
10X0
1100
1111
107  
108 0 chomp $str;
109  
110 0 my $caller = '';
111 0 ▶ if ($frames) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frames) 0 0 0
112 0 $caller = sprintf " %s", ( caller($frames) )[3];
113 }
114  
115 0 printf STDERR "[WARN%s] $str\n", $caller;
116 }
117  
118 ######################################################################
119  
120 17 ▶method _build_verbosity {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine _build_verbosity: not called CC=3 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
121  
122 0 my $level = 0;
123 0 ▶ $level = $ENV{VERBOSE} if $ENV{VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"VERBOSE"} 0 0 0
124  
125 0 return $level;
126 }
127  
128 17 ▶method _verbose (Num $level, Str $str, Num $frames) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _verbose: not called CC=17 SCAR=51.6
Branch: 11 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 3 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
defined $level and not ref $level and &Scalar::Util::looks_like_number($level) or _failed_check(12, "Num", $level, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
if ($#_ >= 1) 0 0 0
Type::Tiny::XS::Str($str) or _failed_check(9, "Str", $str, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 2 ? : 0 0 0
if ($#_ >= 2) 0 0 0
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames) or _failed_check(12, "Num", $frames, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition0%0/12
defined $level and not ref $level and &Scalar::Util::looks_like_number($level)
!ll&&!rl&&r
000
defined $level and not ref $level
!ll&&!rl&&r
000
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
!ll&&!rl&&r
000
defined $frames and not ref $frames
!ll&&!rl&&r
000
MC/DC0%0/3
defined $level and not ref $level and &Scalar::Util::looks_like_number($level)
defined $level not ref $level &Scalar::Util::looks_like_number($level)
MC/DC0%0/3
defined $frames and not ref $frames and &Scalar::Util::looks_like_number($frames)
defined $frames not ref $frames &Scalar::Util::looks_like_number($frames)
Truth table0%0/4
A and not(B) and C
A= defined $levelB= not ref $levelC= &Scalar::Util::looks_like_number($level)
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and C
A= defined $framesB= not ref $framesC= &Scalar::Util::looks_like_number($frames)
ABCresult
0XX0
10X0
1100
1111
129  
130 0 ▶ if ( $self->verbosity >= $level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbosity >= $level) 0 0 0
131 0 chomp $str;
132 0 my $caller = ( caller($frames) )[3];
133 0 printf STDERR "[VERBOSE-%d] ($caller) $str\n", $level;
134 }
135 }
136  
137 17 1;
« lib/CLI/Driver/Action.pm ↑ Summary lib/CLI/Driver/ArgParserRole.pm »