Lim/CLI.pm

filter: Statementstmt 15.5% 48 / 308 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.1% 15 / 34 Podpod 100% 12 / 12 totaltotal 14.4% 75 / 518 CC 80 SCAR 48.5
CC
80
Cov
10%
new
306.0
process
272.0
__ANON__
210.0
CRAP 128.4
« Lim/RPC/Protocol/REST.pm ↑ Summary Lim/RPC/Transport/Client/RabbitMQ.pm »
1 package Lim::CLI;
2  
3 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Module::Find qw(findsubmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Lim::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Lim::Agent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Lim::Plugins ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =encoding utf8
23  
24 =head1 NAME
25  
26 Lim::CLI - The command line interface to Lim
27  
28 =head1 VERSION
29  
30 See L<Lim> for version.
31  
32 =cut
33  
34 2 our $VERSION = $Lim::VERSION;
35 2 our @BUILTINS = (qw(quit exit help));
36  
37 =head1 SYNOPSIS
38  
39 =over 4
40  
41 use Lim::CLI;
42  
43 $cli = Lim::CLI->new(...);
44  
45 =back
46  
47 =head1 DESCRIPTION
48  
49 This is the CLI that takes the input from the user and sends it to the plugin in
50 question. It uses L<AnyEvent::ReadLine::Gnu> if it is available and that enables
51 command line completion and history functions. It will load all plugins present
52 on the system and use their CLI part if it exists.
53  
54 Failing to have a supported readline module it will use a basic
55 L<AnyEvent::Handle> to read each line of input and process it.
56  
57 Built in commands that can not be used by any plugins are:
58  
59 =over 4
60  
61 quit - Will quit the CLI
62 exit - Will exit the relative section or quit the CLI
63 help - Will show help for the relative section where the user is
64  
65 =back
66  
67 =head1 METHODS
68  
69 =over 4
70  
71 =item $cli = Lim::CLI->new(key => value...)
72  
73 Create a new Lim::CLI object.
74  
75 =over 4
76  
77 =item on_quit => $callback->($cli_object)
78  
79 Callback to call when the CLI quits, either with the user doing CTRL-D, CTRL-C
80 or the command 'quit'.
81  
82 =back
83  
84 =cut
85  
86 0 ▶sub new {
Subroutine new: not called CC=17 SCAR=57.2
Pod documented
87 0 my $this = shift;
88 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
89 0 my %args = ( @_ );
90 0 my $self = {
91 logger => Log::Log4perl->get_logger($class),
92 cli => {},
93 busy => 0,
94 no_completion => 0,
95 prompt => 'lim> '
96 };
97 0 bless $self, $class;
98 0 weaken($self->{logger});
99 0 my $real_self = $self;
100 0 weaken($self);
101  
102 0 ▶ unless (defined $args{on_quit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"on_quit"}) 0 0 0
103 0 confess __PACKAGE__, ': Missing on_quit';
104 }
105 0 ▶ unless (ref($args{on_quit}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args{"on_quit"} eq "CODE") 0 0 0
106 0 confess __PACKAGE__, ': on_quit is not CODE';
107 }
108 0 $self->{on_quit} = $args{on_quit};
109  
110 0 foreach my $module (qw(Lim::Agent)) {
111 0 my $name = lc($module->Name);
112  
113 0 ▶ if (exists $self->{cli}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cli"}{$name}) 0 0 0
114 0 ▶ Lim::WARN and $self->{logger}->warn('Can not load internal CLI module ', $module, ': name ', $name, ' already in use');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not load internal CLI module ", $module, ": name ", $name, " already in use") 0 0 0
115 0 next;
116 }
117  
118 0 ▶ if (defined (my $obj = $module->CLI(cli => $self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $module->CLI("cli", $self))) 0 0 0
119 0 $self->{cli}->{$name} = {
120 name => $name,
121 module => $module,
122 obj => $obj
123 };
124 }
125 }
126  
127 0 foreach my $module (Lim::Plugins->instance->LoadedModules) {
128 0 my $name = lc($module->Name);
129  
130 0 ▶ if (exists $self->{cli}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cli"}{$name}) 0 0 0
131 0 ▶ Lim::WARN and $self->{logger}->warn('Can not use CLI module ', $module, ': name ', $name, ' already in use');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Can not use CLI module ", $module, ": name ", $name, " already in use") 0 0 0
132 0 next;
133 }
134  
135 0 ▶ if (defined (my $obj = $module->CLI(cli => $self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $module->CLI("cli", $self))) 0 0 0
136 0 $self->{cli}->{$name} = {
137 name => $name,
138 module => $module,
139 obj => $obj
140 };
141 }
142 }
143  
144 0 eval {
145 0 require AnyEvent::ReadLine::Gnu;
146 };
147 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
148 $self->{rl} = AnyEvent::ReadLine::Gnu->new(
149 prompt => 'lim> ',
150 0 ▶ on_line => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
152 0 return;
153 }
154  
155 0 $self->process(@_);
156 0 });
157  
158 0 $self->{rl}->Attribs->{completion_entry_function} = $self->{rl}->Attribs->{list_completion_function};
159 0 ▶ $self->{rl}->Attribs->{attempted_completion_function} = sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
160 0 my ($text, $line, $start, $end) = @_;
161  
162 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
163 0 return;
164 }
165  
166 0 my @parts = split(/\s+/o, substr($line, 0, $start));
167 0 my $builtins = 0;
168  
169 0 ▶ if ($self->{current}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current"}) 0 0 0
170 0 unshift(@parts, $self->{current}->{name});
171 0 $builtins = 1;
172 }
173  
174 0 ▶ if (scalar @parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parts) { } 0 0 0
175 0 my $part = shift(@parts);
176  
177 0 ▶ if (exists $self->{cli}->{$part}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'cli'}{$part}) { } 0 0 0
178 0 my $cmd = $self->{cli}->{$part}->{module}->Commands;
179  
180 0 ▶ while (defined ($part = shift(@parts))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($part = shift @parts)) 0 0 0
181 0 ▶ unless (exists $cmd->{$part} and ref($cmd->{$part}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cmd->{$part} and ref $cmd->{$part} eq "HASH") 0 0 0
Condition0%0/3
exists $cmd->{$part} and ref $cmd->{$part} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $cmd->{$part} and ref $cmd->{$part} eq "HASH"
exists $cmd->{$part} ref $cmd->{$part} eq "HASH"
Truth table0%0/3
A and B
A= exists $cmd->{$part}B= ref $cmd->{$part} eq "HASH"
ABresult
0X0
100
111
182 0 ▶ if ($self->{no_completion}++ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_completion"}++ == 2) 0 0 0
183 0 ▶ if (ref($cmd->{$part}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd->{$part} eq 'ARRAY') { } 0 0 0
184 0 ▶ if (@{$cmd->{$part}} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$cmd->{$part};} == 1) { } 0 0 0
elsif (@{$cmd->{$part};} == 2) { } 0 0 0
185 0 $self->println('completion finished: ', $part, '<RET> - ', $cmd->{$part}->[0]);
186 }
187 0 elsif (@{$cmd->{$part}} == 2) {
188 0 $self->println('completion finished: ', $part, ' ', $cmd->{$part}->[0], ' <RET> - ', $cmd->{$part}->[1]);
189 }
190 else {
191 0 $self->println('no completion found');
192 }
193 }
194 else {
195 0 $self->println('no completion found');
196 }
197 }
198 0 $self->{rl}->Attribs->{completion_word} = [];
199 0 return ();
200 }
201  
202 0 $builtins = 0;
203 0 $cmd = $cmd->{$part};
204 }
205 0 ▶ if ($builtins) {
Branch: 1 branch
BranchTrueFalseTotal
if ($builtins) { } 0 0 0
206 0 $self->{rl}->Attribs->{completion_word} = [keys %{$cmd}, @BUILTINS];
207 }
208 else {
209 0 $self->{rl}->Attribs->{completion_word} = [keys %{$cmd}];
210 }
211 }
212 else {
213 0 ▶ if ($self->{no_completion}++ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_completion"}++ == 2) 0 0 0
214 0 $self->println('no completion found');
215 }
216 0 $self->{rl}->Attribs->{completion_word} = [];
217 0 return;
218 }
219 }
220 else {
221 0 $self->{rl}->Attribs->{completion_word} = [keys %{$self->{cli}}, @BUILTINS];
222 }
223 0 $self->{no_completion} = 0;
224 0 return ();
225 0 };
226  
227 0 $self->{rl}->StifleHistory(Lim::Config->{cli}->{history_length});
228 0 ▶ if (Lim::Config->{cli}->{history_file} and -r Lim::Config->{cli}->{history_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Lim::Config()->{"cli"}{"history_file"} and -r &Lim::Config()->{"cli"}{"history_file"}) 0 0 0
Condition0%0/3
&Lim::Config()->{"cli"}{"history_file"} and -r &Lim::Config()->{"cli"}{"history_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
&Lim::Config()->{"cli"}{"history_file"} and -r &Lim::Config()->{"cli"}{"history_file"}
&Lim::Config()->{"cli"}{"history_file"} -r &Lim::Config()->{"cli"}{"history_file"}
Truth table0%0/3
A and B
A= &Lim::Config()->{"cli"}{"history_file"}B= -r &Lim::Config()->{"cli"}{"history_file"}
ABresult
0X0
100
111
229 0 $self->{rl}->ReadHistory(Lim::Config->{cli}->{history_file});
230 0 $self->{rl}->history_set_pos($self->{rl}->Attribs->{history_length});
231 }
232 }
233 else {
234 $self->{stdin_watcher} = AnyEvent::Handle->new(
235 fh => \*STDIN,
236 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
237 0 my ($handle, $fatal, $msg) = @_;
238 0 $handle->destroy;
239 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
240 0 return;
241 }
242 0 $self->{on_quit}($self);
243 },
244 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
245 0 my ($handle) = @_;
246 0 $handle->destroy;
247 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
248 0 return;
249 }
250 0 $self->{on_quit}($self);
251 },
252 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 my ($handle) = @_;
254  
255 0 ▶ $handle->push_read(line => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
256 0 shift;
257 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
258 0 return;
259 }
260 0 $self->process(@_);
261 0 });
262 0 });
263  
264 0 IO::Handle::autoflush STDOUT 1;
265 }
266  
267 0 ▶ if (defined (my $appender = Log::Log4perl->appender_by_name('LimCLI'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $appender = "Log::Log4perl"->appender_by_name("LimCLI"))) 0 0 0
268 0 Log::Log4perl->eradicate_appender('Screen');
269 0 $appender->{cli} = $self;
270 0 weaken($appender->{cli});
271 }
272  
273 0 $self->println('Welcome to LIM ', $Lim::VERSION, ' command line interface');
274 0 $self->prompt;
275  
276 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("new ", __PACKAGE__, " ", $self) 0 0 0
277 0 $real_self;
278 }
279  
280 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
281 0 my ($self) = @_;
282 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("destroy ", __PACKAGE__, " ", $self) 0 0 0
283  
284 0 ▶ if (exists $self->{rl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"rl"}) 0 0 0
285 0 ▶ if (Lim::Config->{cli}->{history_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Lim::Config()->{"cli"}{"history_file"}) 0 0 0
286 0 $self->{rl}->WriteHistory(Lim::Config->{cli}->{history_file});
287 }
288 }
289  
290 0 delete $self->{current};
291 0 delete $self->{rl};
292 0 delete $self->{stdin_watcher};
293 0 delete $self->{cli};
294 }
295  
296 =item $cli->process($line)
297  
298 Process a line of input, called from the input watcher
299 (L<AnyEvent::ReadLine::Gnu> or L<AnyEvent::Handle>).
300  
301 =cut
302  
303 0 ▶sub process {
Subroutine process: not called CC=16 SCAR=56.1
Pod documented
304 0 my ($self, $line) = @_;
305 0 my ($cmd, $args);
306  
307 0 ▶ if ($self->{busy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"busy"}) 0 0 0
308 0 return;
309 }
310  
311 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
312 0 ($cmd, $args) = split(/\s+/o, $line, 2);
313 0 $cmd = lc($cmd);
314 }
315 else {
316 0 $cmd = 'quit';
317 }
318  
319 0 ▶ if ($cmd eq 'quit') {
Branch: 3 branches
BranchTrueFalseTotal
if ($cmd eq 'quit') { } 0 0 0
elsif ($cmd eq 'exit') { } 0 0 0
elsif ($cmd eq 'help') { } 0 0 0
320 0 $self->{on_quit}($self);
321 0 return;
322 }
323 elsif ($cmd eq 'exit') {
324 0 ▶ if (exists $self->{current}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'current'}) { } 0 0 0
325 0 delete $self->{current};
326 0 $self->set_prompt('lim> ');
327 0 $self->prompt;
328 }
329 else {
330 0 $self->{on_quit}($self);
331 0 return;
332 }
333 }
334 elsif ($cmd eq 'help') {
335 0 ▶ if (exists $self->{current}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'current'}) { } 0 0 0
336 0 $self->print_command_help($self->{current}->{module}->Commands);
337 }
338 else {
339 0 my @cmds = keys %{$self->{cli}};
340 0 push(@cmds, @BUILTINS);
341 0 $self->println('Available commands: ', join(' ', sort @cmds));
342 }
343 0 $self->prompt;
344 }
345 else {
346 0 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) { } 0 0 0
347 0 ▶ if (exists $self->{current}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'current'}) { } 0 0 0
elsif (exists $self->{'cli'}{$cmd}) { } 0 0 0
348 0 ▶ if ($self->{current}->{module}->Commands->{$cmd} and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'current'}{'module'}->Commands->{$cmd} and $self->{'current'}{'obj'}->can($cmd)) { } 0 0 0
Condition0%0/3
$self->{"current"}{"module"}->Commands->{$cmd} and $self->{"current"}{"obj"}->can($cmd)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"current"}{"module"}->Commands->{$cmd} and $self->{"current"}{"obj"}->can($cmd)
$self->{"current"}{"module"}->Commands->{$cmd} $self->{"current"}{"obj"}->can($cmd)
Truth table0%0/3
A and B
A= $self->{"current"}{"module"}->Commands->{$cmd}B= $self->{"current"}{"obj"}->can($cmd)
ABresult
0X0
100
111
349 $self->{current}->{obj}->can($cmd))
350 {
351 0 $self->{busy} = 1;
352 0 $self->set_prompt('');
353 0 $self->{current}->{obj}->$cmd($args);
354 }
355 else {
356 0 $self->unknown_command($cmd);
357 }
358 }
359 elsif (exists $self->{cli}->{$cmd}) {
360 0 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 0 0 0
361 0 my $current = $self->{cli}->{$cmd};
362 0 ($cmd, $args) = split(/\s+/o, $args, 2);
363 0 $cmd = lc($cmd);
364  
365 0 ▶ if ($current->{module}->Commands->{$cmd} and
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{'module'}->Commands->{$cmd} and $current->{'obj'}->can($cmd)) { } 0 0 0
Condition0%0/3
$current->{"module"}->Commands->{$cmd} and $current->{"obj"}->can($cmd)
!ll&&!rl&&r
000
MC/DC0%0/2
$current->{"module"}->Commands->{$cmd} and $current->{"obj"}->can($cmd)
$current->{"module"}->Commands->{$cmd} $current->{"obj"}->can($cmd)
Truth table0%0/3
A and B
A= $current->{"module"}->Commands->{$cmd}B= $current->{"obj"}->can($cmd)
ABresult
0X0
100
111
366 $current->{obj}->can($cmd))
367 {
368 0 $self->{busy} = 1;
369 0 $self->set_prompt('');
370 0 $current->{obj}->$cmd($args);
371 }
372 else {
373 0 $self->unknown_command($cmd);
374 }
375 }
376 else {
377 0 $self->{current} = $self->{cli}->{$cmd};
378 0 $self->set_prompt('lim'.$self->{current}->{obj}->Prompt.'> ');
379 0 $self->prompt;
380 }
381 }
382 else {
383 0 $self->unknown_command($cmd);
384 }
385 }
386 else {
387 0 $self->prompt;
388 }
389 }
390 }
391  
392 =item $cli->prompt
393  
394 Print the prompt, called from C<process>.
395  
396 =cut
397  
398 0 ▶sub prompt {
Subroutine prompt: not called CC=2 SCAR=17.9
Pod documented
399 0 my ($self) = @_;
400  
401 0 ▶ if (exists $self->{rl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"rl"}) 0 0 0
402 0 return;
403 }
404  
405 0 $self->print($self->{prompt});
406 0 IO::Handle::flush STDOUT;
407 }
408  
409 =item $cli->set_prompt
410  
411 Set the prompt, called from C<process>.
412  
413 =cut
414  
415 0 ▶sub set_prompt {
Subroutine set_prompt: not called CC=2 SCAR=17.9
Pod documented
416 0 my ($self, $prompt) = @_;
417  
418 0 $self->{prompt} = $prompt;
419  
420 0 ▶ if (exists $self->{rl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"rl"}) 0 0 0
421 0 $self->{rl}->hide;
422 0 $AnyEvent::ReadLine::Gnu::prompt = $prompt;
423 0 $self->{rl}->show;
424 }
425  
426 0 $self;
427 }
428  
429 =item $cli->clear_line
430  
431 Reset the input.
432  
433 =cut
434  
435 0 ▶sub clear_line {
Subroutine clear_line: not called CC=2 SCAR=17.9
Pod documented
436 0 my ($self) = @_;
437  
438 0 ▶ if (exists $self->{rl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'rl'}) { } 0 0 0
439 0 $self->{rl}->replace_line('', 1);
440 0 $self->{rl}->hide;
441 0 $self->{rl}->show;
442 }
443 else {
444 0 $self->{stdin_watcher}->{rbuf} = '';
445 0 print "\r";
446 0 IO::Handle::flush STDOUT;
447 }
448  
449 0 $self;
450 }
451  
452 =item $cli->unknown_command
453  
454 Prints the "unknown command" error if the command can not be found.
455  
456 =cut
457  
458 0 ▶sub unknown_command {
Subroutine unknown_command: not called CC=1 SCAR=6.9
Pod documented
459 0 my ($self, $cmd) = @_;
460  
461 0 $self->println('unknown command: ', $cmd);
462 0 $self->prompt;
463  
464 0 $self;
465 }
466  
467 =item $cli->print
468  
469 Print some output, called from L<Lim::Component::CLI> and here.
470  
471 =cut
472  
473 0 ▶sub print {
Subroutine print: not called CC=3 SCAR=24.8
Pod documented
474 0 my $self = shift;
475  
476 0 ▶ if (exists $self->{rl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'rl'}) { } 0 0 0
477 0 $self->{rl}->print(@_);
478 }
479 else {
480 0 foreach (@_) {
481 0 print;
482 0 IO::Handle::flush STDOUT;
483 }
484 }
485  
486 0 $self;
487 }
488  
489 =item $cli->println
490  
491 Print some output and add a newline, called from L<Lim::Component::CLI> and
492 here.
493  
494 =cut
495  
496 0 ▶sub println {
Subroutine println: not called CC=3 SCAR=24.8
Pod documented
497 0 my $self = shift;
498  
499 0 ▶ if (exists $self->{rl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'rl'}) { } 0 0 0
500 0 $self->{rl}->hide;
501 0 $self->{rl}->print(@_, "\n");
502 0 $self->{rl}->show;
503 }
504 else {
505 0 foreach (@_) {
506 0 print;
507 0 IO::Handle::flush STDOUT;
508 }
509 0 print "\n";
510 0 IO::Handle::flush STDOUT;
511 }
512  
513 0 $self;
514 }
515  
516 =item $cli->print_command_help($module->Commands)
517  
518 Print the help for all commands from a plugin.
519  
520 =cut
521  
522 0 ▶sub print_command_help {
Subroutine print_command_help: not called CC=7 SCAR=40.3
Pod documented
523 0 my ($self, $commands, $level) = @_;
524 0 my $space = ' ' x ($level * 4);
525  
526 0 ▶ if (ref($commands) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $commands eq "HASH") 0 0 0
527 0 foreach my $key (sort (keys %$commands)) {
528 0 ▶ if (ref($commands->{$key}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $commands->{$key} eq 'HASH') { } 0 0 0
elsif (ref $commands->{$key} eq 'ARRAY') { } 0 0 0
529 0 $self->println($space, $key);
530 0 $self->print_command_help($commands->{$key}, $level+1);
531 }
532 elsif (ref($commands->{$key}) eq 'ARRAY') {
533 0 ▶ if (@{$commands->{$key}} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$commands->{$key};} == 1) { } 0 0 0
elsif (@{$commands->{$key};} == 2) { } 0 0 0
534 0 $self->println($space, $key, ' - ', $commands->{$key}->[0]);
535 }
536 0 elsif (@{$commands->{$key}} == 2) {
537 0 $self->println($space, $key, ' ', $commands->{$key}->[0], ' - ', $commands->{$key}->[1]);
538 }
539 else {
540 0 $self->println($space, $key, ' - unknown/invalid help');
541 }
542 }
543 else {
544 0 $self->println($space, $key, ' - no help');
545 }
546 }
547 }
548  
549 0 $self;
550 }
551  
552 =item $cli->Successful
553  
554 Called from L<Lim::Component::CLI> when a command was successful.
555  
556 =cut
557  
558 0 ▶sub Successful {
Subroutine Successful: not called CC=2 SCAR=17.9
Pod documented
559 0 my ($self) = @_;
560  
561 0 $self->{busy} = 0;
562 0 ▶ if (exists $self->{current}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'current'}) { } 0 0 0
563 0 $self->set_prompt('lim'.$self->{current}->{obj}->Prompt.'> ');
564 }
565 else {
566 0 $self->set_prompt('lim> ');
567 }
568 0 $self->prompt;
569 0 return;
570 }
571  
572 =item $cli->Error($LimError || @error_text)
573  
574 Called from L<Lim::Component::CLI> when a command issued an error. The error can
575 be a L<Lim::Error> object or list of strings that will be joined to produce an
576 error string.
577  
578 =cut
579  
580 0 ▶sub Error {
Subroutine Error: not called CC=6 SCAR=37.4
Pod documented
581 0 my $self = shift;
582  
583 0 ▶ $self->print('Command Error: ', ( scalar @_ > 0 ? '' : 'unknown' ));
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 ? : 0 0 0
584 0 foreach (@_) {
585 0 ▶ if (blessed $_ and $_->isa('Lim::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa('Lim::Error')) { } 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("Lim::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Lim::Error")
builtin::blessed($_) $_->isa("Lim::Error")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("Lim::Error")
ABresult
0X0
100
111
586 0 $self->print($_->toString);
587 }
588 else {
589 0 $self->print($_);
590 }
591 }
592 0 $self->println;
593  
594 0 $self->{busy} = 0;
595 0 ▶ if (exists $self->{current}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'current'}) { } 0 0 0
596 0 $self->set_prompt('lim'.$self->{current}->{obj}->Prompt.'> ');
597 }
598 else {
599 0 $self->set_prompt('lim> ');
600 }
601 0 $self->prompt;
602 }
603  
604 =item $cli->Editor($content)
605  
606 Call up an editor for the C<$content> provided. Will return the new content if
607 it has changed or undef on error or if nothing was changed.
608  
609 Will use L<Lim::Config>->{cli}->{editor} which will be the environment variable
610 EDITOR or what ever your configure it to be.
611  
612 =cut
613  
614 0 ▶sub Editor {
Subroutine Editor: not called CC=10 SCAR=47.0
Pod documented
615 0 my ($self, $content) = @_;
616 0 my $tmp = File::Temp->new;
617 0 my $sha = Digest::SHA::sha1_base64($content);
618  
619 0 ▶ Lim::DEBUG and $self->{logger}->debug('Editing ', $tmp->filename, ', hash before ', $sha);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Editing ", $tmp->filename, ", hash before ", $sha) 0 0 0
620  
621 0 print $tmp $content;
622 0 $tmp->flush;
623  
624 # TODO check if editor exists
625  
626 0 ▶ if (system(Lim::Config->{cli}->{editor}, $tmp->filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (system &Lim::Config()->{"cli"}{"editor"}, $tmp->filename) 0 0 0
627 0 ▶ Lim::DEBUG and $self->{logger}->debug('EDITOR returned failure');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("EDITOR returned failure") 0 0 0
628 0 return;
629 }
630  
631 0 my $fh = IO::File->new;
632 0 ▶ unless ($fh->open($tmp->filename)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($tmp->filename)) 0 0 0
633 0 ▶ Lim::DEBUG and $self->{logger}->debug('Unable to reopen temp file');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Unable to reopen temp file") 0 0 0
634 0 return;
635 }
636  
637 0 $fh->seek(0, SEEK_END);
638 0 my $tell = $fh->tell;
639 0 $fh->seek(0, SEEK_SET);
640 0 ▶ unless ($fh->read($content, $tell) == $tell) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->read($content, $tell) == $tell) 0 0 0
641 0 ▶ Lim::DEBUG and $self->{logger}->debug('Unable to read temp file');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Unable to read temp file") 0 0 0
642 0 return;
643 }
644  
645 0 ▶ if ($sha eq Digest::SHA::sha1_base64($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha eq &Digest::SHA::sha1_base64($content)) 0 0 0
646 0 ▶ Lim::DEBUG and $self->{logger}->debug('No change detected, checksum is the same');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("No change detected, checksum is the same") 0 0 0
647 0 return;
648 }
649  
650 0 return $content;
651 }
652  
653 =back
654  
655 =head1 AUTHOR
656  
657 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
658  
659 =head1 BUGS
660  
661 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
662  
663 =head1 SUPPORT
664  
665 You can find documentation for this module with the perldoc command.
666  
667 perldoc Lim::CLI
668  
669 You can also look for information at:
670  
671 =over 4
672  
673 =item * Lim issue tracker (report bugs here)
674  
675 L<https://github.com/jelu/lim/issues>
676  
677 =back
678  
679 =head1 ACKNOWLEDGEMENTS
680  
681 =head1 LICENSE AND COPYRIGHT
682  
683 Copyright 2012-2013 Jerry Lundström.
684  
685 This program is free software; you can redistribute it and/or modify it
686 under the terms of either: the GNU General Public License as published
687 by the Free Software Foundation; or the Artistic License.
688  
689 See http://dev.perl.org/licenses/ for more information.
690  
691  
692 =cut
693  
694 2 1; # End of Lim::CLI
« Lim/RPC/Protocol/REST.pm ↑ Summary Lim/RPC/Transport/Client/RabbitMQ.pm »