1 package PVE
::CLIHandler
;
8 use PVE
::Exception
qw(raise raise_param_exc);
12 use base
qw(PVE::RESTHandler);
17 my $expand_command_name = sub {
22 for my $k (keys(%$def)) {
25 $expanded = undef; # more than one match
32 $cmd = $expanded if $expanded;
37 my $complete_command_names = sub {
40 return if ref($cmddef) ne 'HASH';
42 foreach my $cmd (keys %$cmddef) {
43 next if $cmd eq 'help';
50 __PACKAGE__-
>register_method ({
54 description
=> "Get help about specified command.",
56 additionalProperties
=> 0,
59 description
=> "Command name",
62 completion
=> $complete_command_names,
65 description
=> "Verbose output format.",
71 returns
=> { type
=> 'null' },
76 die "not initialized" if !($cmddef && $exename);
78 my $cmd = $param->{cmd
};
80 my $verbose = defined($cmd) && $cmd;
81 $verbose = $param->{verbose
} if defined($param->{verbose
});
85 print_usage_verbose
();
87 print_usage_short
(\
*STDOUT
);
92 $cmd = &$expand_command_name($cmddef, $cmd);
94 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
96 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
99 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ?
'full' : 'short');
103 print "USAGE: $str\n";
110 sub print_simple_pod_manpage
{
111 my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
113 my $synopsis = " $name help\n\n";
114 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
119 my $parser = PVE
::PodParser-
>new();
120 $parser->{include
}->{synopsis
} = $synopsis;
121 $parser->parse_from_file($podfn);
124 sub print_pod_manpage
{
127 die "not initialized" if !($cmddef && $exename);
128 die "no pod file specified" if !$podfn;
132 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
134 my $style = 'full'; # or should we use 'short'?
136 foreach my $cmd (sorted_commands
()) {
137 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
138 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
140 $str =~ s/^USAGE: //;
142 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
144 $synopsis .= " $str\n\n";
150 my $parser = PVE
::PodParser-
>new();
151 $parser->{include
}->{synopsis
} = $synopsis;
152 $parser->parse_from_file($podfn);
155 sub print_usage_verbose
{
157 die "not initialized" if !($cmddef && $exename);
159 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
161 foreach my $cmd (sort keys %$cmddef) {
162 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
163 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full');
168 sub sorted_commands
{
169 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
172 sub print_usage_short
{
175 die "not initialized" if !($cmddef && $exename);
177 print $fd "ERROR: $msg\n" if $msg;
178 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
181 foreach my $cmd (sorted_commands
()) {
182 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
183 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short');
184 print $fd "\n" if $oldclass && $oldclass ne $class;
190 my $print_bash_completion = sub {
191 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
195 return if !(defined($cur) && defined($prev) && defined($bash_command));
196 return if !defined($ENV{COMP_LINE
});
197 return if !defined($ENV{COMP_POINT
});
199 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
200 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
202 # fixme: shell quoting??
203 my @args = split(/\s+/, $cmdline);
204 my $pos = scalar(@args) - 2;
205 $pos += 1 if $cmdline =~ m/\s+$/;
207 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
211 my $print_result = sub {
213 print "$p\n" if $p =~ m/^$cur/;
222 &$print_result(keys %$cmddef);
228 my $def = $cmddef->{$cmd};
231 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
235 my ($class, $name, $arg_param, $uri_param) = @$def;
239 map { $skip_param->{$_} = 1; } @$arg_param;
240 map { $skip_param->{$_} = 1; } keys %$uri_param;
242 my $fpcount = scalar(@$arg_param);
244 my $info = $class->map_method_by_name($name);
246 my $schema = $info->{parameters
};
247 my $prop = $schema->{properties
};
249 my $print_parameter_completion = sub {
251 my $d = $prop->{$pname};
252 if ($d->{completion
}) {
253 my $vt = ref($d->{completion
});
255 my $res = $d->{completion
}->($cmd, $pname, $cur);
256 &$print_result(@$res);
258 } elsif ($d->{type
} eq 'boolean') {
259 &$print_result('0', '1');
260 } elsif ($d->{enum
}) {
261 &$print_result(@{$d->{enum
}});
265 # positional arguments
266 $pos += 1 if $simple_cmd;
267 if ($fpcount && $pos <= $fpcount) {
268 my $pname = $arg_param->[$pos -1];
269 &$print_parameter_completion($pname);
273 my @option_list = ();
274 foreach my $key (keys %$prop) {
275 next if $skip_param->{$key};
276 push @option_list, "--$key";
280 &$print_result(@option_list);
284 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
286 &$print_parameter_completion($pname);
290 &$print_result(@option_list);
296 # simply verify all registered methods
297 PVE
::RESTHandler
::validate_method_schemas
();
300 sub generate_bash_completions
{
303 # generate bash completion config
306 $exename =~ s/^.*:://;
309 # $exename bash completion
311 # see http://tiswww.case.edu/php/chet/bash/FAQ
312 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
313 # this modifies global var, but I found no better way
314 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
316 complete -C '$exename bashcomplete' $exename
320 sub find_cli_class_source
{
325 my $cpath = "PVE/CLI/${exename}.pm";
326 my $spath = "PVE/Service/${exename}.pm";
327 foreach my $p (@INC) {
328 foreach my $s (($cpath, $spath)) {
329 my $testfn = "$p/$s";
335 last if defined($filename);
341 sub generate_pod_manpage
{
342 my ($class, $podfn) = @_;
345 $exename =~ s/^.*:://;
347 $podfn = find_cli_class_source
($exename) if !defined($podfn);
349 die "unable to find source for class '$class'" if !$podfn;
352 my $def = ${"${class}::cmddef"};
354 if (ref($def) eq 'ARRAY') {
355 print_simple_pod_manpage
($podfn, @$def);
359 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
361 print_pod_manpage
($podfn);
366 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
368 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
371 $exename =~ s/^.*:://;
376 if ($class !~ m/^PVE::Service::/) {
377 die "please run as root\n" if $> != 0;
379 PVE
::INotify
::inotify_init
();
381 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
382 $rpcenv->init_request();
383 $rpcenv->set_language($ENV{LANG
});
384 $rpcenv->set_user('root@pam');
388 my $def = ${"${class}::cmddef"};
390 if (ref($def) eq 'ARRAY') {
391 handle_simple_cmd
($def, \
@ARGV, $pwcallback, $podfn, $preparefunc);
394 my $cmd = shift @ARGV;
395 handle_cmd
($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc);
402 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
407 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
410 print_usage_short
(\
*STDERR
, "no command specified");
412 } elsif ($cmd eq 'verifyapi') {
413 PVE
::RESTHandler
::validate_method_schemas
();
415 } elsif ($cmd eq 'printmanpod') {
416 $podfn = find_cli_class_source
($exename) if !defined($podfn);
417 print_pod_manpage
($podfn);
419 } elsif ($cmd eq 'bashcomplete') {
420 &$print_bash_completion($cmddef, 0, @$args);
424 &$preparefunc() if $preparefunc;
426 $cmd = &$expand_command_name($cmddef, $cmd);
428 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
431 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
435 my $prefix = "$exename $cmd";
436 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
438 &$outsub($res) if $outsub;
441 sub handle_simple_cmd
{
442 my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_;
444 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
445 die "no class specified" if !$class;
447 if (scalar(@$args) >= 1) {
448 if ($args->[0] eq 'help') {
449 my $str = "USAGE: $name help\n";
450 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
451 print STDERR
"$str\n\n";
453 } elsif ($args->[0] eq 'bashcomplete') {
455 &$print_bash_completion({ $name => $def }, $name, @$args);
457 } elsif ($args->[0] eq 'verifyapi') {
458 PVE
::RESTHandler
::validate_method_schemas
();
460 } elsif ($args->[0] eq 'printmanpod') {
461 $podfn = find_cli_class_source
($name) if !defined($podfn);
462 print_simple_pod_manpage
($podfn, @$def);
467 &$preparefunc() if $preparefunc;
469 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
471 &$outsub($res) if $outsub;