Csistck/Oper.pm

filter: Statementstmt 77.5% 45 / 58 Branchbran 41.6% 5 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 68.4% 63 / 92 SCAR 22.8
CC
8
Cov
70%
log_message
6.0
__ANON__
5.1
set_mode_by_cli
4.2
CRAP 9.8
« Csistck/Test.pm ↑ Summary Csistck/Test/Script.pm »
1 package Csistck::Oper;
2  
3 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 our @EXPORT_OK = qw/
10 debug error info
11 repair
12 /;
13  
14 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Logging levels, default boolean
19 17 our $Modes = {
20 debug => 0,
21 error => 1,
22 info => 0
23 };
24  
25 # Boolean options
26 17 our $Options = {
27 verbose => 0,
28 debug => 0,
29 quiet => 0,
30 repair => 0,
31 help => 0,
32 color => 1
33 };
34  
35 # Set up colored output
36 17 $Term::ANSIColor::EACHLINE = "\n";
37 17 my $Colors = {
38 'debug' => 'black',
39 'info' => 'yellow',
40 'error' => 'red'
41 };
42  
43 # Dynamic setup of reporting functions
44 17 for my $level (keys %{$Modes}) {
45  
46 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 51 *{"Csistck\::Oper\::$level"} = sub {
49 73 ▶ my $func = shift;
Subroutine __ANON__: called CC=3 SCAR=16.3
50  
51 # Maybe this isn't the best way. If func is passed and
52 # is code, execute. If func is passed and is a scalar,
53 # debug it?!
54 73 ▶ if (defined $func and $Modes->{$level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func and $Csistck::Oper::Modes->{$level}) { } 0 73 73
Condition33%1/3
defined $func and $Csistck::Oper::Modes->{$level}
!ll&&!rl&&r
0730
MC/DC0%0/2
defined $func and $Csistck::Oper::Modes->{$level}
defined $func $Csistck::Oper::Modes->{$level}
Truth table33%1/3
A and B
A= defined $funcB= $Csistck::Oper::Modes->{$level}
ABresult
0X0
100
111
55 0 given (ref $func) {
56 0 when ("CODE") { return &$func; };
57 0 when ("") { return log_message($level, $func) };
58 }
59 }
60 else {
61 # Return mode
62 73 return $Modes->{$level};
63 }
64 51 };
65 }
66  
67 # Repair mode accessor
68 sub repair {
69 1 ▶ return $Options->{repair};
Subroutine repair: called CC=1 SCAR=0.0
Pod undocumented
70 }
71  
72 # Set up mode via command line options
73 sub set_mode_by_cli {
74  
75 # Map options (as getopt negatable option) to $Options
76 # TODO more options
77 5 ▶ my %opts = map { +"$_!" => \$Options->{$_} } keys %{$Options};
Subroutine set_mode_by_cli: called CC=4 SCAR=14.3
Pod undocumented
78 5 my $result = GetOptions(%opts);
79  
80 # Set reporting mode based on options
81 5 ▶ $Modes->{info} = ($Options->{verbose}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Csistck::Oper::Options->{'verbose'} ? : 0 5 5
82 5 ▶ $Modes->{debug} = ($Options->{debug}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Csistck::Oper::Options->{'debug'} ? : 0 5 5
83 5 ▶ $Modes->{error} = ($Options->{quiet}) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$Csistck::Oper::Options->{'quiet'} ? : 0 5 5
84 }
85  
86 # Display usage
87 sub usage {
88 5 ▶ return undef unless ($Options->{help});
Subroutine usage: called CC=2 SCAR=10.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $Csistck::Oper::Options->{"help"} 5 0 5
89
90 0 print <<EOF;
91 Usage: $0 [OPTION]...
92  
93 Arguments:
94
95 --help Display usage
96 --verbose Verbose output
97 --debug Debug output
98 --repair Run repair operations
99 --quiet Less output
100 --nocolor Turn off colored output
101  
102 EOF
103 0 return 1;
104 }
105  
106 sub log_message {
107 0 ▶ my $level = shift;
Subroutine log_message: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my $msg = shift;
109
110 0 my $log_line = sprintf("[%s]\ %s\n", uc($level), $msg);
111  
112 0 ▶ if ($Options->{color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Csistck::Oper::Options->{'color'}) { } 0 0 0
113 0 print(colored($log_line, $Colors->{$level}));
114 }
115 else {
116 0 print($log_line);
117 }
118 }
119  
120 17 1;
« Csistck/Test.pm ↑ Summary Csistck/Test/Script.pm »