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