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