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');
100 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
102 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
103 $verbose ?
'full' : 'short', $pwcallback,
108 print "USAGE: $str\n";
115 sub print_simple_asciidoc_synopsys
{
116 my ($class, $name, $arg_param, $uri_param) = @_;
118 die "not initialized" if !$cli_handler_class;
120 my $pwcallback = $cli_handler_class->can('read_password');
121 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
123 my $synopsis = "*${name}* `help`\n\n";
125 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param,
126 'asciidoc', $pwcallback, $stringfilemap);
131 sub print_asciidoc_synopsys
{
133 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
135 my $pwcallback = $cli_handler_class->can('read_password');
136 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
140 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
143 foreach my $cmd (sort keys %$cmddef) {
144 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
145 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
146 $uri_param, 'asciidoc', $pwcallback,
148 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
150 $synopsis .= "$str\n\n";
159 sub print_usage_verbose
{
161 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
163 my $pwcallback = $cli_handler_class->can('read_password');
164 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
166 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
168 foreach my $cmd (sort keys %$cmddef) {
169 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
170 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
171 'full', $pwcallback, $stringfilemap);
176 sub sorted_commands
{
177 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
180 sub print_usage_short
{
183 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
185 my $pwcallback = $cli_handler_class->can('read_password');
186 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
188 print $fd "ERROR: $msg\n" if $msg;
189 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
192 foreach my $cmd (sorted_commands
()) {
193 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
194 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
195 print $fd "\n" if $oldclass && $oldclass ne $class;
201 my $print_bash_completion = sub {
202 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
206 return if !(defined($cur) && defined($prev) && defined($bash_command));
207 return if !defined($ENV{COMP_LINE
});
208 return if !defined($ENV{COMP_POINT
});
210 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
211 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
213 my $args = PVE
::Tools
::split_args
($cmdline);
214 my $pos = scalar(@$args) - 2;
215 $pos += 1 if $cmdline =~ m/\s+$/;
217 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
221 my $print_result = sub {
223 print "$p\n" if $p =~ m/^$cur/;
232 &$print_result(keys %$cmddef);
238 my $def = $cmddef->{$cmd};
241 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
245 my ($class, $name, $arg_param, $uri_param) = @$def;
249 $arg_param = [ $arg_param ] if !ref($arg_param);
251 map { $skip_param->{$_} = 1; } @$arg_param;
252 map { $skip_param->{$_} = 1; } keys %$uri_param;
254 my $fpcount = scalar(@$arg_param);
256 my $info = $class->map_method_by_name($name);
258 my $schema = $info->{parameters
};
259 my $prop = $schema->{properties
};
261 my $print_parameter_completion = sub {
263 my $d = $prop->{$pname};
264 if ($d->{completion
}) {
265 my $vt = ref($d->{completion
});
267 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
268 &$print_result(@$res);
270 } elsif ($d->{type
} eq 'boolean') {
271 &$print_result('0', '1');
272 } elsif ($d->{enum
}) {
273 &$print_result(@{$d->{enum
}});
277 # positional arguments
278 $pos += 1 if $simple_cmd;
279 if ($fpcount && $pos <= $fpcount) {
280 my $pname = $arg_param->[$pos -1];
281 &$print_parameter_completion($pname);
285 my @option_list = ();
286 foreach my $key (keys %$prop) {
287 next if $skip_param->{$key};
288 push @option_list, "--$key";
292 &$print_result(@option_list);
296 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
298 &$print_parameter_completion($pname);
302 &$print_result(@option_list);
308 # simply verify all registered methods
309 PVE
::RESTHandler
::validate_method_schemas
();
312 my $get_exe_name = sub {
322 sub generate_bash_completions
{
325 # generate bash completion config
327 $exename = &$get_exe_name($class);
330 # $exename bash completion
332 # see http://tiswww.case.edu/php/chet/bash/FAQ
333 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
334 # this modifies global var, but I found no better way
335 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
337 complete -o default -C '$exename bashcomplete' $exename
341 sub find_cli_class_source
{
348 my $cpath = "PVE/CLI/${name}.pm";
349 my $spath = "PVE/Service/${name}.pm";
350 foreach my $p (@INC) {
351 foreach my $s (($cpath, $spath)) {
352 my $testfn = "$p/$s";
358 last if defined($filename);
364 sub generate_asciidoc_synopsys
{
367 $cli_handler_class = $class;
369 $exename = &$get_exe_name($class);
372 my $def = ${"${class}::cmddef"};
374 if (ref($def) eq 'ARRAY') {
375 print_simple_asciidoc_synopsys
(@$def);
379 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
381 print_asciidoc_synopsys
();
385 my $handle_cmd = sub {
386 my ($def, $cmdname, $cmd, $args, $pwcallback, $preparefunc, $stringfilemap) = @_;
391 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
394 print_usage_short
(\
*STDERR
, "no command specified");
396 } elsif ($cmd eq 'verifyapi') {
397 PVE
::RESTHandler
::validate_method_schemas
();
399 } elsif ($cmd eq 'bashcomplete') {
400 &$print_bash_completion($cmddef, 0, @$args);
404 &$preparefunc() if $preparefunc;
406 $cmd = &$expand_command_name($cmddef, $cmd);
408 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
411 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
415 my $prefix = "$exename $cmd";
416 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
418 &$outsub($res) if $outsub;
421 my $handle_simple_cmd = sub {
422 my ($def, $args, $pwcallback, $preparefunc, $stringfilemap) = @_;
424 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
425 die "no class specified" if !$class;
427 if (scalar(@$args) >= 1) {
428 if ($args->[0] eq 'help') {
429 my $str = "USAGE: $name help\n";
430 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
431 print STDERR
"$str\n\n";
433 } elsif ($args->[0] eq 'bashcomplete') {
435 &$print_bash_completion({ $name => $def }, $name, @$args);
437 } elsif ($args->[0] eq 'verifyapi') {
438 PVE
::RESTHandler
::validate_method_schemas
();
443 &$preparefunc() if $preparefunc;
445 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
447 &$outsub($res) if $outsub;
451 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
453 # Note: "depreciated function run_cli - use run_cli_handler instead";
454 # silently ignore $podfn , which is no longer supported.
456 die "password callback is no longer supported" if $pwcallback;
458 run_cli_handler
($class, prepare
=> $preparefunc);
461 sub run_cli_handler
{
462 my ($class, %params) = @_;
464 $cli_handler_class = $class;
466 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
468 foreach my $key (keys %params) {
469 next if $key eq 'prepare';
470 next if $key eq 'no_init'; # used by lxc hooks
471 next if $key eq 'no_rpcenv';
472 die "unknown parameter '$key'";
475 my $preparefunc = $params{prepare
};
476 my $no_init = $params{no_init
};
477 my $no_rpcenv = $params{no_rpcenv
};
479 my $pwcallback = $class->can('read_password');
480 my $stringfilemap = $class->can('string_param_file_mapping');
482 $exename = &$get_exe_name($class);
486 if ($class !~ m/^PVE::Service::/) {
487 die "please run as root\n" if $> != 0;
489 PVE
::INotify
::inotify_init
() if !$no_init;
492 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
493 $rpcenv->init_request() if !$no_init;
494 $rpcenv->set_language($ENV{LANG
});
495 $rpcenv->set_user('root@pam');
500 my $def = ${"${class}::cmddef"};
502 if (ref($def) eq 'ARRAY') {
503 &$handle_simple_cmd($def, \
@ARGV, $pwcallback, $preparefunc, $stringfilemap);
506 my $cmd = shift @ARGV;
507 &$handle_cmd($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $preparefunc, $stringfilemap);