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 __PACKAGE__-
>register_method ({
41 description
=> "Get help about specified command.",
43 additionalProperties
=> 0,
46 description
=> "Command name",
51 description
=> "Verbose output format.",
57 returns
=> { type
=> 'null' },
62 die "not initialized" if !($cmddef && $exename);
64 my $cmd = $param->{cmd
};
66 my $verbose = defined($cmd) && $cmd;
67 $verbose = $param->{verbose
} if defined($param->{verbose
});
71 print_usage_verbose
();
73 print_usage_short
(\
*STDOUT
);
78 $cmd = &$expand_command_name($cmddef, $cmd);
80 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
82 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
85 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ?
'full' : 'short');
89 print "USAGE: $str\n";
96 sub print_simple_pod_manpage
{
97 my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
99 my $synopsis = " $name help\n\n";
100 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
105 my $parser = PVE
::PodParser-
>new();
106 $parser->{include
}->{synopsis
} = $synopsis;
107 $parser->parse_from_file($podfn);
110 sub print_pod_manpage
{
113 die "not initialized" if !($cmddef && $exename);
114 die "no pod file specified" if !$podfn;
118 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
120 my $style = 'full'; # or should we use 'short'?
122 foreach my $cmd (sorted_commands
()) {
123 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
124 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
126 $str =~ s/^USAGE: //;
128 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
130 $synopsis .= " $str\n\n";
136 my $parser = PVE
::PodParser-
>new();
137 $parser->{include
}->{synopsis
} = $synopsis;
138 $parser->parse_from_file($podfn);
141 sub print_usage_verbose
{
143 die "not initialized" if !($cmddef && $exename);
145 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
147 foreach my $cmd (sort keys %$cmddef) {
148 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
149 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full');
154 sub sorted_commands
{
155 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
158 sub print_usage_short
{
161 die "not initialized" if !($cmddef && $exename);
163 print $fd "ERROR: $msg\n" if $msg;
164 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
167 foreach my $cmd (sorted_commands
()) {
168 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
169 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short');
170 print $fd "\n" if $oldclass && $oldclass ne $class;
176 my $print_bash_completion = sub {
177 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
181 return if !(defined($cur) && defined($prev) && defined($bash_command));
182 return if !defined($ENV{COMP_LINE
});
183 return if !defined($ENV{COMP_POINT
});
185 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
186 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
188 # fixme: shell quoting??
189 my @args = split(/\s+/, $cmdline);
190 my $pos = scalar(@args) - 2;
191 $pos += 1 if $cmdline =~ m/\s+$/;
193 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
197 my $print_result = sub {
199 print "$p\n" if $p =~ m/^$cur/;
208 &$print_result(keys %$cmddef);
214 my $def = $cmddef->{$cmd};
217 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
221 my ($class, $name, $arg_param, $uri_param) = @$def;
225 map { $skip_param->{$_} = 1; } @$arg_param;
226 map { $skip_param->{$_} = 1; } keys %$uri_param;
228 my $fpcount = scalar(@$arg_param);
230 my $info = $class->map_method_by_name($name);
232 my $schema = $info->{parameters
};
233 my $prop = $schema->{properties
};
235 my $print_parameter_completion = sub {
237 my $d = $prop->{$pname};
238 if ($d->{completion
}) {
239 my $vt = ref($d->{completion
});
241 my $res = $d->{completion
}->($cmd, $pname, $cur);
242 &$print_result(@$res);
244 } elsif ($d->{type
} eq 'boolean') {
245 &$print_result('0', '1');
246 } elsif ($d->{enum
}) {
247 &$print_result(@{$d->{enum
}});
251 # positional arguments
252 $pos += 1 if $simple_cmd;
253 if ($fpcount && $pos <= $fpcount) {
254 my $pname = $arg_param->[$pos -1];
255 &$print_parameter_completion($pname);
259 my @option_list = ();
260 foreach my $key (keys %$prop) {
261 next if $skip_param->{$key};
262 push @option_list, "--$key";
266 &$print_result(@option_list);
270 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
272 &$print_parameter_completion($pname);
276 &$print_result(@option_list);
282 # simply verify all registered methods
283 PVE
::RESTHandler
::validate_method_schemas
();
286 sub generate_bash_completions
{
289 # generate bash completion config
292 $exename =~ s/^.*:://;
295 # $exename bash completion
297 # see http://tiswww.case.edu/php/chet/bash/FAQ
298 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
299 # this modifies global var, but I found no better way
300 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
302 complete -C '$exename bashcomplete' $exename
306 sub find_cli_class_source
{
311 my $cpath = "PVE/CLI/${exename}.pm";
312 foreach my $p (@INC) {
313 my $testfn = "$p/$cpath";
323 sub generate_pod_manpage
{
324 my ($class, $podfn) = @_;
327 $exename =~ s/^.*:://;
329 $podfn = find_cli_class_source
($exename) if !defined($podfn);
331 die "unable to find source for class '$class'" if !$podfn;
334 my $def = ${"${class}::cmddef"};
336 if (ref($def eq 'ARRAY')) {
337 print_simple_pod_manpage
($podfn, @$def);
340 print_pod_manpage
($podfn);
345 my ($class, $pwcallback, $podfn, $preparefunc) = @_;
347 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
350 $exename =~ s/^.*:://;
354 die "please run as root\n" if $> != 0;
356 PVE
::INotify
::inotify_init
();
358 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
359 $rpcenv->init_request();
360 $rpcenv->set_language($ENV{LANG
});
361 $rpcenv->set_user('root@pam');
364 my $def = ${"${class}::cmddef"};
366 if (ref($def) eq 'ARRAY') {
367 handle_simple_cmd
($def, \
@ARGV, $pwcallback, $podfn, $preparefunc);
370 my $cmd = shift @ARGV;
371 handle_cmd
($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $podfn, $preparefunc);
378 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
383 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
386 print_usage_short
(\
*STDERR
, "no command specified");
388 } elsif ($cmd eq 'verifyapi') {
389 PVE
::RESTHandler
::validate_method_schemas
();
391 } elsif ($cmd eq 'printmanpod') {
392 print_pod_manpage
($podfn);
394 } elsif ($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);
413 &$outsub($res) if $outsub;
416 sub handle_simple_cmd
{
417 my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_;
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');
426 print STDERR
"$str\n\n";
428 } elsif ($args->[0] eq 'bashcomplete') {
430 &$print_bash_completion({ $name => $def }, $name, @$args);
432 } elsif ($args->[0] eq 'verifyapi') {
433 PVE
::RESTHandler
::validate_method_schemas
();
435 } elsif ($args->[0] eq 'printmanpod') {
436 $podfn = find_cli_class_source
($name) if !defined($podfn);
437 print_simple_pod_manpage
($podfn, @$def);
442 &$preparefunc() if $preparefunc;
444 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
446 &$outsub($res) if $outsub;