Csistck/Oper.pm

filter: Statementstmt 93.4% 57 / 61 Branchbran 58.3% 7 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 4 totaltotal 84.2% 80 / 95 CC 8 SCAR 8.0
CC
8
Cov
87%
set_mode_by_cli
4.1
__ANON__
3.0
usage
2.9
CRAP 2.2
« Csistck/Test/Pkg.pm ↑ Summary Csistck/Config.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 # String options
36 17 our $Roles = [];
37  
38 # Set up colored output
39 17 $Term::ANSIColor::EACHLINE = "\n";
40 17 my $Colors = {
41 'debug' => 'black',
42 'info' => 'yellow',
43 'error' => 'red'
44 };
45  
46 # Dynamic setup of reporting functions
47 17 for my $level (keys %{$Modes}) {
48  
49 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 51 ▶ *{"Csistck\::Oper\::$level"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
52 73 my $func = shift;
53  
54 # Maybe this isn't the best way. If func is passed and
55 # is code, execute. If func is passed and is a scalar,
56 # debug it?!
57 73 ▶ if (defined $func and $Modes->{$level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func and $Csistck::Oper::Modes->{$level}) { } 8 65 73
Condition66%2/3
defined $func and $Csistck::Oper::Modes->{$level}
!ll&&!rl&&r
0658
MC/DC50%1/2
defined $func and $Csistck::Oper::Modes->{$level}
defined $func $Csistck::Oper::Modes->{$level}
Truth table66%2/3
A and B
A= defined $funcB= $Csistck::Oper::Modes->{$level}
ABresult
0X0
100
111
58 8 given (ref $func) {
59 0 when ("CODE") { return &$func; };
60 8 when ("") { return log_message($level, $func) };
61 }
62 }
63 else {
64 # Return mode
65 65 return $Modes->{$level};
66 }
67 51 };
68 }
69  
70 # Repair mode accessor
71 1 ▶sub repair {
Subroutine repair: called CC=1 SCAR=0.0
Pod undocumented
72 1 return $Options->{repair};
73 }
74  
75 # Set up mode via command line options
76 5 ▶sub set_mode_by_cli {
Subroutine set_mode_by_cli: called CC=4 SCAR=14.2
Pod undocumented
77  
78 # Map options (as getopt negatable option) to $Options
79 # TODO more options
80 5 my %opts = map { +"$_!" => \$Options->{$_} } keys %{$Options};
81 5 my $result = GetOptions(%{{
82 5 'role=s' => \@{$Roles},
83 %opts
84 }});
85  
86 # Set reporting mode based on options
87 5 ▶ $Modes->{info} = ($Options->{verbose}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Csistck::Oper::Options->{'verbose'} ? : 0 5 5
88 5 ▶ $Modes->{debug} = ($Options->{debug}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Csistck::Oper::Options->{'debug'} ? : 0 5 5
89 5 ▶ $Modes->{error} = ($Options->{quiet}) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$Csistck::Oper::Options->{'quiet'} ? : 0 5 5
90 }
91  
92 # Display usage
93 5 ▶sub usage {
Subroutine usage: called CC=2 SCAR=10.5
Pod undocumented
94 5 ▶ return undef unless ($Options->{help});
Branch: 1 branch
BranchTrueFalseTotal
unless $Csistck::Oper::Options->{"help"} 5 0 5
95
96 0 print <<EOF;
97 Usage: $0 [OPTION]...
98  
99 Arguments:
100
101 --help Display usage
102 --verbose Verbose output
103 --debug Debug output
104 --repair Run repair operations
105 --role=ROLE Force check on weak role ROLE
106 --quiet Less output
107 --nocolor Turn off colored output
108  
109 EOF
110 0 return 1;
111 }
112  
113 8 ▶sub log_message {
Subroutine log_message: called CC=2 SCAR=7.2
Pod undocumented
114 8 my $level = shift;
115 8 my $msg = shift;
116
117 8 my $log_line = sprintf("[%s]\ %s\n", uc($level), $msg);
118  
119 8 ▶ if ($Options->{color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Csistck::Oper::Options->{'color'}) { } 8 0 8
120 8 print(colored($log_line, $Colors->{$level}));
121 }
122 else {
123 0 print($log_line);
124 }
125 }
126  
127 17 1;
« Csistck/Test/Pkg.pm ↑ Summary Csistck/Config.pm »