lib/App/nodie.pm

filter: Statementstmt 39.5% 32 / 81 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 3 totaltotal 27.1% 42 / 155 CC 26 SCAR 61.8
CC
26
Cov
24%
main
702.0
get_logtime
2.0
run
2.0
CRAP 480.7
↑ Summary lib/App/nodie/nodie.pl »
1 package App::nodie;
2 =head1 NAME
3  
4 App::nodie - runs command again when its dead
5  
6 =head1 VERSION
7  
8 version 1.03
9  
10 =head1 SYNOPSIS
11  
12 #!/bin/sh
13 perl -MApp::nodie -erun -- command arg1 arg2 ...
14  
15 =head1 DESCRIPTION
16  
17 App::nodie runs command again when its dead.
18  
19 See also: L<nodie.pl|https://metacpan.org/pod/distribution/App-nodie/lib/App/nodie/nodie.pl>
20  
21 =cut
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use feature qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶no if ($] >= 5.018), 'warnings' => 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Lazy::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 require Exporter;
35 1 our $VERSION = '1.03';
36 1 our @ISA = qw(Exporter);
37 1 our @EXPORT = qw(main run);
38 1 our @EXPORT_OK = qw();
39 }
40  
41  
42 0 ▶sub get_logtime {
Subroutine get_logtime: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
44 0 return sprintf("[%04d-%02d-%02d %02d:%02d:%02d]", $year+1900, $mon+1, $mday, $hour, $min, $sec);
45 }
46  
47 0 ▶sub main {
Subroutine main: not called CC=26 SCAR=65.5
Pod undocumented
48 0 my $cmdargs = cmdargs({ valuableArgs => 0, noCommand => 1, optionAtAll => 0 }, @_);
49 0 ▶ if (defined($cmdargs->{'-h'}) or defined($cmdargs->{'--help'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmdargs->{"-h"} or defined $cmdargs->{"--help"}) 0 0 0
Condition0%0/3
defined $cmdargs->{"-h"} or defined $cmdargs->{"--help"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $cmdargs->{"-h"} or defined $cmdargs->{"--help"}
defined $cmdargs->{"-h"} defined $cmdargs->{"--help"}
Truth table0%0/3
A or B
A= defined $cmdargs->{"-h"}B= defined $cmdargs->{"--help"}
ABresult
1X1
011
000
50 {
51 0 my @lines;
52 0 @lines = get_pod_text(dirname(__FILE__)."/nodie/nodie.pl", "SYNOPSIS");
53 0 ▶ @lines = get_pod_text(dirname(__FILE__)."/nodie/nodie.pl", "ABSTRACT") unless defined($lines[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lines[0] 0 0 0
54 0 $lines[0] = "nodie.pl";
55 0 say join("\n", @lines);
56 0 return 0;
57 }
58 0 my $arg_exitcodes = $cmdargs->{'-e'};
59 0 ▶ $arg_exitcodes = $cmdargs->{'--exitcodes'} unless defined($arg_exitcodes);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_exitcodes 0 0 0
60 0 ▶ $arg_exitcodes = "" unless defined($arg_exitcodes);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_exitcodes 0 0 0
61 0 my @exitcodes = split(/\s*,\s*/, $arg_exitcodes);
62 0 my %exitcodes = array_to_hash(@exitcodes);
63 0 ▶ while (my $key = each %exitcodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = each %exitcodes)) 0 0 0
64 0 my $value = $exitcodes{$key};
65 0 ▶ unless (looks_like_number($value) and $value == int($value) and $value >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($value) and $value == int $value and $value >= 0) 0 0 0
Condition0%0/6
&looks_like_number($value) and $value == int $value and $value >= 0
!ll&&!rl&&r
000
&looks_like_number($value) and $value == int $value
!ll&&!rl&&r
000
MC/DC0%0/3
&looks_like_number($value) and $value == int $value and $value >= 0
&looks_like_number($value) $value == int $value $value >= 0
Truth table0%0/4
A and B and C
A= &looks_like_number($value)B= $value == int $valueC= $value >= 0
ABCresult
0XX0
10X0
1100
1111
66 0 delete $exitcodes{$key};
67 0 next;
68 }
69 0 $exitcodes{$key} = int($value);
70 }
71 0 @exitcodes = values %exitcodes;
72 0 ▶ push @exitcodes, 0, 2 unless @exitcodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @exitcodes 0 0 0
73 0 my $arg_log = $cmdargs->{'-l'};
74 0 ▶ $arg_log = $cmdargs->{'--log'} unless defined($arg_log);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_log 0 0 0
75 0 my $log_fh;
76 0 ▶ if (defined($arg_log)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg_log) 0 0 0
77 0 ▶ $arg_log = "&STDERR" if $arg_log =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_log =~ /^\s*$/ 0 0 0
78 0 ▶ $arg_log = "&STDOUT" if $arg_log =~ /^\s*\-\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_log =~ /^\s*\-\s*$/ 0 0 0
79 0 my $mode = "";
80 0 ▶ if ($arg_log =~ /^&(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_log =~ /^&(.*)$/) 0 0 0
81 0 $mode .= "&";
82 0 $arg_log = $1;
83 }
84 0 ▶ open($log_fh, ">>".$mode, $arg_log) or undef($log_fh);
Branch: 1 branch
BranchTrueFalseTotal
open $log_fh, '>>' . $mode, $arg_log or undef $log_fh 0 0 0
85 0 ▶ warn "Can't open log file $mode$arg_log: $!\n" unless defined($log_fh);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $log_fh 0 0 0
86 }
87 0 my @params = (@{$cmdargs->{parameters}}, @{$cmdargs->{late_parameters}});
88 0 ▶ die "command is not specified\n" unless @params;
Branch: 1 branch
BranchTrueFalseTotal
unless @params 0 0 0
89 0 my $exitcode;
90 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
grep /^$exitcode$/, @exitcodes or do { do { sleep 1 if defined $exitcode; print $log_fh get_logtime() . ' ' . (defined $exitcode ? "Restarting" : "Starting") . "...\n" if defined $log_fh; sleep 1 if defined $exitcode; $exitcode = system2(@params); die "$!\n" if $exitcode < 0; print $log_fh get_logtime() . " Returned exit code: $exitcode\n" if defined $log_fh } } 0 0 0
91 0 ▶ sleep 1 if defined($exitcode);
Branch: 1 branch
BranchTrueFalseTotal
if defined $exitcode 0 0 0
92 0 ▶ print $log_fh get_logtime()." ".(defined($exitcode)? "Restarting": "Starting")."...\n" if defined($log_fh);
Branch: 2 branches
BranchTrueFalseTotal
if defined $log_fh 0 0 0
defined $exitcode ? : 0 0 0
93 0 ▶ sleep 1 if defined($exitcode);
Branch: 1 branch
BranchTrueFalseTotal
if defined $exitcode 0 0 0
94 0 $exitcode = system2(@params);
95 0 ▶ die "$!\n" if $exitcode < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exitcode < 0 0 0 0
96 0 ▶ print $log_fh get_logtime()." Returned exit code: $exitcode\n" if defined($log_fh);
Branch: 1 branch
BranchTrueFalseTotal
if defined $log_fh 0 0 0
97 } while (not grep(/^$exitcode$/, @exitcodes));
98 0 return $exitcode;
99 }
100  
101 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
102 0 return main(@ARGV);
103 }
104  
105  
106 1 1;
107 __END__
↑ Summary lib/App/nodie/nodie.pl »