Simple.pm

filter: Statementstmt 91.5% 54 / 59 Branchbran 56.2% 9 / 16 Conditioncond 55.5% 10 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 12 / 12 Podpod 40.0% 2 / 5 totaltotal 74.7% 89 / 119 CC 19 SCAR 20.2
CC
19
Cov
78%
new
15.1
run_command_log
7.3
run_command_view
3.0
CRAP 7.5
« Simple/UDP.pm ↑ Summary Simple/File.pm »
1 package App::Sysadmin::Log::Simple;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: application class for managing a simple sysadmin log
6 5 our $VERSION = '0.009'; # VERSION
7  
8 5 ▶use autodie qw(:file :filesys);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 use Module::Pluggable
12 5 ▶ search_path => [__PACKAGE__],
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 instantiate => 'new';
14  
15  
16 4 ▶sub new {
Subroutine new: called CC=11 SCAR=27.1
Pod documented
17 4 my $class = shift;
18 4 my %opts = @_;
19 4 my $today = DateTime->now;
20 4 ▶ if ($opts{date}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"date"}) 3 1 4
21 3 my ($in_year, $in_month, $in_day) = split(m{/}, $opts{date});
22 3 ▶ my $in_date = DateTime->new(
Branch: 1 branch
BranchTrueFalseTotal
my $in_date = "DateTime"->new("year", $in_year, "month", $in_month, "day", $in_day) or croak("Couldn't understand your date - use YYYY/MM/DD\n") 3 0 3
23 year => $in_year,
24 month => $in_month,
25 day => $in_day,
26 ) or croak "Couldn't understand your date - use YYYY/MM/DD\n";
27 3 ▶ croak "Cannot use a date in the future\n" if $in_date > $today;
Branch: 1 branch
BranchTrueFalseTotal
if $in_date > $today 0 3 3
28 3 $today = $in_date;
29 }
30  
31 return bless {
32 do_twitter => $opts{do_twitter} // 0,
33 do_file => $opts{do_file} // 1,
34 do_http => $opts{do_http} // 0,
35 do_udp => $opts{do_udp} // 1,
36 logdir => $opts{logdir},
37 date => $today,
38 user => $opts{user} || $ENV{SUDO_USER} || $ENV{USER},
39 in => $opts{read_from} || \*STDIN,
40 udp => $opts{udp},
41 http => $opts{http},
42 4 ▶ }, $class;
Condition50%8/16
$opts{"do_twitter"} // 0
l!l
04
$opts{"do_file"} // 1
l!l
04
$opts{"do_http"} // 0
l!l
04
$opts{"do_udp"} // 1
l!l
04
$opts{'user'} || $ENV{'SUDO_USER'} || $ENV{'USER'}
l!l&&r!l&&!r
004
$opts{"user"} or $ENV{"SUDO_USER"}
l!l&&r!l&&!r
004
$opts{'read_from'} || \*STDIN
l!l
22
MC/DC0%0/1
$opts{"do_twitter"} // 0
$opts{"do_twitter"}
MC/DC0%0/1
$opts{"do_file"} // 1
$opts{"do_file"}
MC/DC0%0/1
$opts{"do_http"} // 0
$opts{"do_http"}
MC/DC0%0/1
$opts{"do_udp"} // 1
$opts{"do_udp"}
MC/DC0%0/3
$opts{'user'} || $ENV{'SUDO_USER'} || $ENV{'USER'}
$opts{"user"} $ENV{"SUDO_USER"} $ENV{'USER'}
MC/DC100%1/1
$opts{'read_from'} || \*STDIN
$opts{'read_from'}
Truth table50%1/2
A
A= $opts{"do_twitter"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"do_file"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"do_http"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"do_udp"}
Aresult
11
00
Truth table25%1/4
A or B || C
A= $opts{"user"}B= $ENV{"SUDO_USER"}C= $ENV{'USER'}
ABCresult
1XX1
01X1
0011
0000
Truth table100%2/2
A
A= $opts{'read_from'}
Aresult
11
00
43 }
44  
45  
46 3 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod documented
47 3 my $self = shift;
48 3 my $cmd = shift;
49  
50 3 ▶ $cmd ||= 'log';
Condition100%2/2
$cmd ||= "log"
l!l
12
MC/DC100%1/1
$cmd ||= "log"
$cmd
Truth table100%2/2
A
A= $cmd
Aresult
11
00
51 3 $self->run_command($cmd);
52 1 return;
53 }
54  
55 3 ▶sub run_command {
Subroutine run_command: called CC=2 SCAR=7.0
Pod undocumented
56 3 my $self = shift;
57 3 my $cmd = shift;
58  
59 3 my $s = $self->can("run_command_$cmd");
60 3 ▶ die "Unknown command '$cmd'" unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 3 3
61 3 return $self->$s();
62 }
63  
64 2 ▶sub run_command_log {
Subroutine run_command_log: called CC=5 SCAR=19.8
Pod undocumented
65 2 my $self = shift;
66 2 say 'Log entry:';
67 2 my $in = $self->{in};
68 2 my $logentry = <$in>; # one line
69 2 chomp $logentry;
70 2 ▶ croak 'A log entry is needed' unless $logentry;
Branch: 1 branch
BranchTrueFalseTotal
unless $logentry 0 2 2
71  
72 2 PLUGIN: foreach my $plugin ( $self->plugins(app => $self) ) {
73 2 ▶ next PLUGIN unless $plugin->can('log');
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->can("log") 0 2 2
74 2 my $r = $plugin->log($logentry);
75 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
76 0 my $name = ref $plugin;
77 0 my $re = __PACKAGE__ . '::';
78 0 $name =~ s/^$re//;
79 0 say sprintf '[%-8s] %s', $name, $r;
80 }
81 }
82 }
83  
84 1 ▶sub run_command_view {
Subroutine run_command_view: called CC=3 SCAR=11.0
Pod undocumented
85 1 my $self = shift;
86 1 PLUGIN: foreach my $plugin ( $self->plugins(app => $self) ) {
87 3 ▶ next PLUGIN unless $plugin->can('view');
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->can("view") 2 1 3
88 1 $plugin->view();
89 }
90 }
91  
92  
93 5 1;
94  
95 __END__
« Simple/UDP.pm ↑ Summary Simple/File.pm »