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