Perlbal/ManageCommand.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 62.5% 5 / 8 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 13 totaltotal 78.2% 90 / 115 CC 8 SCAR 6.5
CC
8
Cov
91%
parse_error
4.8
parse
3.0
out
2.1
CRAP 1.9
« Perlbal/Plugin/AutoRemoveLeadingDir.pm ↑ Summary Perlbal/Fields.pm »
1 # class representing a one-liner management command. all the responses
2 # to a command should be done through this instance (out, err, ok, etc)
3 #
4 # Copyright 2005-2007, Six Apart, Ltd.
5 #
6  
7 package Perlbal::ManageCommand;
8 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 'base', # the base command name (like "proc")
14 'cmd',
15 'ok',
16 'err',
17 'out',
18 'orig',
19 'argn',
20 'ctx',
21 22 );
22  
23 1298 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
24 1298 my ($class, $base, $cmd, $out, $ok, $err, $orig, $ctx) = @_;
25 1298 my $self = fields::new($class);
26  
27 1298 $self->{base} = $base;
28 1298 $self->{cmd} = $cmd;
29 1298 $self->{ok} = $ok;
30 1298 $self->{err} = $err;
31 1298 $self->{out} = $out;
32 1298 $self->{orig} = $orig;
33 1298 $self->{ctx} = $ctx;
34 1298 $self->{argn} = [];
35 1298 return $self;
36 }
37  
38 # returns an managecommand object for functions that need one, but
39 # this does nothing but explode if there any problems.
40 871 ▶sub loud_crasher {
Subroutine loud_crasher: called CC=1 SCAR=0.1
Pod undocumented
41 22 ▶ use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 0 ▶ __PACKAGE__->new(undef, undef, sub {}, sub {}, sub { confess "MC:err: @_" }, "", Perlbal::CommandContext->new);
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 }
44  
45 172 ▶sub out { my $mc = shift; return @_ ? $mc->{out}->(@_) : $mc->{out}; }
Subroutine out: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 172 0 172
46 1243 ▶sub ok { my $mc = shift; return $mc->{ok}->(@_); }
Subroutine ok: called CC=1 SCAR=0.0
Pod undocumented
47  
48 11 ▶sub err {
Subroutine err: called CC=1 SCAR=0.0
Pod undocumented
49 11 my ($mc, $err) = @_;
50 11 $err =~ s/\n$//;
51 11 $mc->{err}->($err);
52 }
53  
54 3 ▶sub cmd { my $mc = shift; return $mc->{cmd}; }
Subroutine cmd: called CC=1 SCAR=0.0
Pod undocumented
55 6 ▶sub orig { my $mc = shift; return $mc->{orig}; }
Subroutine orig: called CC=1 SCAR=0.0
Pod undocumented
56 25 ▶sub end { my $mc = shift; $mc->{out}->("."); 1; }
Subroutine end: called CC=1 SCAR=0.0
Pod undocumented
57  
58 408 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod undocumented
59 408 my $mc = shift;
60 408 my $regexp = shift;
61 408 my $usage = shift;
62  
63 408 my @ret = ($mc->{cmd} =~ /$regexp/);
64 408 ▶ $mc->parse_error($usage) unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 2 406 408
65  
66 406 my $i = 0;
67 406 foreach (@ret) {
68 1077 $mc->{argn}[$i++] = $_;
69 }
70 406 return $mc;
71 }
72  
73 66 ▶sub arg {
Subroutine arg: called CC=1 SCAR=0.0
Pod undocumented
74 66 my $mc = shift;
75 66 my $n = shift; # 1-based array, to correspond with $1, $2, $3
76 66 return $mc->{argn}[$n - 1];
77 }
78  
79 358 ▶sub args {
Subroutine args: called CC=1 SCAR=0.0
Pod undocumented
80 358 my $mc = shift;
81 358 return @{$mc->{argn}};
82 }
83  
84 2 ▶sub parse_error {
Subroutine parse_error: called CC=4 SCAR=15.6
Pod undocumented
85 2 my $mc = shift;
86 2 my $usage = shift;
87 2 ▶ $usage .= "\n" if $usage && $usage !~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
if $usage and not $usage =~ /\n$/ 2 0 2
Condition33%1/3
$usage and not $usage =~ /\n$/
!ll&&!rl&&r
002
MC/DC0%0/2
$usage and not $usage =~ /\n$/
$usage not $usage =~ /\n$/
Truth table33%1/3
A and not(B)
A= $usageB= not $usage =~ /\n$/
ABresult
0X0
100
111
88 2 ▶ die $usage || "Invalid syntax to '$mc->{base}' command\n"
Condition50%1/2
$usage || "Invalid syntax to '$mc->{'base'}' command\n"
l!l
20
MC/DC0%0/1
$usage || "Invalid syntax to '$mc->{'base'}' command\n"
$usage
Truth table50%1/2
A
A= $usage
Aresult
11
00
89 }
90  
91 18 ▶sub no_opts {
Subroutine no_opts: called CC=2 SCAR=7.1
Pod undocumented
92 18 my $mc = shift;
93 die "The '$mc->{base}' command takes no arguments\n"
94 18 ▶ unless $mc->{cmd} eq $mc->{base};
Branch: 1 branch
BranchTrueFalseTotal
unless $mc->{"cmd"} eq $mc->{"base"} 0 18 18
95 18 return $mc;
96 }
97  
98 22 1;
99  
100 # Local Variables:
101 # mode: perl
102 # c-basic-indent: 4
103 # indent-tabs-mode: nil
104 # End:
« Perlbal/Plugin/AutoRemoveLeadingDir.pm ↑ Summary Perlbal/Fields.pm »