1 package PVE
::CLIHandler
;
8 use PVE
::Exception
qw(raise raise_param_exc);
12 use base
qw(PVE::RESTHandler);
17 my $expand_command_name = sub {
22 for my $k (keys(%$def)) {
25 $expanded = undef; # more than one match
32 $cmd = $expanded if $expanded;
37 my $complete_command_names = sub {
40 return if ref($cmddef) ne 'HASH';
42 foreach my $cmd (keys %$cmddef) {
43 next if $cmd eq 'help';
50 __PACKAGE__-
>register_method ({
54 description
=> "Get help about specified command.",
56 additionalProperties
=> 0,
59 description
=> "Command name",
62 completion
=> $complete_command_names,
65 description
=> "Verbose output format.",
71 returns
=> { type
=> 'null' },
76 die "not initialized" if !($cmddef && $exename);
78 my $cmd = $param->{cmd
};
80 my $verbose = defined($cmd) && $cmd;
81 $verbose = $param->{verbose
} if defined($param->{verbose
});
85 print_usage_verbose
();
87 print_usage_short
(\
*STDOUT
);
92 $cmd = &$expand_command_name($cmddef, $cmd);
94 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
96 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
99 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ?
'full' : 'short');
103 print "USAGE: $str\n";
110 sub print_simple_pod_manpage
{
111 my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
113 my $synopsis = " $name help\n\n";
114 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
119 my $parser = PVE
::PodParser-
>new();
120 $parser->{include
}->{synopsis
} = $synopsis;
121 $parser->parse_from_file($podfn);
124 sub print_pod_manpage
{
127 die "not initialized" if !($cmddef && $exename);
128 die "no pod file specified" if !$podfn;
132 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
134 my $style = 'full'; # or should we use 'short'?
136 foreach my $cmd (sorted_commands
()) {
137 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
138 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
140 $str =~ s/^USAGE: //;
142 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
144 $synopsis .= " $str\n\n";
150 my $parser = PVE
::PodParser-
>new();
151 $parser->{include
}->{synopsis
} = $synopsis;
152 $parser->parse_from_file($podfn);
155 sub print_usage_verbose
{
157 die "not initialized" if !($cmddef && $exename);
159 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
161 foreach my $cmd (sort keys %$cmddef) {
162 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
163 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full');
168 sub sorted_commands
{
169 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
172 sub print_usage_short
{
175 die "not initialized" if !($cmddef && $exename);
177 print $fd "ERROR: $msg\n" if $msg;
178 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
181 foreach my $cmd (sorted_commands
()) {
182 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
183 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short');
184 print $fd "\n" if $oldclass && $oldclass ne $class;
190 my $print_bash_completion = sub {
191 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
195 return if !(defined($cur) && defined($prev) && defined($bash_command));
196 return if !defined($ENV{COMP_LINE
});
197 return if !defined($ENV{COMP_POINT
});
199 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
200 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
202 my $args = PVE
::Tools
::split_args
($cmdline);
203 my $pos = scalar(@$args) - 2;
204 $pos += 1 if $cmdline =~ m/\s+$/;
206 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
210 my $print_result = sub {
212 print "$p\n" if $p =~ m/^$cur/;
221 &$print_result(keys %$cmddef);
227 my $def = $cmddef->{$cmd};
230 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
234 my ($class, $name, $arg_param, $uri_param) = @$def;
238 $arg_param = [ $arg_param ] if !ref($arg_param);
240 map { $skip_param->{$_} = 1; } @$arg_param;
241 map { $skip_param->{$_} = 1; } keys %$uri_param;
243 my $fpcount = scalar(@$arg_param);
245 my $info = $class->map_method_by_name($name);
247 my $schema = $info->{parameters
};
248 my $prop = $schema->{properties
};
250 my $print_parameter_completion = sub {
252 my $d = $prop->{$pname};
253 if ($d->{completion
}) {
254 my $vt = ref($d->{completion
});
256 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
257 &$print_result(@$res);
259 } elsif ($d->{type
} eq 'boolean') {
260 &$print_result('0', '1');
261 } elsif ($d->{enum
}) {
262 &$print_result(@{$d->{enum
}});
266 # positional arguments
267 $pos += 1 if $simple_cmd;
268 if ($fpcount && $pos <= $fpcount) {
269 my $pname = $arg_param->[$pos -1];
270 &$print_parameter_completion($pname);
274 my @option_list = ();
275 foreach my $key (keys %$prop) {
276 next if $skip_param->{$key};
277 push @option_list, "--$key";
281 &$print_result(@option_list);
285 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
287 &$print_parameter_completion($pname);
291 &$print_result(@option_list);
297 # simply verify all registered methods
298 PVE
::RESTHandler
::validate_method_schemas
();
301 my $get_exe_name = sub {
311 sub generate_bash_completions
{
314 # generate bash completion config
316 $exename = &$get_exe_name($class);
319 # $exename bash completion
321 # see http://tiswww.case.edu/php/chet/bash/FAQ
322 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
323 # this modifies global var, but I found no better way
324 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
326 complete -C '$exename bashcomplete' $exename
330 sub find_cli_class_source
{
337 my $cpath = "PVE/CLI/${name}.pm";
338 my $spath = "PVE/Service/${name}.pm";
339 foreach my $p (@INC) {
340 foreach my $s (($cpath, $spath)) {
341 my $testfn = "$p/$s";
347 last if defined($filename);
353 sub generate_pod_manpage
{
354 my ($class, $podfn) = @_;
356 $exename = &$get_exe_name($class);
358 $podfn = find_cli_class_source
($exename) if !defined($podfn);
360 die "unable to find source for class '$class'" if !$podfn;
363 my $def = ${"${class}::cmddef"};
365 if (ref($def) eq 'ARRAY') {
366 print_simple_pod_manpage
($podfn, @$def);
370 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
372 print_pod_manpage
($podfn);
377 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
379 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
381 $exename = &$get_exe_name($class);
386 if ($class !~ m/^PVE::Service::/) {
387 die "please run as root\n" if $> != 0;
389 PVE
::INotify
::inotify_init
();
391 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
392 $rpcenv->init_request();
393 $rpcenv->set_language($ENV{LANG
});
394 $rpcenv->set_user('root@pam');
398 my $def = ${"${class}::cmddef"};
400 if (ref($def) eq 'ARRAY') {
401 handle_simple_cmd
($def, \
@ARGV, $pwcallback, $podfn, $preparefunc);
404 my $cmd = shift @ARGV;
405 handle_cmd
($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc);
412 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
417 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
420 print_usage_short
(\
*STDERR
, "no command specified");
422 } elsif ($cmd eq 'verifyapi') {
423 PVE
::RESTHandler
::validate_method_schemas
();
425 } elsif ($cmd eq 'printmanpod') {
426 $podfn = find_cli_class_source
($exename) if !defined($podfn);
427 print_pod_manpage
($podfn);
429 } elsif ($cmd eq 'bashcomplete') {
430 &$print_bash_completion($cmddef, 0, @$args);
434 &$preparefunc() if $preparefunc;
436 $cmd = &$expand_command_name($cmddef, $cmd);
438 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
441 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
445 my $prefix = "$exename $cmd";
446 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
448 &$outsub($res) if $outsub;
451 sub handle_simple_cmd
{
452 my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_;
454 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
455 die "no class specified" if !$class;
457 if (scalar(@$args) >= 1) {
458 if ($args->[0] eq 'help') {
459 my $str = "USAGE: $name help\n";
460 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
461 print STDERR
"$str\n\n";
463 } elsif ($args->[0] eq 'bashcomplete') {
465 &$print_bash_completion({ $name => $def }, $name, @$args);
467 } elsif ($args->[0] eq 'verifyapi') {
468 PVE
::RESTHandler
::validate_method_schemas
();
470 } elsif ($args->[0] eq 'printmanpod') {
471 $podfn = find_cli_class_source
($name) if !defined($podfn);
472 print_simple_pod_manpage
($podfn, @$def);
477 &$preparefunc() if $preparefunc;
479 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
481 &$outsub($res) if $outsub;