]>
Commit | Line | Data |
---|---|---|
1 | package PVE::CLIHandler; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::SafeSyslog; | |
7 | use PVE::Exception qw(raise raise_param_exc); | |
8 | use PVE::RESTHandler; | |
9 | use PVE::INotify; | |
10 | ||
11 | use base qw(PVE::RESTHandler); | |
12 | ||
13 | # $cmddef defines which (sub)commands are available in a specific CLI class. | |
14 | # A real command is always an array consisting of its class, name, array of | |
15 | # position fixed (required) parameters and hash of predefined parameters when | |
16 | # mapping a CLI command t o an API call. Optionally an output method can be | |
17 | # passed at the end, e.g., for formatting or transformation purpose. | |
18 | # | |
19 | # [class, name, fixed_params, API_pre-set params, output_sub ] | |
20 | # | |
21 | # In case of so called 'simple commands', the $cmddef can be also just an | |
22 | # array. | |
23 | # | |
24 | # Examples: | |
25 | # $cmddef = { | |
26 | # command => [ 'PVE::API2::Class', 'command', [ 'arg1', 'arg2' ], { node => $nodename } ], | |
27 | # do => { | |
28 | # this => [ 'PVE::API2::OtherClass', 'method', [ 'arg1' ], undef, sub { | |
29 | # my ($res) = @_; | |
30 | # print "$res\n"; | |
31 | # }], | |
32 | # that => [ 'PVE::API2::OtherClass', 'subroutine' [] ], | |
33 | # }, | |
34 | # dothat => { alias => 'do that' }, | |
35 | # } | |
36 | my $cmddef; | |
37 | my $exename; | |
38 | my $cli_handler_class; | |
39 | ||
40 | my $assert_initialized = sub { | |
41 | my @caller = caller; | |
42 | die "$caller[0]:$caller[2] - not initialized\n" | |
43 | if !($cmddef && $exename && $cli_handler_class); | |
44 | }; | |
45 | ||
46 | my $abort = sub { | |
47 | my ($reason, $cmd) = @_; | |
48 | print_usage_short (\*STDERR, $reason, $cmd); | |
49 | exit (-1); | |
50 | }; | |
51 | ||
52 | my $expand_command_name = sub { | |
53 | my ($def, $cmd) = @_; | |
54 | ||
55 | return $cmd if exists $def->{$cmd}; # command is already complete | |
56 | ||
57 | my $is_alias = sub { ref($_[0]) eq 'HASH' && $_[0]->{alias} }; | |
58 | my @expanded = grep { /^\Q$cmd\E/ && !$is_alias->($def->{$_}) } keys %$def; | |
59 | ||
60 | return $expanded[0] if scalar(@expanded) == 1; # enforce exact match | |
61 | ||
62 | return undef; | |
63 | }; | |
64 | ||
65 | my $get_commands = sub { | |
66 | my $def = shift // die "no command definition passed!"; | |
67 | return [ grep { !(ref($def->{$_}) eq 'HASH' && defined($def->{$_}->{alias})) } sort keys %$def ]; | |
68 | }; | |
69 | ||
70 | my $complete_command_names = sub { $get_commands->($cmddef) }; | |
71 | ||
72 | # traverses the command definition using the $argv array, resolving one level | |
73 | # of aliases. | |
74 | # Returns the matching (sub) command and its definition, and argument array for | |
75 | # this (sub) command and a hash where we marked which (sub) commands got | |
76 | # expanded (e.g. st => status) while traversing | |
77 | sub resolve_cmd { | |
78 | my ($argv, $is_alias) = @_; | |
79 | ||
80 | my ($def, $cmd) = ($cmddef, $argv); | |
81 | my $cmdstr = $exename; | |
82 | ||
83 | if (ref($argv) eq 'ARRAY') { | |
84 | my $expanded_last_arg; | |
85 | my $last_arg_id = scalar(@$argv) - 1; | |
86 | ||
87 | for my $i (0..$last_arg_id) { | |
88 | $cmd = $expand_command_name->($def, $argv->[$i]); | |
89 | if (defined($cmd)) { | |
90 | # If the argument was expanded (or was already complete) and it | |
91 | # is the final argument, tell our caller about it: | |
92 | $expanded_last_arg = $cmd if $i == $last_arg_id; | |
93 | } else { | |
94 | # Otherwise continue with the unexpanded version of it. | |
95 | $cmd = $argv->[$i]; | |
96 | } | |
97 | $cmdstr .= " $cmd"; | |
98 | $def = $def->{$cmd}; | |
99 | last if !defined($def); | |
100 | ||
101 | if (ref($def) eq 'ARRAY') { | |
102 | # could expand to a real command, rest of $argv are its arguments | |
103 | my $cmd_args = [ @$argv[$i+1..$last_arg_id] ]; | |
104 | return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr); | |
105 | } | |
106 | ||
107 | if (defined($def->{alias})) { | |
108 | die "alias loop detected for '$cmd'" if $is_alias; # avoids cycles | |
109 | # replace aliased (sub)command with the expanded aliased command | |
110 | splice @$argv, $i, 1, split(/ +/, $def->{alias}); | |
111 | return resolve_cmd($argv, 1); | |
112 | } | |
113 | } | |
114 | # got either a special command (bashcomplete, verifyapi) or an unknown | |
115 | # cmd, just return first entry as cmd and the rest of $argv as cmd_arg | |
116 | my $cmd_args = [ @$argv[1..$last_arg_id] ]; | |
117 | return ($argv->[0], $def, $cmd_args, $expanded_last_arg, $cmdstr); | |
118 | } | |
119 | return ($cmd, $def, undef, undef, $cmdstr); | |
120 | } | |
121 | ||
122 | sub generate_usage_str { | |
123 | my ($format, $cmd, $indent, $separator, $sortfunc) = @_; | |
124 | ||
125 | $assert_initialized->(); | |
126 | die 'format required' if !$format; | |
127 | ||
128 | $sortfunc //= sub { sort keys %{$_[0]} }; | |
129 | $separator //= ''; | |
130 | $indent //= ''; | |
131 | ||
132 | my $read_password_func = $cli_handler_class->can('read_password'); | |
133 | my $param_mapping_func = $cli_handler_class->can('param_mapping') || | |
134 | $cli_handler_class->can('string_param_file_mapping'); | |
135 | ||
136 | my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd); | |
137 | $abort->("unknown command '$cmdstr'") if !defined($def) && ref($cmd) eq 'ARRAY'; | |
138 | ||
139 | my $generate; | |
140 | $generate = sub { | |
141 | my ($indent, $separator, $def, $prefix) = @_; | |
142 | ||
143 | my $str = ''; | |
144 | if (ref($def) eq 'HASH') { | |
145 | my $oldclass = undef; | |
146 | foreach my $cmd (&$sortfunc($def)) { | |
147 | ||
148 | if (ref($def->{$cmd}) eq 'ARRAY') { | |
149 | my ($class, $name, $arg_param, $fixed_param) = @{$def->{$cmd}}; | |
150 | ||
151 | $str .= $separator if $oldclass && $oldclass ne $class; | |
152 | $str .= $indent; | |
153 | $str .= $class->usage_str($name, "$prefix $cmd", $arg_param, | |
154 | $fixed_param, $format, | |
155 | $read_password_func, $param_mapping_func); | |
156 | $oldclass = $class; | |
157 | ||
158 | } elsif (defined($def->{$cmd}->{alias}) && ($format eq 'asciidoc')) { | |
159 | ||
160 | $str .= "*$prefix $cmd*\n\nAn alias for '$exename $def->{$cmd}->{alias}'.\n\n"; | |
161 | ||
162 | } else { | |
163 | next if $def->{$cmd}->{alias}; | |
164 | ||
165 | my $substr = $generate->($indent, $separator, $def->{$cmd}, "$prefix $cmd"); | |
166 | if ($substr) { | |
167 | $substr .= $separator if $substr !~ /\Q$separator\E{2}/; | |
168 | $str .= $substr; | |
169 | } | |
170 | } | |
171 | ||
172 | } | |
173 | } else { | |
174 | my ($class, $name, $arg_param, $fixed_param) = @$def; | |
175 | $abort->("unknown command '$cmd'") if !$class; | |
176 | ||
177 | $str .= $indent; | |
178 | $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, | |
179 | $read_password_func, $param_mapping_func); | |
180 | } | |
181 | return $str; | |
182 | }; | |
183 | ||
184 | return $generate->($indent, $separator, $def, $cmdstr); | |
185 | } | |
186 | ||
187 | __PACKAGE__->register_method ({ | |
188 | name => 'help', | |
189 | path => 'help', | |
190 | method => 'GET', | |
191 | description => "Get help about specified command.", | |
192 | parameters => { | |
193 | additionalProperties => 0, | |
194 | properties => { | |
195 | 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args', { | |
196 | description => 'Shows help for a specific command', | |
197 | completion => $complete_command_names, | |
198 | }), | |
199 | verbose => { | |
200 | description => "Verbose output format.", | |
201 | type => 'boolean', | |
202 | optional => 1, | |
203 | }, | |
204 | }, | |
205 | }, | |
206 | returns => { type => 'null' }, | |
207 | ||
208 | code => sub { | |
209 | my ($param) = @_; | |
210 | ||
211 | $assert_initialized->(); | |
212 | ||
213 | my $cmd = $param->{'extra-args'}; | |
214 | ||
215 | my $verbose = defined($cmd) && $cmd; | |
216 | $verbose = $param->{verbose} if defined($param->{verbose}); | |
217 | ||
218 | if (!$cmd) { | |
219 | if ($verbose) { | |
220 | print_usage_verbose(); | |
221 | } else { | |
222 | print_usage_short(\*STDOUT); | |
223 | } | |
224 | return undef; | |
225 | } | |
226 | ||
227 | my $str; | |
228 | if ($verbose) { | |
229 | $str = generate_usage_str('full', $cmd, ''); | |
230 | } else { | |
231 | $str = generate_usage_str('short', $cmd, ' ' x 7); | |
232 | } | |
233 | $str =~ s/^\s+//; | |
234 | ||
235 | if ($verbose) { | |
236 | print "$str\n"; | |
237 | } else { | |
238 | print "USAGE: $str\n"; | |
239 | } | |
240 | ||
241 | return undef; | |
242 | ||
243 | }}); | |
244 | ||
245 | sub print_simple_asciidoc_synopsis { | |
246 | $assert_initialized->(); | |
247 | ||
248 | my $synopsis = "*${exename}* `help`\n\n"; | |
249 | $synopsis .= generate_usage_str('asciidoc'); | |
250 | ||
251 | return $synopsis; | |
252 | } | |
253 | ||
254 | sub print_asciidoc_synopsis { | |
255 | $assert_initialized->(); | |
256 | ||
257 | my $synopsis = ""; | |
258 | ||
259 | $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n"; | |
260 | ||
261 | $synopsis .= generate_usage_str('asciidoc'); | |
262 | ||
263 | $synopsis .= "\n"; | |
264 | ||
265 | return $synopsis; | |
266 | } | |
267 | ||
268 | sub print_usage_verbose { | |
269 | $assert_initialized->(); | |
270 | ||
271 | print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; | |
272 | ||
273 | my $str = generate_usage_str('full'); | |
274 | ||
275 | print "$str\n"; | |
276 | } | |
277 | ||
278 | sub print_usage_short { | |
279 | my ($fd, $msg, $cmd) = @_; | |
280 | ||
281 | $assert_initialized->(); | |
282 | ||
283 | print $fd "ERROR: $msg\n" if $msg; | |
284 | print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n"; | |
285 | ||
286 | print {$fd} generate_usage_str('short', $cmd, ' ' x 7, "\n", sub { | |
287 | my ($h) = @_; | |
288 | return sort { | |
289 | if (ref($h->{$a}) eq 'ARRAY' && ref($h->{$b}) eq 'ARRAY') { | |
290 | # $a and $b are both real commands order them by their class | |
291 | return $h->{$a}->[0] cmp $h->{$b}->[0] || $a cmp $b; | |
292 | } elsif (ref($h->{$a}) eq 'ARRAY' xor ref($h->{$b}) eq 'ARRAY') { | |
293 | # real command and subcommand mixed, put sub commands first | |
294 | return ref($h->{$b}) eq 'ARRAY' ? -1 : 1; | |
295 | } else { | |
296 | # both are either from the same class or subcommands | |
297 | return $a cmp $b; | |
298 | } | |
299 | } keys %$h; | |
300 | }); | |
301 | } | |
302 | ||
303 | my $print_bash_completion = sub { | |
304 | my ($simple_cmd, $bash_command, $cur, $prev) = @_; | |
305 | ||
306 | my $debug = 0; | |
307 | ||
308 | return if !(defined($cur) && defined($prev) && defined($bash_command)); | |
309 | return if !defined($ENV{COMP_LINE}); | |
310 | return if !defined($ENV{COMP_POINT}); | |
311 | ||
312 | my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); | |
313 | print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug; | |
314 | ||
315 | my $args = PVE::Tools::split_args($cmdline); | |
316 | shift @$args; # no need for program name | |
317 | my $print_result = sub { | |
318 | foreach my $p (@_) { | |
319 | print "$p\n" if $p =~ m/^$cur/; | |
320 | } | |
321 | }; | |
322 | ||
323 | my ($cmd, $def) = ($simple_cmd, $cmddef); | |
324 | if (!$simple_cmd) { | |
325 | ($cmd, $def, $args, my $expanded) = resolve_cmd($args); | |
326 | ||
327 | if (defined($expanded) && $prev ne $expanded) { | |
328 | print "$expanded\n"; | |
329 | return; | |
330 | } | |
331 | ||
332 | if (ref($def) eq 'HASH') { | |
333 | &$print_result(@{$get_commands->($def)}); | |
334 | return; | |
335 | } | |
336 | } | |
337 | return if !$def; | |
338 | ||
339 | my $pos = scalar(@$args) - 1; | |
340 | $pos += 1 if $cmdline =~ m/\s+$/; | |
341 | print STDERR "pos: $pos\n" if $debug; | |
342 | return if $pos < 0; | |
343 | ||
344 | my $skip_param = {}; | |
345 | ||
346 | my ($class, $name, $arg_param, $uri_param) = @$def; | |
347 | $arg_param //= []; | |
348 | $uri_param //= {}; | |
349 | ||
350 | $arg_param = [ $arg_param ] if !ref($arg_param); | |
351 | ||
352 | map { $skip_param->{$_} = 1; } @$arg_param; | |
353 | map { $skip_param->{$_} = 1; } keys %$uri_param; | |
354 | ||
355 | my $info = $class->map_method_by_name($name); | |
356 | ||
357 | my $prop = $info->{parameters}->{properties}; | |
358 | ||
359 | my $print_parameter_completion = sub { | |
360 | my ($pname) = @_; | |
361 | my $d = $prop->{$pname}; | |
362 | if ($d->{completion}) { | |
363 | my $vt = ref($d->{completion}); | |
364 | if ($vt eq 'CODE') { | |
365 | my $res = $d->{completion}->($cmd, $pname, $cur, $args); | |
366 | &$print_result(@$res); | |
367 | } | |
368 | } elsif ($d->{type} eq 'boolean') { | |
369 | &$print_result('0', '1'); | |
370 | } elsif ($d->{enum}) { | |
371 | &$print_result(@{$d->{enum}}); | |
372 | } | |
373 | }; | |
374 | ||
375 | # positional arguments | |
376 | if ($pos < scalar(@$arg_param)) { | |
377 | my $pname = $arg_param->[$pos]; | |
378 | &$print_parameter_completion($pname); | |
379 | return; | |
380 | } | |
381 | ||
382 | my @option_list = (); | |
383 | foreach my $key (keys %$prop) { | |
384 | next if $skip_param->{$key}; | |
385 | push @option_list, "--$key"; | |
386 | } | |
387 | ||
388 | if ($cur =~ m/^-/) { | |
389 | &$print_result(@option_list); | |
390 | return; | |
391 | } | |
392 | ||
393 | if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) { | |
394 | my $pname = $1; | |
395 | &$print_parameter_completion($pname); | |
396 | return; | |
397 | } | |
398 | ||
399 | &$print_result(@option_list); | |
400 | }; | |
401 | ||
402 | sub verify_api { | |
403 | my ($class) = @_; | |
404 | ||
405 | # simply verify all registered methods | |
406 | PVE::RESTHandler::validate_method_schemas(); | |
407 | } | |
408 | ||
409 | my $get_exe_name = sub { | |
410 | my ($class) = @_; | |
411 | ||
412 | my $name = $class; | |
413 | $name =~ s/^.*:://; | |
414 | $name =~ s/_/-/g; | |
415 | ||
416 | return $name; | |
417 | }; | |
418 | ||
419 | sub generate_bash_completions { | |
420 | my ($class) = @_; | |
421 | ||
422 | # generate bash completion config | |
423 | ||
424 | $exename = &$get_exe_name($class); | |
425 | ||
426 | print <<__EOD__; | |
427 | # $exename bash completion | |
428 | ||
429 | # see http://tiswww.case.edu/php/chet/bash/FAQ | |
430 | # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion | |
431 | # this modifies global var, but I found no better way | |
432 | COMP_WORDBREAKS=\${COMP_WORDBREAKS//:} | |
433 | ||
434 | complete -o default -C '$exename bashcomplete' $exename | |
435 | __EOD__ | |
436 | } | |
437 | ||
438 | sub generate_asciidoc_synopsys { | |
439 | my ($class) = @_; | |
440 | $class->generate_asciidoc_synopsis(); | |
441 | }; | |
442 | ||
443 | sub generate_asciidoc_synopsis { | |
444 | my ($class) = @_; | |
445 | ||
446 | $cli_handler_class = $class; | |
447 | ||
448 | $exename = &$get_exe_name($class); | |
449 | ||
450 | no strict 'refs'; | |
451 | my $def = ${"${class}::cmddef"}; | |
452 | $cmddef = $def; | |
453 | ||
454 | if (ref($def) eq 'ARRAY') { | |
455 | print_simple_asciidoc_synopsis(); | |
456 | } else { | |
457 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
458 | ||
459 | print_asciidoc_synopsis(); | |
460 | } | |
461 | } | |
462 | ||
463 | # overwrite this if you want to run/setup things early | |
464 | sub setup_environment { | |
465 | my ($class) = @_; | |
466 | ||
467 | # do nothing by default | |
468 | } | |
469 | ||
470 | my $handle_cmd = sub { | |
471 | my ($args, $read_password_func, $preparefunc, $param_mapping_func) = @_; | |
472 | ||
473 | $cmddef->{help} = [ __PACKAGE__, 'help', ['extra-args'] ]; | |
474 | ||
475 | my ($cmd, $def, $cmd_args, undef, $cmd_str) = resolve_cmd($args); | |
476 | ||
477 | $abort->("no command specified") if !$cmd; | |
478 | ||
479 | # call verifyapi before setup_environment(), don't execute any real code in | |
480 | # this case | |
481 | if ($cmd eq 'verifyapi') { | |
482 | PVE::RESTHandler::validate_method_schemas(); | |
483 | return; | |
484 | } | |
485 | ||
486 | $cli_handler_class->setup_environment(); | |
487 | ||
488 | if ($cmd eq 'bashcomplete') { | |
489 | &$print_bash_completion(undef, @$cmd_args); | |
490 | return; | |
491 | } | |
492 | ||
493 | # checked special commands, if def is still a hash we got an incomplete sub command | |
494 | $abort->("incomplete command '$cmd_str'") if ref($def) eq 'HASH'; | |
495 | ||
496 | &$preparefunc() if $preparefunc; | |
497 | ||
498 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []}; | |
499 | $abort->("unknown command '$cmd_str'") if !$class; | |
500 | ||
501 | my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $read_password_func, $param_mapping_func); | |
502 | ||
503 | &$outsub($res) if $outsub; | |
504 | }; | |
505 | ||
506 | my $handle_simple_cmd = sub { | |
507 | my ($args, $read_password_func, $preparefunc, $param_mapping_func) = @_; | |
508 | ||
509 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef}; | |
510 | die "no class specified" if !$class; | |
511 | ||
512 | if (scalar(@$args) >= 1) { | |
513 | if ($args->[0] eq 'help') { | |
514 | my $str = "USAGE: $name help\n"; | |
515 | $str .= generate_usage_str('long'); | |
516 | print STDERR "$str\n\n"; | |
517 | return; | |
518 | } elsif ($args->[0] eq 'verifyapi') { | |
519 | PVE::RESTHandler::validate_method_schemas(); | |
520 | return; | |
521 | } | |
522 | } | |
523 | ||
524 | $cli_handler_class->setup_environment(); | |
525 | ||
526 | if (scalar(@$args) >= 1) { | |
527 | if ($args->[0] eq 'bashcomplete') { | |
528 | shift @$args; | |
529 | &$print_bash_completion($name, @$args); | |
530 | return; | |
531 | } | |
532 | } | |
533 | ||
534 | &$preparefunc() if $preparefunc; | |
535 | ||
536 | my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $read_password_func, $param_mapping_func); | |
537 | ||
538 | &$outsub($res) if $outsub; | |
539 | }; | |
540 | ||
541 | sub run_cli_handler { | |
542 | my ($class, %params) = @_; | |
543 | ||
544 | $cli_handler_class = $class; | |
545 | ||
546 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
547 | ||
548 | foreach my $key (keys %params) { | |
549 | next if $key eq 'prepare'; | |
550 | next if $key eq 'no_init'; # not used anymore | |
551 | next if $key eq 'no_rpcenv'; # not used anymore | |
552 | die "unknown parameter '$key'"; | |
553 | } | |
554 | ||
555 | my $preparefunc = $params{prepare}; | |
556 | ||
557 | my $read_password_func = $class->can('read_password'); | |
558 | my $param_mapping_func = $cli_handler_class->can('param_mapping') || | |
559 | $class->can('string_param_file_mapping'); | |
560 | ||
561 | $exename = &$get_exe_name($class); | |
562 | ||
563 | initlog($exename); | |
564 | ||
565 | no strict 'refs'; | |
566 | $cmddef = ${"${class}::cmddef"}; | |
567 | ||
568 | if (ref($cmddef) eq 'ARRAY') { | |
569 | &$handle_simple_cmd(\@ARGV, $read_password_func, $preparefunc, $param_mapping_func); | |
570 | } else { | |
571 | &$handle_cmd(\@ARGV, $read_password_func, $preparefunc, $param_mapping_func); | |
572 | } | |
573 | ||
574 | exit 0; | |
575 | } | |
576 | ||
577 | 1; |