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');
101 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
103 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
104 $verbose ?
'full' : 'short', $pwcallback,
109 print "USAGE: $str\n";
116 sub print_simple_asciidoc_synopsys
{
117 my ($class, $name, $arg_param, $uri_param) = @_;
119 die "not initialized" if !$cli_handler_class;
121 my $pwcallback = $cli_handler_class->can('read_password');
122 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
124 my $synopsis = "*${name}* `help`\n\n";
126 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param,
127 'asciidoc', $pwcallback, $stringfilemap);
132 sub print_asciidoc_synopsys
{
134 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
136 my $pwcallback = $cli_handler_class->can('read_password');
137 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
141 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
144 foreach my $cmd (sort keys %$cmddef) {
145 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
146 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
147 $uri_param, 'asciidoc', $pwcallback,
149 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
151 $synopsis .= "$str\n\n";
160 sub print_simple_pod_manpage
{
161 my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
163 die "not initialized" if !$cli_handler_class;
165 my $pwcallback = $cli_handler_class->can('read_password');
166 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
168 my $synopsis = " $name help\n\n";
169 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
174 my $parser = PVE
::PodParser-
>new();
175 $parser->{include
}->{synopsis
} = $synopsis;
176 $parser->parse_from_file($podfn);
179 sub print_pod_manpage
{
182 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
183 die "no pod file specified" if !$podfn;
185 my $pwcallback = $cli_handler_class->can('read_password');
186 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
190 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
192 my $style = 'full'; # or should we use 'short'?
194 foreach my $cmd (sorted_commands
()) {
195 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
196 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
197 $uri_param, $style, $pwcallback,
199 $str =~ s/^USAGE: //;
201 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
203 $synopsis .= " $str\n\n";
209 my $parser = PVE
::PodParser-
>new();
210 $parser->{include
}->{synopsis
} = $synopsis;
211 $parser->parse_from_file($podfn);
214 sub print_usage_verbose
{
216 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
218 my $pwcallback = $cli_handler_class->can('read_password');
219 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
221 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
223 foreach my $cmd (sort keys %$cmddef) {
224 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
225 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
226 'full', $pwcallback, $stringfilemap);
231 sub sorted_commands
{
232 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
235 sub print_usage_short
{
238 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
240 my $pwcallback = $cli_handler_class->can('read_password');
241 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
243 print $fd "ERROR: $msg\n" if $msg;
244 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
247 foreach my $cmd (sorted_commands
()) {
248 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
249 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
250 print $fd "\n" if $oldclass && $oldclass ne $class;
256 my $print_bash_completion = sub {
257 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
261 return if !(defined($cur) && defined($prev) && defined($bash_command));
262 return if !defined($ENV{COMP_LINE
});
263 return if !defined($ENV{COMP_POINT
});
265 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
266 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
268 my $args = PVE
::Tools
::split_args
($cmdline);
269 my $pos = scalar(@$args) - 2;
270 $pos += 1 if $cmdline =~ m/\s+$/;
272 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
276 my $print_result = sub {
278 print "$p\n" if $p =~ m/^$cur/;
287 &$print_result(keys %$cmddef);
293 my $def = $cmddef->{$cmd};
296 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
300 my ($class, $name, $arg_param, $uri_param) = @$def;
304 $arg_param = [ $arg_param ] if !ref($arg_param);
306 map { $skip_param->{$_} = 1; } @$arg_param;
307 map { $skip_param->{$_} = 1; } keys %$uri_param;
309 my $fpcount = scalar(@$arg_param);
311 my $info = $class->map_method_by_name($name);
313 my $schema = $info->{parameters
};
314 my $prop = $schema->{properties
};
316 my $print_parameter_completion = sub {
318 my $d = $prop->{$pname};
319 if ($d->{completion
}) {
320 my $vt = ref($d->{completion
});
322 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
323 &$print_result(@$res);
325 } elsif ($d->{type
} eq 'boolean') {
326 &$print_result('0', '1');
327 } elsif ($d->{enum
}) {
328 &$print_result(@{$d->{enum
}});
332 # positional arguments
333 $pos += 1 if $simple_cmd;
334 if ($fpcount && $pos <= $fpcount) {
335 my $pname = $arg_param->[$pos -1];
336 &$print_parameter_completion($pname);
340 my @option_list = ();
341 foreach my $key (keys %$prop) {
342 next if $skip_param->{$key};
343 push @option_list, "--$key";
347 &$print_result(@option_list);
351 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
353 &$print_parameter_completion($pname);
357 &$print_result(@option_list);
363 # simply verify all registered methods
364 PVE
::RESTHandler
::validate_method_schemas
();
367 my $get_exe_name = sub {
377 sub generate_bash_completions
{
380 # generate bash completion config
382 $exename = &$get_exe_name($class);
385 # $exename bash completion
387 # see http://tiswww.case.edu/php/chet/bash/FAQ
388 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
389 # this modifies global var, but I found no better way
390 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
392 complete -o default -C '$exename bashcomplete' $exename
396 sub find_cli_class_source
{
403 my $cpath = "PVE/CLI/${name}.pm";
404 my $spath = "PVE/Service/${name}.pm";
405 foreach my $p (@INC) {
406 foreach my $s (($cpath, $spath)) {
407 my $testfn = "$p/$s";
413 last if defined($filename);
419 sub generate_pod_manpage
{
420 my ($class, $podfn) = @_;
422 $cli_handler_class = $class;
424 $exename = &$get_exe_name($class);
426 $podfn = find_cli_class_source
($exename) if !defined($podfn);
428 die "unable to find source for class '$class'" if !$podfn;
431 my $def = ${"${class}::cmddef"};
433 if (ref($def) eq 'ARRAY') {
434 print_simple_pod_manpage
($podfn, @$def);
438 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
440 print_pod_manpage
($podfn);
444 sub generate_asciidoc_synopsys
{
447 $cli_handler_class = $class;
449 $exename = &$get_exe_name($class);
452 my $def = ${"${class}::cmddef"};
454 if (ref($def) eq 'ARRAY') {
455 print_simple_asciidoc_synopsys
(@$def);
459 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
461 print_asciidoc_synopsys
();
465 my $handle_cmd = sub {
466 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc, $stringfilemap) = @_;
471 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
474 print_usage_short
(\
*STDERR
, "no command specified");
476 } elsif ($cmd eq 'verifyapi') {
477 PVE
::RESTHandler
::validate_method_schemas
();
479 } elsif ($cmd eq 'printmanpod') {
480 $podfn = find_cli_class_source
($exename) if !defined($podfn);
481 print_pod_manpage
($podfn);
483 } elsif ($cmd eq 'bashcomplete') {
484 &$print_bash_completion($cmddef, 0, @$args);
488 &$preparefunc() if $preparefunc;
490 $cmd = &$expand_command_name($cmddef, $cmd);
492 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
495 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
499 my $prefix = "$exename $cmd";
500 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
502 &$outsub($res) if $outsub;
505 my $handle_simple_cmd = sub {
506 my ($def, $args, $pwcallback, $podfn, $preparefunc, $stringfilemap) = @_;
508 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
509 die "no class specified" if !$class;
511 if (scalar(@$args) >= 1) {
512 if ($args->[0] eq 'help') {
513 my $str = "USAGE: $name help\n";
514 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
515 print STDERR
"$str\n\n";
517 } elsif ($args->[0] eq 'bashcomplete') {
519 &$print_bash_completion({ $name => $def }, $name, @$args);
521 } elsif ($args->[0] eq 'verifyapi') {
522 PVE
::RESTHandler
::validate_method_schemas
();
524 } elsif ($args->[0] eq 'printmanpod') {
525 $podfn = find_cli_class_source
($name) if !defined($podfn);
526 print_simple_pod_manpage
($podfn, @$def);
531 &$preparefunc() if $preparefunc;
533 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
535 &$outsub($res) if $outsub;
539 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
541 # Note: "depreciated function run_cli - use run_cli_handler instead";
543 die "password callback is no longer supported" if $pwcallback;
545 run_cli_handler
($class, podfn
=> $podfn, prepare
=> $preparefunc);
548 sub run_cli_handler
{
549 my ($class, %params) = @_;
551 $cli_handler_class = $class;
553 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
555 foreach my $key (keys %params) {
556 next if $key eq 'podfn';
557 next if $key eq 'prepare';
558 next if $key eq 'no_init'; # used by lxc hooks
559 die "unknown parameter '$key'";
562 my $podfn = $params{podfn
};
563 my $preparefunc = $params{prepare
};
564 my $no_init = $params{no_init
};
566 my $pwcallback = $class->can('read_password');
567 my $stringfilemap = $class->can('string_param_file_mapping');
569 $exename = &$get_exe_name($class);
573 if ($class !~ m/^PVE::Service::/) {
574 die "please run as root\n" if $> != 0;
576 PVE
::INotify
::inotify_init
() if !$no_init;
578 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
579 $rpcenv->init_request() if !$no_init;
580 $rpcenv->set_language($ENV{LANG
});
581 $rpcenv->set_user('root@pam');
585 my $def = ${"${class}::cmddef"};
587 if (ref($def) eq 'ARRAY') {
588 &$handle_simple_cmd($def, \
@ARGV, $pwcallback, $podfn, $preparefunc, $stringfilemap);
591 my $cmd = shift @ARGV;
592 &$handle_cmd($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc, $stringfilemap);