Osgish/Command/Global.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 84 CC 7 SCAR 19.6
CC
7
Cov
0%
cmd_shutdown
12.0
cmd_restart
12.0
global_commands
6.0
CRAP 7.1
1 #!/usr/bin/perl
2  
3 package OSGi::Osgish::Command::Global;
4 use strict;
5 use vars qw(@ISA);
6 use Term::ANSIColor qw(:constants);
7 use Term::Clui;
8  
9 @ISA = qw(OSGi::Osgish::Command);
10  
11 =head1 NAME
12  
13 OSGi::Osgish::Command::Global - Globally available commands
14  
15 =head1 DESCRIPTION
16  
17 =head1 COMMANDS
18  
19 =over
20  
21 =cut
22  
23  
24  
25 sub name { "global" }
26  
27 sub global_commands {
28 my $self = shift;
29 my $osgi = $self->agent;
30
31 return
32 {
33 "error" => {
34 desc => "Show last error (if any)",
35 proc => $self->cmd_last_error,
36 doc => <<EOT
37 Show the last error, if any occured. Including all
38 stacktraces returned by the server.
39 EOT
40 },
41 "help" => {
42 desc => "Print online help",
43 args => sub { shift->help_args(undef, @_); },
44 method => sub { shift->help_call(undef, @_); },
45 doc => <<EOT,
46 help [<command>]
47 h [<command>]
48  
49 Print online help. Without option, show a summary. With
50 option, show specific help for command <command>.
51 EOT
52 },
53 "h" => { alias => "help", exclude_from_completion=>1},
54 "history" => {
55 desc => "Command History",
56 doc => <<EOT,
57  
58 history [-c] [-d <num>]
59  
60 Specify a number to list the last N lines of history
61  
62 Options:
63 -c : Clear the command history
64 -d <num> : Delete a single item <num>
65 EOT
66 args => "[-c] [-d] [number]",
67 method => sub { shift->history_call(@_) },
68 },
69 "quit" => {
70 desc => "Quit",
71 maxargs => 0,
72 method => sub { shift->exit_requested(1); },
73 doc => <<EOT,
74 Quit osgish.
75 EOT
76 },
77 "q" => { alias => 'quit', exclude_from_completion => 1 },
78 $osgi ? ("shutdown" => {
79 desc => "Shutdown server",
80 proc => $self->cmd_shutdown,
81 },
82 "restart" => {
83 desc => "Restart server",
84 proc => $self->cmd_restart
85 }
86 ) : ()
87 };
88 }
89  
90 # Shutdown a server
91 sub cmd_shutdown {
92 my $self = shift;
93 return sub {
94 my $osgish = $self->osgish;
95 my $osgi = $osgish->agent;
96 unless ($osgi) {
97 print "Not connected to a server\n";
98 return;
99 }
100 my ($yellow,$reset) = $osgish->color("host",RESET);
101 my $server = $osgish->server;
102 my $answer = &choose("Really shutdown " . $yellow . $server . $reset . " ?","yes","no");
103 if ($answer eq "yes") {
104 $osgi->shutdown;
105 $osgish->agent(undef);
106 $osgish->commands->reset_stack;
107 } else {
108 print "Shutdown of ". $yellow . $server . $reset . " cancelled\n";
109 }
110 }
111 }
112  
113 # Restart the server's framework
114 sub cmd_restart {
115 my $self = shift;
116 return sub {
117 my $osgish = $self->osgish;
118 my $osgi = $osgish->agent;
119 unless ($osgi) {
120 print "Not connected to a server\n";
121 return;
122 }
123 my ($yellow,$reset) = $osgish->color("host",RESET);
124 my $server = $osgish->server;
125 my $answer = &choose("Really restart " . $yellow . $server . $reset . " ?","yes","no");
126 if ($answer eq "yes") {
127 $osgi->restart;
128 } else {
129 print "Restart of ". $yellow . $server . $reset . " cancelled\n";
130 }
131 }
132 }
133  
134  
135 sub cmd_last_error {
136 my $self = shift;
137 return sub {
138 my $osgi = $self->osgish->agent;
139 my $txt = $self->osgish->last_error;
140 if ($txt) {
141 chomp $txt;
142 print "$txt\n";
143 } else {
144 print "No errors\n";
145 }
146 }
147 }
148  
149 =head1 LICENSE
150  
151 This file is part of osgish.
152  
153 Osgish is free software: you can redistribute it and/or modify
154 it under the terms of the GNU General Public License as published by
155 the Free Software Foundation, either version 2 of the License, or
156 (at your option) any later version.
157  
158 osgish is distributed in the hope that it will be useful,
159 but WITHOUT ANY WARRANTY; without even the implied warranty of
160 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
161 GNU General Public License for more details.
162  
163 You should have received a copy of the GNU General Public License
164 along with osgish. If not, see <http://www.gnu.org/licenses/>.
165  
166 A commercial license is available as well. Please contact roland@cpan.org for
167 further details.
168  
169 =head1 PROFESSIONAL SERVICES
170  
171 Just in case you need professional support for this module (or JMX or OSGi in
172 general), you might want to have a look at www.consol.com Contact
173 roland.huss@consol.de for further information (or use the contact form at
174 http://www.consol.com/contact/)
175  
176 =head1 AUTHOR
177  
178 roland@cpan.org
179  
180 =cut
181  
182 1;
183