Lim/CLI.pm

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