1 package PVE
::CLIHandler
;
8 use PVE
::Exception
qw(raise raise_param_exc);
13 use base
qw(PVE::RESTHandler);
17 my $cli_handler_class;
19 my $expand_command_name = sub {
24 for my $k (keys(%$def)) {
27 $expanded = undef; # more than one match
34 $cmd = $expanded if $expanded;
39 my $complete_command_names = sub {
42 return if ref($cmddef) ne 'HASH';
44 foreach my $cmd (keys %$cmddef) {
45 next if $cmd eq 'help';
52 __PACKAGE__-
>register_method ({
56 description
=> "Get help about specified command.",
58 additionalProperties
=> 0,
61 description
=> "Command name",
64 completion
=> $complete_command_names,
67 description
=> "Verbose output format.",
73 returns
=> { type
=> 'null' },
78 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
80 my $cmd = $param->{cmd
};
82 my $verbose = defined($cmd) && $cmd;
83 $verbose = $param->{verbose
} if defined($param->{verbose
});
87 print_usage_verbose
();
89 print_usage_short
(\
*STDOUT
);
94 $cmd = &$expand_command_name($cmddef, $cmd);
96 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
98 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
100 my $pwcallback = $cli_handler_class->can('read_password');
102 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
103 $verbose ?
'full' : 'short', $pwcallback);
107 print "USAGE: $str\n";
114 sub print_simple_asciidoc_synopsys
{
115 my ($class, $name, $arg_param, $uri_param) = @_;
117 die "not initialized" if !$cli_handler_class;
119 my $pwcallback = $cli_handler_class->can('read_password');
121 my $synopsis = "*${name}* `help`\n\n";
123 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param, 'asciidoc', $pwcallback);
128 sub print_asciidoc_synopsys
{
130 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
132 my $pwcallback = $cli_handler_class->can('read_password');
136 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
139 foreach my $cmd (sort keys %$cmddef) {
140 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
141 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
142 $uri_param, 'asciidoc', $pwcallback);
143 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
145 $synopsis .= "$str\n\n";
154 sub print_simple_pod_manpage
{
155 my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
157 die "not initialized" if !$cli_handler_class;
159 my $pwcallback = $cli_handler_class->can('read_password');
161 my $synopsis = " $name help\n\n";
162 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
167 my $parser = PVE
::PodParser-
>new();
168 $parser->{include
}->{synopsis
} = $synopsis;
169 $parser->parse_from_file($podfn);
172 sub print_pod_manpage
{
175 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
176 die "no pod file specified" if !$podfn;
178 my $pwcallback = $cli_handler_class->can('read_password');
182 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
184 my $style = 'full'; # or should we use 'short'?
186 foreach my $cmd (sorted_commands
()) {
187 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
188 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
189 $uri_param, $style, $pwcallback);
190 $str =~ s/^USAGE: //;
192 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
194 $synopsis .= " $str\n\n";
200 my $parser = PVE
::PodParser-
>new();
201 $parser->{include
}->{synopsis
} = $synopsis;
202 $parser->parse_from_file($podfn);
205 sub print_usage_verbose
{
207 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
209 my $pwcallback = $cli_handler_class->can('read_password');
211 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
213 foreach my $cmd (sort keys %$cmddef) {
214 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
215 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
216 'full', $pwcallback);
221 sub sorted_commands
{
222 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
225 sub print_usage_short
{
228 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
230 my $pwcallback = $cli_handler_class->can('read_password');
232 print $fd "ERROR: $msg\n" if $msg;
233 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
236 foreach my $cmd (sorted_commands
()) {
237 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
238 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback);
239 print $fd "\n" if $oldclass && $oldclass ne $class;
245 my $print_bash_completion = sub {
246 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
250 return if !(defined($cur) && defined($prev) && defined($bash_command));
251 return if !defined($ENV{COMP_LINE
});
252 return if !defined($ENV{COMP_POINT
});
254 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
255 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
257 my $args = PVE
::Tools
::split_args
($cmdline);
258 my $pos = scalar(@$args) - 2;
259 $pos += 1 if $cmdline =~ m/\s+$/;
261 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
265 my $print_result = sub {
267 print "$p\n" if $p =~ m/^$cur/;
276 &$print_result(keys %$cmddef);
282 my $def = $cmddef->{$cmd};
285 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
289 my ($class, $name, $arg_param, $uri_param) = @$def;
293 $arg_param = [ $arg_param ] if !ref($arg_param);
295 map { $skip_param->{$_} = 1; } @$arg_param;
296 map { $skip_param->{$_} = 1; } keys %$uri_param;
298 my $fpcount = scalar(@$arg_param);
300 my $info = $class->map_method_by_name($name);
302 my $schema = $info->{parameters
};
303 my $prop = $schema->{properties
};
305 my $print_parameter_completion = sub {
307 my $d = $prop->{$pname};
308 if ($d->{completion
}) {
309 my $vt = ref($d->{completion
});
311 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
312 &$print_result(@$res);
314 } elsif ($d->{type
} eq 'boolean') {
315 &$print_result('0', '1');
316 } elsif ($d->{enum
}) {
317 &$print_result(@{$d->{enum
}});
321 # positional arguments
322 $pos += 1 if $simple_cmd;
323 if ($fpcount && $pos <= $fpcount) {
324 my $pname = $arg_param->[$pos -1];
325 &$print_parameter_completion($pname);
329 my @option_list = ();
330 foreach my $key (keys %$prop) {
331 next if $skip_param->{$key};
332 push @option_list, "--$key";
336 &$print_result(@option_list);
340 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
342 &$print_parameter_completion($pname);
346 &$print_result(@option_list);
352 # simply verify all registered methods
353 PVE
::RESTHandler
::validate_method_schemas
();
356 my $get_exe_name = sub {
366 sub generate_bash_completions
{
369 # generate bash completion config
371 $exename = &$get_exe_name($class);
374 # $exename bash completion
376 # see http://tiswww.case.edu/php/chet/bash/FAQ
377 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
378 # this modifies global var, but I found no better way
379 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
381 complete -o default -C '$exename bashcomplete' $exename
385 sub find_cli_class_source
{
392 my $cpath = "PVE/CLI/${name}.pm";
393 my $spath = "PVE/Service/${name}.pm";
394 foreach my $p (@INC) {
395 foreach my $s (($cpath, $spath)) {
396 my $testfn = "$p/$s";
402 last if defined($filename);
408 sub generate_pod_manpage
{
409 my ($class, $podfn) = @_;
411 $cli_handler_class = $class;
413 $exename = &$get_exe_name($class);
415 $podfn = find_cli_class_source
($exename) if !defined($podfn);
417 die "unable to find source for class '$class'" if !$podfn;
420 my $def = ${"${class}::cmddef"};
422 if (ref($def) eq 'ARRAY') {
423 print_simple_pod_manpage
($podfn, @$def);
427 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
429 print_pod_manpage
($podfn);
433 sub generate_asciidoc_synopsys
{
436 $cli_handler_class = $class;
438 $exename = &$get_exe_name($class);
441 my $def = ${"${class}::cmddef"};
443 if (ref($def) eq 'ARRAY') {
444 print_simple_asciidoc_synopsys
(@$def);
448 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
450 print_asciidoc_synopsys
();
454 my $handle_cmd = sub {
455 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
460 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
463 print_usage_short
(\
*STDERR
, "no command specified");
465 } elsif ($cmd eq 'verifyapi') {
466 PVE
::RESTHandler
::validate_method_schemas
();
468 } elsif ($cmd eq 'printmanpod') {
469 $podfn = find_cli_class_source
($exename) if !defined($podfn);
470 print_pod_manpage
($podfn);
472 } elsif ($cmd eq 'bashcomplete') {
473 &$print_bash_completion($cmddef, 0, @$args);
477 &$preparefunc() if $preparefunc;
479 $cmd = &$expand_command_name($cmddef, $cmd);
481 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
484 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
488 my $prefix = "$exename $cmd";
489 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
491 &$outsub($res) if $outsub;
494 my $handle_simple_cmd = sub {
495 my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_;
497 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
498 die "no class specified" if !$class;
500 if (scalar(@$args) >= 1) {
501 if ($args->[0] eq 'help') {
502 my $str = "USAGE: $name help\n";
503 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
504 print STDERR
"$str\n\n";
506 } elsif ($args->[0] eq 'bashcomplete') {
508 &$print_bash_completion({ $name => $def }, $name, @$args);
510 } elsif ($args->[0] eq 'verifyapi') {
511 PVE
::RESTHandler
::validate_method_schemas
();
513 } elsif ($args->[0] eq 'printmanpod') {
514 $podfn = find_cli_class_source
($name) if !defined($podfn);
515 print_simple_pod_manpage
($podfn, @$def);
520 &$preparefunc() if $preparefunc;
522 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
524 &$outsub($res) if $outsub;
528 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
530 # Note: "depreciated function run_cli - use run_cli_handler instead";
532 die "password callback is no longer supported" if $pwcallback;
534 run_cli_handler
($class, podfn
=> $podfn, prepare
=> $preparefunc);
537 sub run_cli_handler
{
538 my ($class, %params) = @_;
540 $cli_handler_class = $class;
542 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
544 foreach my $key (keys %params) {
545 next if $key eq 'podfn';
546 next if $key eq 'prepare';
547 next if $key eq 'no_init'; # used by lxc hooks
548 die "unknown parameter '$key'";
551 my $podfn = $params{podfn
};
552 my $preparefunc = $params{prepare
};
553 my $no_init = $params{no_init
};
555 my $pwcallback = $class->can('read_password');
557 $exename = &$get_exe_name($class);
561 if ($class !~ m/^PVE::Service::/) {
562 die "please run as root\n" if $> != 0;
564 PVE
::INotify
::inotify_init
() if !$no_init;
566 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
567 $rpcenv->init_request() if !$no_init;
568 $rpcenv->set_language($ENV{LANG
});
569 $rpcenv->set_user('root@pam');
573 my $def = ${"${class}::cmddef"};
575 if (ref($def) eq 'ARRAY') {
576 &$handle_simple_cmd($def, \
@ARGV, $pwcallback, $podfn, $preparefunc);
579 my $cmd = shift @ARGV;
580 &$handle_cmd($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc);