| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package OSGi::Osgish::Command; | ||
| 4 | use strict; | ||
| 5 | use POSIX qw(strftime); | ||
| 6 | use Term::Clui; | ||
| 7 | |||
| 8 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | OSGi::Osgish::Command - Base object for commands | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This is the base command from which all osgish commands should be extended. It | ||
| 17 | provides registration hooks so that the command handler can determine the | ||
| 18 | position of this command in the menu structure. Additionally it provides common | ||
| 19 | methods useful for each command to perform its action. | ||
| 20 | |||
| 21 | A L<OSGi::Osgish::Command> is a collection of shell commands, grouped in a | ||
| 22 | certain context. E.g L<OSGi::Osgish::Command::Bundle> groups all bundle related | ||
| 23 | actions. It can be reused in different contexts and hence can occur at | ||
| 24 | different places in the menu structure. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item $command_handler = new OSGi::Osgish::Command($osgish) | ||
| 33 | |||
| 34 | Constructor, which should not called be directly on this module but on a | ||
| 35 | submodule. In fact, it will be called (indirectly) only by the | ||
| 36 | L<OSGi::Osgish::CommandHandler> during the command registration process. | ||
| 37 | The single argument required is the central osgish object. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub new { | ||
| 42 | my $class = shift; | ||
| 43 | my $osgish = shift; | ||
| 44 | my $self = ref($_[0]) eq "HASH" ? $_[0] : { @_ }; | ||
| 45 | bless $self,(ref($class) || $class); | ||
| 46 | $self->{osgish} = $osgish; | ||
| 47 | return $self; | ||
| 48 | } | ||
| 49 | |||
| 50 | =item $global_commands = $cmd->global_commands | ||
| 51 | |||
| 52 | This method is called by the command handler during registration in order to | ||
| 53 | obtain the global commands which are always present in the menu. The default | ||
| 54 | implementation returns C<undef> which means that no global commands should be | ||
| 55 | registered. Overwrite this to provide a command hashref as known to | ||
| 56 | L<Term::ShellUI> for setting the global commands. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub global_commands { | ||
| 61 | return undef; | ||
| 62 | } | ||
| 63 | |||
| 64 | =item $top_commands = $cmd->top_commands | ||
| 65 | |||
| 66 | This method is called by the command handler during registration in order to | ||
| 67 | obtain the top commands which are present in the top level menu. The default | ||
| 68 | implementation returns C<undef> which means that no top commands are to be | ||
| 69 | registered. Overwrite this to provide a command hashref as known to | ||
| 70 | L<Term::ShellUI> for setting the top commands. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub top_commands { | ||
| 75 | return undef; | ||
| 76 | } | ||
| 77 | |||
| 78 | =item $osgish = $cmd->osgish | ||
| 79 | |||
| 80 | Get the osgish object used during construction. This is a convenience method | ||
| 81 | for sublassed commands. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub osgish { | ||
| 86 | return shift->{osgish}; | ||
| 87 | } | ||
| 88 | |||
| 89 | =item $complete_handler = $cmd->complete | ||
| 90 | |||
| 91 | Convenience method to get the L<OSGi::Osgish::CompletionHandler> for getting | ||
| 92 | various command line completions. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub complete { | ||
| 97 | return shift->{osgish}->complete; | ||
| 98 | } | ||
| 99 | |||
| 100 | =item $agent = $cmd->agent | ||
| 101 | |||
| 102 | Convenience method to get the L<OSGi::Osgish::Agent> in order to | ||
| 103 | contact the server agent bundle (via L<JMX::Jmx4Perl>) | ||
| 104 | |||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub agent { | ||
| 109 | return shift->{osgish}->agent; | ||
| 110 | } | ||
| 111 | |||
| 112 | =item @colors = $cmd->color(@color_ids) | ||
| 113 | |||
| 114 | Return a list of ANSI color strings for the given symbolic color names which | ||
| 115 | are looked up from the current color theme. If no coloring is enabled, empty | ||
| 116 | strings are returned. This method dispatched directly to the underylyin | ||
| 117 | C<osgish> object. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub color { | ||
| 122 | return shift->{osgish}->color(@_); | ||
| 123 | } | ||
| 124 | |||
| 125 | =item $cmd->push_on_stack("context",$cmds) | ||
| 126 | |||
| 127 | Rerturn a sub (closure) which can be used as a command to update the context | ||
| 128 | stack managed by the command handler. Update in this sense means push the given | ||
| 129 | context ("C<context>") on the stack, remembering the provided shell commands | ||
| 130 | C<$cmds> for later use when traversing the stack upwards via C<..> | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub push_on_stack { | ||
| 135 | my $self = shift; | ||
| 136 | my @args = @_; | ||
| 137 | return sub { | ||
| 138 | $self->{osgish}->{commands}->push_on_stack(@args); | ||
| 139 | }; | ||
| 140 | } | ||
| 141 | |||
| 142 | =item ($opts,@args) = $cmd->extract_command_options($spec,@args); | ||
| 143 | |||
| 144 | Extract any options from a command specified via C<$spec>. This method uses | ||
| 145 | L<Getopt::Long> for extrating the options. It returns a hashref with the | ||
| 146 | extracted options and an array of remaining arguments | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub extract_command_options { | ||
| 151 | my ($self,$spec,@args) = @_; | ||
| 152 | my $opts = {}; | ||
| 153 | GetOptionsFromArray(\@args, $opts,@{$spec}); | ||
| 154 | return ($opts,@args); | ||
| 155 | } | ||
| 156 | |||
| 157 | =item $label = $cmd->format_date($time) | ||
| 158 | |||
| 159 | Formats a date like for C<ls -l>: | ||
| 160 | |||
| 161 | Dec 2 18:21 | ||
| 162 | Jun 23 2009 | ||
| 163 | |||
| 164 | This format is especially useful when used in listing. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub format_date { | ||
| 169 | my $self = shift; | ||
| 170 | my $time = shift; | ||
| 171 | if (time - $time > 60*60*24*365) { | ||
| 172 | return strftime "%b %d %Y",localtime($time); | ||
| 173 | } else { | ||
| 174 | return strftime "%b %d %H:%M",localtime($time); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | =item $cmd->print_paged($txt,$nr_lines) | ||
| 179 | |||
| 180 | Use a pager for printing C<$txt> which has C<$nr_lines> lines. Only if | ||
| 181 | C<$nr_lines> exceeds a certain limit (default: 24), then the pager is used, | ||
| 182 | otherwise C<$txt> is printed directly. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub print_paged { | ||
| 187 | my $self = shift; | ||
| 188 | my $text = shift; | ||
| 189 | my $nr = shift; | ||
| 190 | if (!$nr) { | ||
| 191 | $nr = scalar(split /\n/s,$text); | ||
| 192 | } | ||
| 193 | if (defined($nr) && $nr < 24) { | ||
| 194 | print $text; | ||
| 195 | } else { | ||
| 196 | view("",$text); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | =item $trimmed = $cmd->trim_string($string,$max) | ||
| 201 | |||
| 202 | Trim a string C<$string> to a certain length C<$max>, i.e. if C<$string> is | ||
| 203 | larger than C<$max>, then it is truncated to to C<$max-3> and C<...> is | ||
| 204 | appended. If it is less or equal, than C<$string> is returned unchanged. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub trim_string { | ||
| 209 | my $self = shift; | ||
| 210 | my $string = shift; | ||
| 211 | my $max = shift; | ||
| 212 | return length($string) > $max ? substr($string,0,$max-3) . "..." : $string; | ||
| 213 | } | ||
| 214 | |||
| 215 | =item $converted = $cmd->convert_wildcard_pattern_to_regexp($orig) | ||
| 216 | |||
| 217 | Convert the wildcards C<*> and C<.> to their regexp equivalent and return a | ||
| 218 | regular expression. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub convert_wildcard_pattern_to_regexp { | ||
| 223 | my $self = shift; | ||
| 224 | my $wildcard = shift; | ||
| 225 | $wildcard =~ s/\?/./g; | ||
| 226 | $wildcard =~ s/\*/.*/g; | ||
| 227 | return qr/^$wildcard$/; | ||
| 228 | } | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | =head1 LICENSE | ||
| 235 | |||
| 236 | This file is part of osgish. | ||
| 237 | |||
| 238 | Osgish is free software: you can redistribute it and/or modify | ||
| 239 | it under the terms of the GNU General Public License as published by | ||
| 240 | the Free Software Foundation, either version 2 of the License, or | ||
| 241 | (at your option) any later version. | ||
| 242 | |||
| 243 | osgish is distributed in the hope that it will be useful, | ||
| 244 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 245 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 246 | GNU General Public License for more details. | ||
| 247 | |||
| 248 | You should have received a copy of the GNU General Public License | ||
| 249 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 250 | |||
| 251 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 252 | further details. | ||
| 253 | |||
| 254 | =head1 PROFESSIONAL SERVICES | ||
| 255 | |||
| 256 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 257 | general), you might want to have a look at www.consol.com Contact | ||
| 258 | roland.huss@consol.de for further information (or use the contact form at | ||
| 259 | http://www.consol.com/contact/) | ||
| 260 | |||
| 261 | =head1 AUTHOR | ||
| 262 | |||
| 263 | roland@cpan.org | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | 1; | ||
| 268 |