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_pod_manpage
{
114 my ($podfn, $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";
121 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
126 my $parser = PVE
::PodParser-
>new();
127 $parser->{include
}->{synopsis
} = $synopsis;
128 $parser->parse_from_file($podfn);
131 sub print_pod_manpage
{
134 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
135 die "no pod file specified" if !$podfn;
137 my $pwcallback = $cli_handler_class->can('read_password');
141 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
143 my $style = 'full'; # or should we use 'short'?
145 foreach my $cmd (sorted_commands
()) {
146 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
147 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
148 $uri_param, $style, $pwcallback);
149 $str =~ s/^USAGE: //;
151 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
153 $synopsis .= " $str\n\n";
159 my $parser = PVE
::PodParser-
>new();
160 $parser->{include
}->{synopsis
} = $synopsis;
161 $parser->parse_from_file($podfn);
164 sub print_usage_verbose
{
166 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
168 my $pwcallback = $cli_handler_class->can('read_password');
170 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
172 foreach my $cmd (sort keys %$cmddef) {
173 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
174 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
175 'full', $pwcallback);
180 sub sorted_commands
{
181 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
184 sub print_usage_short
{
187 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
189 my $pwcallback = $cli_handler_class->can('read_password');
191 print $fd "ERROR: $msg\n" if $msg;
192 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
195 foreach my $cmd (sorted_commands
()) {
196 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
197 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback);
198 print $fd "\n" if $oldclass && $oldclass ne $class;
204 my $print_bash_completion = sub {
205 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
209 return if !(defined($cur) && defined($prev) && defined($bash_command));
210 return if !defined($ENV{COMP_LINE
});
211 return if !defined($ENV{COMP_POINT
});
213 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
214 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
216 my $args = PVE
::Tools
::split_args
($cmdline);
217 my $pos = scalar(@$args) - 2;
218 $pos += 1 if $cmdline =~ m/\s+$/;
220 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
224 my $print_result = sub {
226 print "$p\n" if $p =~ m/^$cur/;
235 &$print_result(keys %$cmddef);
241 my $def = $cmddef->{$cmd};
244 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
248 my ($class, $name, $arg_param, $uri_param) = @$def;
252 $arg_param = [ $arg_param ] if !ref($arg_param);
254 map { $skip_param->{$_} = 1; } @$arg_param;
255 map { $skip_param->{$_} = 1; } keys %$uri_param;
257 my $fpcount = scalar(@$arg_param);
259 my $info = $class->map_method_by_name($name);
261 my $schema = $info->{parameters
};
262 my $prop = $schema->{properties
};
264 my $print_parameter_completion = sub {
266 my $d = $prop->{$pname};
267 if ($d->{completion
}) {
268 my $vt = ref($d->{completion
});
270 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
271 &$print_result(@$res);
273 } elsif ($d->{type
} eq 'boolean') {
274 &$print_result('0', '1');
275 } elsif ($d->{enum
}) {
276 &$print_result(@{$d->{enum
}});
280 # positional arguments
281 $pos += 1 if $simple_cmd;
282 if ($fpcount && $pos <= $fpcount) {
283 my $pname = $arg_param->[$pos -1];
284 &$print_parameter_completion($pname);
288 my @option_list = ();
289 foreach my $key (keys %$prop) {
290 next if $skip_param->{$key};
291 push @option_list, "--$key";
295 &$print_result(@option_list);
299 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
301 &$print_parameter_completion($pname);
305 &$print_result(@option_list);
311 # simply verify all registered methods
312 PVE
::RESTHandler
::validate_method_schemas
();
315 my $get_exe_name = sub {
325 sub generate_bash_completions
{
328 # generate bash completion config
330 $exename = &$get_exe_name($class);
333 # $exename bash completion
335 # see http://tiswww.case.edu/php/chet/bash/FAQ
336 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
337 # this modifies global var, but I found no better way
338 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
340 complete -C '$exename bashcomplete' $exename
344 sub find_cli_class_source
{
351 my $cpath = "PVE/CLI/${name}.pm";
352 my $spath = "PVE/Service/${name}.pm";
353 foreach my $p (@INC) {
354 foreach my $s (($cpath, $spath)) {
355 my $testfn = "$p/$s";
361 last if defined($filename);
367 sub generate_pod_manpage
{
368 my ($class, $podfn) = @_;
370 $cli_handler_class = $class;
372 $exename = &$get_exe_name($class);
374 $podfn = find_cli_class_source
($exename) if !defined($podfn);
376 die "unable to find source for class '$class'" if !$podfn;
379 my $def = ${"${class}::cmddef"};
381 if (ref($def) eq 'ARRAY') {
382 print_simple_pod_manpage
($podfn, @$def);
386 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
388 print_pod_manpage
($podfn);
392 my $handle_cmd = sub {
393 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
398 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
401 print_usage_short
(\
*STDERR
, "no command specified");
403 } elsif ($cmd eq 'verifyapi') {
404 PVE
::RESTHandler
::validate_method_schemas
();
406 } elsif ($cmd eq 'printmanpod') {
407 $podfn = find_cli_class_source
($exename) if !defined($podfn);
408 print_pod_manpage
($podfn);
410 } elsif ($cmd eq 'bashcomplete') {
411 &$print_bash_completion($cmddef, 0, @$args);
415 &$preparefunc() if $preparefunc;
417 $cmd = &$expand_command_name($cmddef, $cmd);
419 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
422 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
426 my $prefix = "$exename $cmd";
427 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
429 &$outsub($res) if $outsub;
432 my $handle_simple_cmd = sub {
433 my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_;
435 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
436 die "no class specified" if !$class;
438 if (scalar(@$args) >= 1) {
439 if ($args->[0] eq 'help') {
440 my $str = "USAGE: $name help\n";
441 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
442 print STDERR
"$str\n\n";
444 } elsif ($args->[0] eq 'bashcomplete') {
446 &$print_bash_completion({ $name => $def }, $name, @$args);
448 } elsif ($args->[0] eq 'verifyapi') {
449 PVE
::RESTHandler
::validate_method_schemas
();
451 } elsif ($args->[0] eq 'printmanpod') {
452 $podfn = find_cli_class_source
($name) if !defined($podfn);
453 print_simple_pod_manpage
($podfn, @$def);
458 &$preparefunc() if $preparefunc;
460 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
462 &$outsub($res) if $outsub;
466 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
468 # Note: "depreciated function run_cli - use run_cli_handler instead";
470 die "password callback is no longer supported" if $pwcallback;
472 run_cli_handler
($class, podfn
=> $podfn, prepare
=> $preparefunc);
475 sub run_cli_handler
{
476 my ($class, %params) = @_;
478 $cli_handler_class = $class;
480 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
482 foreach my $key (keys %params) {
483 next if $key eq 'podfn';
484 next if $key eq 'prepare';
485 next if $key eq 'no_init'; # used by lxc hooks
486 die "unknown parameter '$key'";
489 my $podfn = $params{podfn
};
490 my $preparefunc = $params{prepare
};
491 my $no_init = $params{no_init
};
493 my $pwcallback = $class->can('read_password');
495 $exename = &$get_exe_name($class);
499 if ($class !~ m/^PVE::Service::/) {
500 die "please run as root\n" if $> != 0;
502 PVE
::INotify
::inotify_init
() if !$no_init;
504 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
505 $rpcenv->init_request() if !$no_init;
506 $rpcenv->set_language($ENV{LANG
});
507 $rpcenv->set_user('root@pam');
511 my $def = ${"${class}::cmddef"};
513 if (ref($def) eq 'ARRAY') {
514 &$handle_simple_cmd($def, \
@ARGV, $pwcallback, $podfn, $preparefunc);
517 my $cmd = shift @ARGV;
518 &$handle_cmd($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc);