1 package PVE
::CLIHandler
;
7 use PVE
::Exception
qw(raise raise_param_exc);
11 use base
qw(PVE::RESTHandler);
15 my $cli_handler_class;
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 && $cli_handler_class);
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;
98 my $pwcallback = $cli_handler_class->can('read_password');
99 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
101 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
102 $verbose ?
'full' : 'short', $pwcallback,
107 print "USAGE: $str\n";
114 sub print_simple_asciidoc_synopsis
{
115 my ($class, $name, $arg_param, $uri_param) = @_;
117 die "not initialized" if !$cli_handler_class;
119 my $pwcallback = $cli_handler_class->can('read_password');
120 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
122 my $synopsis = "*${name}* `help`\n\n";
124 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param,
125 'asciidoc', $pwcallback, $stringfilemap);
130 sub print_asciidoc_synopsis
{
132 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
134 my $pwcallback = $cli_handler_class->can('read_password');
135 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
139 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
142 foreach my $cmd (sort keys %$cmddef) {
143 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
144 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
145 $uri_param, 'asciidoc', $pwcallback,
147 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
149 $synopsis .= "$str\n\n";
158 sub print_usage_verbose
{
160 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
162 my $pwcallback = $cli_handler_class->can('read_password');
163 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
165 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
167 foreach my $cmd (sort keys %$cmddef) {
168 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
169 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
170 'full', $pwcallback, $stringfilemap);
175 sub sorted_commands
{
176 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
179 sub print_usage_short
{
182 die "not initialized" if !($cmddef && $exename && $cli_handler_class);
184 my $pwcallback = $cli_handler_class->can('read_password');
185 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
187 print $fd "ERROR: $msg\n" if $msg;
188 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
191 foreach my $cmd (sorted_commands
()) {
192 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
193 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
194 print $fd "\n" if $oldclass && $oldclass ne $class;
200 my $print_bash_completion = sub {
201 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
205 return if !(defined($cur) && defined($prev) && defined($bash_command));
206 return if !defined($ENV{COMP_LINE
});
207 return if !defined($ENV{COMP_POINT
});
209 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
210 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
212 my $args = PVE
::Tools
::split_args
($cmdline);
213 my $pos = scalar(@$args) - 2;
214 $pos += 1 if $cmdline =~ m/\s+$/;
216 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
220 my $print_result = sub {
222 print "$p\n" if $p =~ m/^$cur/;
231 &$print_result(keys %$cmddef);
237 my $def = $cmddef->{$cmd};
240 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
244 my ($class, $name, $arg_param, $uri_param) = @$def;
248 $arg_param = [ $arg_param ] if !ref($arg_param);
250 map { $skip_param->{$_} = 1; } @$arg_param;
251 map { $skip_param->{$_} = 1; } keys %$uri_param;
253 my $fpcount = scalar(@$arg_param);
255 my $info = $class->map_method_by_name($name);
257 my $schema = $info->{parameters
};
258 my $prop = $schema->{properties
};
260 my $print_parameter_completion = sub {
262 my $d = $prop->{$pname};
263 if ($d->{completion
}) {
264 my $vt = ref($d->{completion
});
266 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
267 &$print_result(@$res);
269 } elsif ($d->{type
} eq 'boolean') {
270 &$print_result('0', '1');
271 } elsif ($d->{enum
}) {
272 &$print_result(@{$d->{enum
}});
276 # positional arguments
277 $pos += 1 if $simple_cmd;
278 if ($fpcount && $pos <= $fpcount) {
279 my $pname = $arg_param->[$pos -1];
280 &$print_parameter_completion($pname);
284 my @option_list = ();
285 foreach my $key (keys %$prop) {
286 next if $skip_param->{$key};
287 push @option_list, "--$key";
291 &$print_result(@option_list);
295 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
297 &$print_parameter_completion($pname);
301 &$print_result(@option_list);
307 # simply verify all registered methods
308 PVE
::RESTHandler
::validate_method_schemas
();
311 my $get_exe_name = sub {
321 sub generate_bash_completions
{
324 # generate bash completion config
326 $exename = &$get_exe_name($class);
329 # $exename bash completion
331 # see http://tiswww.case.edu/php/chet/bash/FAQ
332 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
333 # this modifies global var, but I found no better way
334 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
336 complete -o default -C '$exename bashcomplete' $exename
340 sub generate_asciidoc_synopsys
{
342 $class->generate_asciidoc_synopsis();
345 sub generate_asciidoc_synopsis
{
348 $cli_handler_class = $class;
350 $exename = &$get_exe_name($class);
353 my $def = ${"${class}::cmddef"};
355 if (ref($def) eq 'ARRAY') {
356 print_simple_asciidoc_synopsis
(@$def);
360 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
362 print_asciidoc_synopsis
();
366 # overwrite this if you want to run/setup things early
367 sub setup_environment
{
370 # do nothing by default
373 my $handle_cmd = sub {
374 my ($def, $cmdname, $cmd, $args, $pwcallback, $preparefunc, $stringfilemap) = @_;
379 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
381 # call verifyapi before setup_environment(), because we do not want to
382 # execute any real code in this case
385 print_usage_short
(\
*STDERR
, "no command specified");
387 } elsif ($cmd eq 'verifyapi') {
388 PVE
::RESTHandler
::validate_method_schemas
();
392 $cli_handler_class->setup_environment();
394 if ($cmd eq 'bashcomplete') {
395 &$print_bash_completion($cmddef, 0, @$args);
399 &$preparefunc() if $preparefunc;
401 $cmd = &$expand_command_name($cmddef, $cmd);
403 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
406 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
410 my $prefix = "$exename $cmd";
411 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
413 &$outsub($res) if $outsub;
416 my $handle_simple_cmd = sub {
417 my ($def, $args, $pwcallback, $preparefunc, $stringfilemap) = @_;
419 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
420 die "no class specified" if !$class;
422 if (scalar(@$args) >= 1) {
423 if ($args->[0] eq 'help') {
424 my $str = "USAGE: $name help\n";
425 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
426 print STDERR
"$str\n\n";
428 } elsif ($args->[0] eq 'verifyapi') {
429 PVE
::RESTHandler
::validate_method_schemas
();
434 $cli_handler_class->setup_environment();
436 if (scalar(@$args) >= 1) {
437 if ($args->[0] eq 'bashcomplete') {
439 &$print_bash_completion({ $name => $def }, $name, @$args);
444 &$preparefunc() if $preparefunc;
446 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
448 &$outsub($res) if $outsub;
451 sub run_cli_handler
{
452 my ($class, %params) = @_;
454 $cli_handler_class = $class;
456 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
458 foreach my $key (keys %params) {
459 next if $key eq 'prepare';
460 next if $key eq 'no_init'; # not used anymore
461 next if $key eq 'no_rpcenv'; # not used anymore
462 die "unknown parameter '$key'";
465 my $preparefunc = $params{prepare
};
467 my $pwcallback = $class->can('read_password');
468 my $stringfilemap = $class->can('string_param_file_mapping');
470 $exename = &$get_exe_name($class);
475 my $def = ${"${class}::cmddef"};
477 if (ref($def) eq 'ARRAY') {
478 &$handle_simple_cmd($def, \
@ARGV, $pwcallback, $preparefunc, $stringfilemap);
481 my $cmd = shift @ARGV;
482 &$handle_cmd($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $preparefunc, $stringfilemap);