File Coverage

blib/lib/Perlbal/ManageCommand.pm
Criterion Covered Total %
statement 66 66 100.0
branch 5 8 62.5
condition 2 6 33.3
subroutine 19 19 100.0
pod 0 13 0.0
total 92 112 82.1


line stmt bran cond sub pod time code
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     22   135 use strict;
  22         57  
  22         868  
9 22     22   130 use warnings;
  22         45  
  22         856  
10 22     22   134 no warnings qw(deprecated);
  22         61  
  22         1217  
11              
12             use fields (
13 22         167 'base', # the base command name (like "proc")
14             'cmd',
15             'ok',
16             'err',
17             'out',
18             'orig',
19             'argn',
20             'ctx',
21 22     22   132 );
  22         56  
22              
23             sub new {
24 1271     1271 0 2958 my ($class, $base, $cmd, $out, $ok, $err, $orig, $ctx) = @_;
25 1271         3632 my $self = fields::new($class);
26              
27 1271         110769 $self->{base} = $base;
28 1271         4692 $self->{cmd} = $cmd;
29 1271         2497 $self->{ok} = $ok;
30 1271         1945 $self->{err} = $err;
31 1271         1971 $self->{out} = $out;
32 1271         2025 $self->{orig} = $orig;
33 1271         1858 $self->{ctx} = $ctx;
34 1271         2289 $self->{argn} = [];
35 1271         16630 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             sub loud_crasher {
41 22     22   5019 use Carp qw(confess);
  22         49  
  22         16851  
42 414     414 0 6330 __PACKAGE__->new(undef, undef, sub {}, sub {}, sub { confess "MC:err: @_" }, "", Perlbal::CommandContext->new);
  170     858   3086  
  274         3888  
  858         7276  
43             }
44              
45 174 50   174 0 326 sub out { my $mc = shift; return @_ ? $mc->{out}->(@_) : $mc->{out}; }
  174         843  
46 1219     1219 0 2039 sub ok { my $mc = shift; return $mc->{ok}->(@_); }
  1219         4577  
47              
48             sub err {
49 10     10 0 29 my ($mc, $err) = @_;
50 10         41 $err =~ s/\n$//;
51 10         117 $mc->{err}->($err);
52             }
53              
54 3     3 0 7 sub cmd { my $mc = shift; return $mc->{cmd}; }
  3         20  
55 6     6 0 15 sub orig { my $mc = shift; return $mc->{orig}; }
  6         58  
56 24     24 0 79 sub end { my $mc = shift; $mc->{out}->("."); 1; }
  24         115  
  24         182  
57              
58             sub parse {
59 395     395 0 853 my $mc = shift;
60 395         706 my $regexp = shift;
61 395         596 my $usage = shift;
62              
63 395         4703 my @ret = ($mc->{cmd} =~ /$regexp/);
64 395 100       1237 $mc->parse_error($usage) unless @ret;
65              
66 393         603 my $i = 0;
67 393         744 foreach (@ret) {
68 1045         4594 $mc->{argn}[$i++] = $_;
69             }
70 393         1682 return $mc;
71             }
72              
73             sub arg {
74 57     57 0 113 my $mc = shift;
75 57         121 my $n = shift; # 1-based array, to correspond with $1, $2, $3
76 57         11912 return $mc->{argn}[$n - 1];
77             }
78              
79             sub args {
80 350     350 0 575 my $mc = shift;
81 350         459 return @{$mc->{argn}};
  350         10278  
82             }
83              
84             sub parse_error {
85 2     2 0 4 my $mc = shift;
86 2         7 my $usage = shift;
87 2 50 33     22 $usage .= "\n" if $usage && $usage !~ /\n$/;
88 2   33     25 die $usage || "Invalid syntax to '$mc->{base}' command\n"
89             }
90              
91             sub no_opts {
92 17     17 0 58 my $mc = shift;
93 17 50       216 die "The '$mc->{base}' command takes no arguments\n"
94             unless $mc->{cmd} eq $mc->{base};
95 17         64 return $mc;
96             }
97              
98             1;
99              
100             # Local Variables:
101             # mode: perl
102             # c-basic-indent: 4
103             # indent-tabs-mode: nil
104             # End: