1 package PVE
::CLIHandler
;
8 use PVE
::Exception
qw(raise raise_param_exc);
12 use base
qw(PVE::RESTHandler);
16 my $cli_handler_class;
18 my $expand_command_name = sub {
23 for my $k (keys(%$def)) {
26 $expanded = undef; # more than one match
33 $cmd = $expanded if $expanded;
38 my $complete_command_names = sub {
41 return if ref($cmddef) ne 'HASH';
43 foreach my $cmd (keys %$cmddef) {
44 next if $cmd eq 'help';
51 __PACKAGE__-
>register_method ({
55 description
=> "Get help about specified command.",
57 additionalProperties
=> 0,
60 description
=> "Command name",
63 completion
=> $complete_command_names,
66 description
=> "Verbose output format.",
72 returns
=> { type
=> 'null' },
77 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
79 my $cmd = $param->{cmd
};
81 my $verbose = defined($cmd) && $cmd;
82 $verbose = $param->{verbose
} if defined($param->{verbose
});
86 print_usage_verbose
();
88 print_usage_short
(\
*STDOUT
);
93 $cmd = &$expand_command_name($cmddef, $cmd);
95 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
97 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
99 my $pwcallback = $cli_handler_class->can('read_password');
101 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
102 $verbose ?
'full' : 'short', $pwcallback);
106 print "USAGE: $str\n";
113 sub print_simple_asciidoc_synopsys
{
114 my ($class, $name, $arg_param, $uri_param) = @_;
116 die "not initialized" if !$cli_handler_class;
118 my $pwcallback = $cli_handler_class->can('read_password');
120 my $synopsis = "*${name}* `help`\n\n";
122 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param, 'asciidoc', $pwcallback);
127 sub print_asciidoc_synopsys
{
129 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
131 my $pwcallback = $cli_handler_class->can('read_password');
135 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
138 foreach my $cmd (sort keys %$cmddef) {
139 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
140 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
141 $uri_param, 'asciidoc', $pwcallback);
142 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
144 $synopsis .= "$str\n\n";
153 sub print_simple_pod_manpage
{
154 my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
156 die "not initialized" if !$cli_handler_class;
158 my $pwcallback = $cli_handler_class->can('read_password');
160 my $synopsis = " $name help\n\n";
161 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
166 my $parser = PVE
::PodParser-
>new();
167 $parser->{include
}->{synopsis
} = $synopsis;
168 $parser->parse_from_file($podfn);
171 sub print_pod_manpage
{
174 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
175 die "no pod file specified" if !$podfn;
177 my $pwcallback = $cli_handler_class->can('read_password');
181 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
183 my $style = 'full'; # or should we use 'short'?
185 foreach my $cmd (sorted_commands
()) {
186 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
187 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
188 $uri_param, $style, $pwcallback);
189 $str =~ s/^USAGE: //;
191 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
193 $synopsis .= " $str\n\n";
199 my $parser = PVE
::PodParser-
>new();
200 $parser->{include
}->{synopsis
} = $synopsis;
201 $parser->parse_from_file($podfn);
204 sub print_usage_verbose
{
206 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
208 my $pwcallback = $cli_handler_class->can('read_password');
210 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
212 foreach my $cmd (sort keys %$cmddef) {
213 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
214 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
215 'full', $pwcallback);
220 sub sorted_commands
{
221 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
224 sub print_usage_short
{
227 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
229 my $pwcallback = $cli_handler_class->can('read_password');
231 print $fd "ERROR: $msg\n" if $msg;
232 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
235 foreach my $cmd (sorted_commands
()) {
236 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
237 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback);
238 print $fd "\n" if $oldclass && $oldclass ne $class;
244 my $print_bash_completion = sub {
245 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
249 return if !(defined($cur) && defined($prev) && defined($bash_command));
250 return if !defined($ENV{COMP_LINE
});
251 return if !defined($ENV{COMP_POINT
});
253 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
254 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
256 my $args = PVE
::Tools
::split_args
($cmdline);
257 my $pos = scalar(@$args) - 2;
258 $pos += 1 if $cmdline =~ m/\s+$/;
260 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
264 my $print_result = sub {
266 print "$p\n" if $p =~ m/^$cur/;
275 &$print_result(keys %$cmddef);
281 my $def = $cmddef->{$cmd};
284 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
288 my ($class, $name, $arg_param, $uri_param) = @$def;
292 $arg_param = [ $arg_param ] if !ref($arg_param);
294 map { $skip_param->{$_} = 1; } @$arg_param;
295 map { $skip_param->{$_} = 1; } keys %$uri_param;
297 my $fpcount = scalar(@$arg_param);
299 my $info = $class->map_method_by_name($name);
301 my $schema = $info->{parameters
};
302 my $prop = $schema->{properties
};
304 my $print_parameter_completion = sub {
306 my $d = $prop->{$pname};
307 if ($d->{completion
}) {
308 my $vt = ref($d->{completion
});
310 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
311 &$print_result(@$res);
313 } elsif ($d->{type
} eq 'boolean') {
314 &$print_result('0', '1');
315 } elsif ($d->{enum
}) {
316 &$print_result(@{$d->{enum
}});
320 # positional arguments
321 $pos += 1 if $simple_cmd;
322 if ($fpcount && $pos <= $fpcount) {
323 my $pname = $arg_param->[$pos -1];
324 &$print_parameter_completion($pname);
328 my @option_list = ();
329 foreach my $key (keys %$prop) {
330 next if $skip_param->{$key};
331 push @option_list, "--$key";
335 &$print_result(@option_list);
339 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
341 &$print_parameter_completion($pname);
345 &$print_result(@option_list);
351 # simply verify all registered methods
352 PVE
::RESTHandler
::validate_method_schemas
();
355 my $get_exe_name = sub {
365 sub generate_bash_completions
{
368 # generate bash completion config
370 $exename = &$get_exe_name($class);
373 # $exename bash completion
375 # see http://tiswww.case.edu/php/chet/bash/FAQ
376 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
377 # this modifies global var, but I found no better way
378 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
380 complete -o default -C '$exename bashcomplete' $exename
384 sub find_cli_class_source
{
391 my $cpath = "PVE/CLI/${name}.pm";
392 my $spath = "PVE/Service/${name}.pm";
393 foreach my $p (@INC) {
394 foreach my $s (($cpath, $spath)) {
395 my $testfn = "$p/$s";
401 last if defined($filename);
407 sub generate_pod_manpage
{
408 my ($class, $podfn) = @_;
410 $cli_handler_class = $class;
412 $exename = &$get_exe_name($class);
414 $podfn = find_cli_class_source
($exename) if !defined($podfn);
416 die "unable to find source for class '$class'" if !$podfn;
419 my $def = ${"${class}::cmddef"};
421 if (ref($def) eq 'ARRAY') {
422 print_simple_pod_manpage
($podfn, @$def);
426 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
428 print_pod_manpage
($podfn);
432 sub generate_asciidoc_synopsys
{
435 $cli_handler_class = $class;
437 $exename = &$get_exe_name($class);
440 my $def = ${"${class}::cmddef"};
442 if (ref($def) eq 'ARRAY') {
443 print_simple_asciidoc_synopsys
(@$def);
447 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
449 print_asciidoc_synopsys
();
453 my $handle_cmd = sub {
454 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
459 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
462 print_usage_short
(\
*STDERR
, "no command specified");
464 } elsif ($cmd eq 'verifyapi') {
465 PVE
::RESTHandler
::validate_method_schemas
();
467 } elsif ($cmd eq 'printmanpod') {
468 $podfn = find_cli_class_source
($exename) if !defined($podfn);
469 print_pod_manpage
($podfn);
471 } elsif ($cmd eq 'bashcomplete') {
472 &$print_bash_completion($cmddef, 0, @$args);
476 &$preparefunc() if $preparefunc;
478 $cmd = &$expand_command_name($cmddef, $cmd);
480 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
483 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
487 my $prefix = "$exename $cmd";
488 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
490 &$outsub($res) if $outsub;
493 my $handle_simple_cmd = sub {
494 my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_;
496 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
497 die "no class specified" if !$class;
499 if (scalar(@$args) >= 1) {
500 if ($args->[0] eq 'help') {
501 my $str = "USAGE: $name help\n";
502 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
503 print STDERR
"$str\n\n";
505 } elsif ($args->[0] eq 'bashcomplete') {
507 &$print_bash_completion({ $name => $def }, $name, @$args);
509 } elsif ($args->[0] eq 'verifyapi') {
510 PVE
::RESTHandler
::validate_method_schemas
();
512 } elsif ($args->[0] eq 'printmanpod') {
513 $podfn = find_cli_class_source
($name) if !defined($podfn);
514 print_simple_pod_manpage
($podfn, @$def);
519 &$preparefunc() if $preparefunc;
521 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
523 &$outsub($res) if $outsub;
527 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
529 # Note: "depreciated function run_cli - use run_cli_handler instead";
531 die "password callback is no longer supported" if $pwcallback;
533 run_cli_handler
($class, podfn
=> $podfn, prepare
=> $preparefunc);
536 sub run_cli_handler
{
537 my ($class, %params) = @_;
539 $cli_handler_class = $class;
541 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
543 foreach my $key (keys %params) {
544 next if $key eq 'podfn';
545 next if $key eq 'prepare';
546 next if $key eq 'no_init'; # used by lxc hooks
547 die "unknown parameter '$key'";
550 my $podfn = $params{podfn
};
551 my $preparefunc = $params{prepare
};
552 my $no_init = $params{no_init
};
554 my $pwcallback = $class->can('read_password');
556 $exename = &$get_exe_name($class);
560 if ($class !~ m/^PVE::Service::/) {
561 die "please run as root\n" if $> != 0;
563 PVE
::INotify
::inotify_init
() if !$no_init;
565 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
566 $rpcenv->init_request() if !$no_init;
567 $rpcenv->set_language($ENV{LANG
});
568 $rpcenv->set_user('root@pam');
572 my $def = ${"${class}::cmddef"};
574 if (ref($def) eq 'ARRAY') {
575 &$handle_simple_cmd($def, \
@ARGV, $pwcallback, $podfn, $preparefunc);
578 my $cmd = shift @ARGV;
579 &$handle_cmd($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc);