lib/ControlFreak/Command.pm

filter: Statementstmt 63.1% 108 / 171 Branchbran 39.0% 39 / 100 Conditioncond 27.7% 15 / 54 MC/DCmcdc 8.3% 3 / 36 Subroutinesub 52.0% 13 / 25 Podpod 20.0% 1 / 5 totaltotal 45.7% 179 / 391 CC 69 SCAR 44.5
CC
69
Cov
50%
process_socket
272.0
process_proxy
134.8
process_service
24.9
CRAP 85.5
↑ Summary blib/lib/ControlFreak/Commander.pm »
1 package ControlFreak::Command;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use ControlFreak::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use ControlFreak::Console;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use ControlFreak::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use AnyEvent::Socket();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Params::Util qw{ _STRING _INSTANCE _CODE };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf-8
13  
14 =head1 NAME
15  
16 ControlFreak::Command - turn string commands to method calls
17  
18 =head1 METHODS
19  
20 =head2 process(%param)
21  
22 Process a command from string and call either the C<ok> or the C<err>
23 callback with optionally a status string.
24  
25 C<%param> has the following keys
26  
27 =over 4
28  
29 =item * cmd
30  
31 The actual command string to process.
32  
33 =item * ok_cb
34  
35 The callback called when the command executed successfully.
36  
37 =item * err_cb
38  
39 The callback called when the command failed.
40  
41 =item * has_priv
42  
43 A boolean that if set gives access to the entire set of commands.
44  
45 =item * ctrl
46  
47 The controller.
48  
49 =back
50  
51 =cut
52  
53 40 ▶sub process {
Subroutine process: called CC=8 SCAR=21.0
Pod documented
54 40 my $class = shift;
55 40 my %param = @_;
56  
57 40 my $cmd = $param{cmd};
58 40 ▶ my $ok = $param{ok_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'ok_cb'} || sub { }
l!l&&r!l&&!r
4000
MC/DC0%0/2
$param{'ok_cb'} || sub { }
$param{'ok_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'ok_cb'}B= sub { }
ABresult
1X1
011
000
59 40 ▶ my $err = $param{err_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'err_cb'} || sub { }
l!l&&r!l&&!r
4000
MC/DC0%0/2
$param{'err_cb'} || sub { }
$param{'err_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'err_cb'}B= sub { }
ABresult
1X1
011
000
60  
61 40 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) 36 4 40
62 ## clean
63 36 $cmd =~ s/\#.*//; # comments
64 36 $cmd =~ s/^\s+//; # leading whitespaces
65 36 $cmd =~ s/\s+$//; # trailing whitespaces
66 }
67  
68 40 ▶ if (! $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 7 33 40
69 7 ▶ if ($param{ignore_void}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'ignore_void'}) { } 1 6 7
70 1 return;
71 }
72 else {
73 6 return $err->("command is void");
74 }
75 }
76  
77 33 my ($kw, $rest) = split /\s+/, $cmd, 2;
78  
79 33 ▶ return $err->("empty command") unless $kw;
Branch: 1 branch
BranchTrueFalseTotal
unless $kw 0 33 33
80  
81 33 my $meth = "process_$kw";
82 33 my $h = $class->can($meth);
83 33 ▶ return $err->("unknown command '$kw'") unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 1 32 33
84  
85 32 return $h->( $class, %param, cmd => $rest );
86 }
87  
88 28 ▶sub process_service {
Subroutine process_service: called CC=18 SCAR=32.2
Pod undocumented
89 28 my $class = shift;
90 28 my %param = @_;
91  
92 28 my $cmd = $param{cmd};
93 28 ▶ my $ok = $param{ok_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'ok_cb'} || sub { }
l!l&&r!l&&!r
2800
MC/DC0%0/2
$param{'ok_cb'} || sub { }
$param{'ok_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'ok_cb'}B= sub { }
ABresult
1X1
011
000
94 28 ▶ my $err = $param{err_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'err_cb'} || sub { }
l!l&&r!l&&!r
2800
MC/DC0%0/2
$param{'err_cb'} || sub { }
$param{'err_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'err_cb'}B= sub { }
ABresult
1X1
011
000
95 28 my $ctrl = $param{ctrl};
96  
97 return $err->("not authorized")
98 28 ▶ unless $param{has_priv};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"has_priv"} 2 26 28
99  
100 26 ▶ return $err->("empty service command") unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 3 23 26
101  
102 23 my ($svcname, $attr, $assignment);
103 23 ▶ if ($cmd =~ /^([\w-]+)\s+([\w-]+)\s*=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^([\w-]+)\s+([\w-]+)\s*=(.*)$/) { } 17 6 23
104 17 $svcname = $1;
105 17 $attr = $2;
106 17 $assignment = $3;
107 }
108 else {
109 6 return $err->("malformed service command '$cmd'");
110 }
111  
112 17 ▶ my $svc = $ctrl->find_or_create_svc($svcname)
Branch: 1 branch
BranchTrueFalseTotal
my $svc = $ctrl->find_or_create_svc($svcname) or return &$err("service name is invalid") 17 0 17
113 or return $err->("service name is invalid");
114  
115 ## Clean the value, before assigning it
116 17 my $value = $assignment;
117 17 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 17 0 17
118 17 $value =~ s/^\s+// ;
119  
120 ## DWIM with quotes
121 17 ▶ if ($value =~ /^"(.*)"/ or $value =~ /^'(.*)'/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^"(.*)"/ or $value =~ /^'(.*)'/) 2 15 17
Condition100%3/3
$value =~ /^"(.*)"/ or $value =~ /^'(.*)'/
l!l&&r!l&&!r
1115
MC/DC100%2/2
$value =~ /^"(.*)"/ or $value =~ /^'(.*)'/
$value =~ /^"(.*)"/ $value =~ /^'(.*)'/
Truth table100%3/3
A or B
A= $value =~ /^"(.*)"/B= $value =~ /^'(.*)'/
ABresult
1X1
011
000
122 2 $value = $1;
123 }
124 }
125  
126 17 ▶ if (defined $value && ! length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not length $value) 5 12 17
Condition66%2/3
defined $value and not length $value
!ll&&!rl&&r
0125
MC/DC50%1/2
defined $value and not length $value
defined $value not length $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not length $value
ABresult
0X0
100
111
127 5 $value = undef;
128 }
129  
130 ## cmd is special because of the array syntax
131 17 ▶ if ($attr eq 'cmd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq "cmd") 14 3 17
132 14 my $succ = $svc->set_cmd_from_con($value);
133 14 ▶ return $succ ? $ok->($svc) : $err->("invalid value");
Branch: 1 branch
BranchTrueFalseTotal
$succ ? : 11 3 14
134 }
135  
136 ## attributes existence check
137 3 my $meth = "set_$attr";
138 3 my $h = $svc->can($meth);
139 3 ▶ return $err->("invalid property '$attr'")
Branch: 1 branch
BranchTrueFalseTotal
unless $h 3 0 3
140 unless $h;
141  
142 0 my $success;
143 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
144 0 ▶ $value = _as_bool($value) if $attr =~/ ^ ignore_std(out|err)
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ / ^ ignore_std(out|err) | no_new_session | respawn_on_(fail|stop) $/x 0 0 0
145 | no_new_session
146 | respawn_on_(fail|stop) $/x;
147 0 $success = $h->($svc, $value);
148 }
149 else {
150 0 $success = $svc->unset($attr);
151 }
152  
153 0 ▶ return $success ? $ok->($svc) : $err->("invalid value");
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
154 }
155  
156 1 ▶sub process_command {
Subroutine process_command: called CC=7 SCAR=21.4
Pod undocumented
157 1 my $class = shift;
158 1 my %param = @_;
159  
160 1 my $cmd = $param{cmd};
161 1 ▶ my $ok = $param{ok_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'ok_cb'} || sub { }
l!l&&r!l&&!r
100
MC/DC0%0/2
$param{'ok_cb'} || sub { }
$param{'ok_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'ok_cb'}B= sub { }
ABresult
1X1
011
000
162 1 ▶ my $err = $param{err_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'err_cb'} || sub { }
l!l&&r!l&&!r
100
MC/DC0%0/2
$param{'err_cb'} || sub { }
$param{'err_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'err_cb'}B= sub { }
ABresult
1X1
011
000
163 1 my $ctrl = $param{ctrl};
164  
165 1 ▶ return $err->("empty command") unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 1 1
166  
167 1 my ($command, @args) = split /\s+/, $cmd;
168  
169 1 ▶ return $err->("malformed service command '$cmd'")
Branch: 1 branch
BranchTrueFalseTotal
unless $command or @args 0 1 1
Condition33%1/3
$command or @args
l!l&&r!l&&!r
100
MC/DC0%0/2
$command or @args
$command @args
Truth table33%1/3
A or B
A= $commandB= @args
ABresult
1X1
011
000
170 unless $command or @args;
171  
172 1 my $meth = "command_$command";
173 1 my $h = $ctrl->can($meth);
174 1 ▶ return $err->("unknown command '$command'") unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 1 1
175 1 $h->($ctrl, args => \@args, %param, err_cb => $err, ok_cb => $ok);
176 1 return;
177 }
178  
179 ## FIXME: very similar to process_service
180 0 ▶sub process_socket {
Subroutine process_socket: not called CC=16 SCAR=56.1
Pod undocumented
181 0 my $class = shift;
182 0 my %param = @_;
183  
184 0 my $cmd = $param{cmd};
185 0 ▶ my $ok = $param{ok_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$param{'ok_cb'} || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$param{'ok_cb'} || sub { }
$param{'ok_cb'} sub { }
Truth table0%0/3
A || B
A= $param{'ok_cb'}B= sub { }
ABresult
1X1
011
000
186 0 ▶ my $err = $param{err_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$param{'err_cb'} || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$param{'err_cb'} || sub { }
$param{'err_cb'} sub { }
Truth table0%0/3
A || B
A= $param{'err_cb'}B= sub { }
ABresult
1X1
011
000
187 0 my $ctrl = $param{ctrl};
188  
189 return $err->("not authorized")
190 0 ▶ unless $param{has_priv};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"has_priv"} 0 0 0
191  
192 0 ▶ return $err->("empty socket command") unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 0 0
193  
194 0 my ($sockname, $attr, $assignment);
195 0 ▶ if ($cmd =~ /^([\w-]+)\s+([\w-]+)\s*=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^([\w-]+)\s+([\w-]+)\s*=(.*)$/) { } 0 0 0
196 0 $sockname = $1;
197 0 $attr = $2;
198 0 $assignment = $3;
199 }
200 else {
201 0 return $err->("malformed socket command '$cmd'");
202 }
203  
204 0 ▶ my $sock = $ctrl->find_or_create_sock($sockname)
Branch: 1 branch
BranchTrueFalseTotal
my $sock = $ctrl->find_or_create_sock($sockname) or return &$err("socket name is invalid") 0 0 0
205 or return $err->("socket name is invalid");
206  
207 ## Clean the value, before assigning it
208 0 my $value = $assignment;
209 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
210 0 $value =~ s/^\s+// ;
211 ## ugly (and repeated)
212 0 ▶ $value = _as_bool($value) if $attr eq 'nonblocking';
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "nonblocking" 0 0 0
213  
214 ## DWIM with quotes
215 0 ▶ if ($value =~ /^"(.*)"/ or $value =~ /^'(.*)'/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^"(.*)"/ or $value =~ /^'(.*)'/) 0 0 0
Condition0%0/3
$value =~ /^"(.*)"/ or $value =~ /^'(.*)'/
l!l&&r!l&&!r
000
MC/DC0%0/2
$value =~ /^"(.*)"/ or $value =~ /^'(.*)'/
$value =~ /^"(.*)"/ $value =~ /^'(.*)'/
Truth table0%0/3
A or B
A= $value =~ /^"(.*)"/B= $value =~ /^'(.*)'/
ABresult
1X1
011
000
216 0 $value = $1;
217 }
218 }
219  
220 0 ▶ if (defined $value && ! length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not length $value) 0 0 0
Condition0%0/3
defined $value and not length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not length $value
defined $value not length $value
Truth table0%0/3
A and not(B)
A= defined $valueB= not length $value
ABresult
0X0
100
111
221 0 $value = undef;
222 }
223  
224 ## attributes existence check
225 0 my $meth = "set_$attr";
226 0 my $h = $sock->can($meth);
227 0 ▶ return $err->("invalid property '$attr'")
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
228 unless $h;
229  
230 0 my $success;
231 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
232 0 $success = $h->($sock, $value);
233 }
234 else {
235 0 $success = $sock->unset($attr);
236 }
237  
238 0 ▶ return $success ? $ok->($sock) : $err->("invalid value");
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
239 }
240  
241 5 ▶sub process_proxy {
Subroutine process_proxy: called CC=22 SCAR=49.0
Pod undocumented
242 5 my $class = shift;
243 5 my %param = @_;
244  
245 5 my $cmd = $param{cmd};
246 5 ▶ my $ok = $param{ok_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'ok_cb'} || sub { }
l!l&&r!l&&!r
500
MC/DC0%0/2
$param{'ok_cb'} || sub { }
$param{'ok_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'ok_cb'}B= sub { }
ABresult
1X1
011
000
247 5 ▶ my $err = $param{err_cb} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{'err_cb'} || sub { }
l!l&&r!l&&!r
500
MC/DC0%0/2
$param{'err_cb'} || sub { }
$param{'err_cb'} sub { }
Truth table33%1/3
A || B
A= $param{'err_cb'}B= sub { }
ABresult
1X1
011
000
248 5 my $ctrl = $param{ctrl};
249  
250 return $err->("not authorized")
251 5 ▶ unless $param{has_priv};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"has_priv"} 1 4 5
252  
253 4 ▶ return $err->("empty proxy command") unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 4 4
254  
255 4 my ($proxyname, $subcmd, $rest, $attr, $assignment);
256 4 ▶ if ($cmd =~ /^([\w-]+)\s+([\w-]+)\s+(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd =~ /^([\w-]+)\s+([\w-]+)\s+(.+)$/) { } 2 2 4
elsif ($cmd =~ /^([\w-]+)\s+([\w-]+)\s*=(.*)$/) { } 0 2 2
257 2 $proxyname = $1;
258 2 $subcmd = $2;
259 2 $rest = $3;
260 }
261 elsif ($cmd =~ /^([\w-]+)\s+([\w-]+)\s*=(.*)$/) {
262 0 $proxyname = $1;
263 0 $attr = $2;
264 0 $assignment = $3;
265 }
266 else {
267 2 return $err->("malformed proxy command '$cmd'");
268 }
269  
270 2 ▶ my $proxy = $ctrl->find_or_create_proxy($proxyname)
Branch: 1 branch
BranchTrueFalseTotal
my $proxy = $ctrl->find_or_create_proxy($proxyname) or return &$err("proxy name is invalid") 2 0 2
271 or return $err->("proxy name is invalid");
272  
273 2 ▶ if ($subcmd && $subcmd eq 'service') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcmd and $subcmd eq "service") 2 0 2
Condition33%1/3
$subcmd and $subcmd eq "service"
!ll&&!rl&&r
002
MC/DC0%0/2
$subcmd and $subcmd eq "service"
$subcmd $subcmd eq "service"
Truth table33%1/3
A and B
A= $subcmdB= $subcmd eq "service"
ABresult
0X0
100
111
274 2 my $svc;
275 $class->process_service(
276 cmd => $rest,
277 ctrl => $ctrl,
278 has_priv => 1,
279 1 ▶ ok_cb => sub { $svc = $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
280 2 err_cb => $err,
281 );
282 2 ▶ return unless $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc 1 1 2
283 1 $proxy->add_service($svc);
284 1 return $ok->($proxy);
285 }
286  
287 ## cmd is special because of the array syntax
288 0 ▶ if ($attr eq 'cmd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq "cmd") 0 0 0
289 0 my $succ = $proxy->set_cmd_from_con($assignment);
290 0 ▶ return $succ ? $ok->($proxy) : $err->("invalid value");
Branch: 1 branch
BranchTrueFalseTotal
$succ ? : 0 0 0
291 }
292  
293 ## Clean the value, before assigning it
294 0 my $value = $assignment;
295 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
296 0 $value =~ s/^\s+// ;
297 0 ▶ $value = _as_bool($value) if $attr =~ / ^noauto /x;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ / ^noauto /x 0 0 0
298  
299 ## DWIM with quotes
300 0 ▶ if ($value =~ /^"(.*)"/ or $value =~ /^'(.*)'/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^"(.*)"/ or $value =~ /^'(.*)'/) 0 0 0
Condition0%0/3
$value =~ /^"(.*)"/ or $value =~ /^'(.*)'/
l!l&&r!l&&!r
000
MC/DC0%0/2
$value =~ /^"(.*)"/ or $value =~ /^'(.*)'/
$value =~ /^"(.*)"/ $value =~ /^'(.*)'/
Truth table0%0/3
A or B
A= $value =~ /^"(.*)"/B= $value =~ /^'(.*)'/
ABresult
1X1
011
000
301 0 $value = $1;
302 }
303 }
304  
305 0 ▶ if (defined $value && ! length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not length $value) 0 0 0
Condition0%0/3
defined $value and not length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not length $value
defined $value not length $value
Truth table0%0/3
A and not(B)
A= defined $valueB= not length $value
ABresult
0X0
100
111
306 0 $value = undef;
307 }
308  
309 ## attributes existence check
310 0 my $meth = "set_$attr";
311 0 my $h = $proxy->can($meth);
312 0 ▶ return $err->("invalid property '$attr'")
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
313 unless $h;
314  
315 0 my $success;
316 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
317 0 $success = $h->($proxy, $value);
318 }
319 else {
320 0 $success = $proxy->unset($attr);
321 }
322  
323 0 ▶ return $success ? $ok->($proxy) : $err->("invalid value");
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
324 }
325  
326 0 ▶sub _as_bool {
Subroutine _as_bool: not called CC=3 SCAR=24.8
327 0 my $value = shift;
328 0 ▶ return 1 if $value =~ /^1| true| on| enabled|yes/xi;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^1| true| on| enabled|yes/xi 0 0 0
329 0 ▶ return 0 if $value =~ /^0|false|off|disabled| no/xi;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^0|false|off|disabled| no/xi 0 0 0
330 0 return;
331  
332 }
333  
334 8 "cd&c";
↑ Summary blib/lib/ControlFreak/Commander.pm »