]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::CLIHandler; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
93ddd7bc | 6 | use PVE::SafeSyslog; |
e143e9d8 DM |
7 | use PVE::Exception qw(raise raise_param_exc); |
8 | use PVE::RESTHandler; | |
881eb755 | 9 | use PVE::INotify; |
e143e9d8 DM |
10 | |
11 | use base qw(PVE::RESTHandler); | |
12 | ||
c1059f7c TL |
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 | # } | |
e143e9d8 DM |
36 | my $cmddef; |
37 | my $exename; | |
891b798d | 38 | my $cli_handler_class; |
e143e9d8 | 39 | |
d204696c TL |
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 | ||
918140af TL |
46 | my $abort = sub { |
47 | my ($reason, $cmd) = @_; | |
48 | print_usage_short (\*STDERR, $reason, $cmd); | |
49 | exit (-1); | |
50 | }; | |
51 | ||
e143e9d8 DM |
52 | my $expand_command_name = sub { |
53 | my ($def, $cmd) = @_; | |
54 | ||
5082a17b WB |
55 | return $cmd if exists $def->{$cmd}; # command is already complete |
56 | ||
ea5a5084 | 57 | my $is_alias = sub { ref($_[0]) eq 'HASH' && exists($_[0]->{alias}) }; |
0b796806 TL |
58 | my @expanded = grep { /^\Q$cmd\E/ && !$is_alias->($def->{$_}) } keys %$def; |
59 | ||
5082a17b WB |
60 | return $expanded[0] if scalar(@expanded) == 1; # enforce exact match |
61 | ||
62 | return undef; | |
e143e9d8 DM |
63 | }; |
64 | ||
50b89b88 TL |
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 ]; | |
edf3d572 DM |
68 | }; |
69 | ||
50b89b88 TL |
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); | |
f9326469 | 81 | my $cmdstr = $exename; |
50b89b88 TL |
82 | |
83 | if (ref($argv) eq 'ARRAY') { | |
5082a17b | 84 | my $expanded_last_arg; |
50b89b88 TL |
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]); | |
5082a17b WB |
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 | } | |
f9326469 | 97 | $cmdstr .= " $cmd"; |
50b89b88 | 98 | $def = $def->{$cmd}; |
57c0d0c6 | 99 | last if !defined($def); |
50b89b88 TL |
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] ]; | |
5082a17b | 104 | return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr); |
50b89b88 TL |
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] ]; | |
5082a17b | 117 | return ($argv->[0], $def, $cmd_args, $expanded_last_arg, $cmdstr); |
50b89b88 | 118 | } |
f9326469 | 119 | return ($cmd, $def, undef, undef, $cmdstr); |
50b89b88 TL |
120 | } |
121 | ||
4c802a57 TL |
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 | ||
4e0214fa | 132 | my $read_password_func = $cli_handler_class->can('read_password'); |
c44ec0f6 DM |
133 | my $param_mapping_func = $cli_handler_class->can('param_mapping') || |
134 | $cli_handler_class->can('string_param_file_mapping'); | |
4c802a57 | 135 | |
f9326469 | 136 | my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd); |
24ccf353 | 137 | $abort->("unknown command '$cmdstr'") if !defined($def) && ref($cmd) eq 'ARRAY'; |
4c802a57 TL |
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, | |
4e0214fa | 155 | $read_password_func, $param_mapping_func); |
4c802a57 | 156 | $oldclass = $class; |
50b89b88 TL |
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 | } | |
4c802a57 TL |
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, | |
4e0214fa | 179 | $read_password_func, $param_mapping_func); |
4c802a57 TL |
180 | } |
181 | return $str; | |
182 | }; | |
183 | ||
50b89b88 | 184 | return $generate->($indent, $separator, $def, $cmdstr); |
4c802a57 TL |
185 | } |
186 | ||
e143e9d8 | 187 | __PACKAGE__->register_method ({ |
3ef20687 | 188 | name => 'help', |
e143e9d8 DM |
189 | path => 'help', |
190 | method => 'GET', | |
191 | description => "Get help about specified command.", | |
192 | parameters => { | |
3ef20687 | 193 | additionalProperties => 0, |
e143e9d8 | 194 | properties => { |
6627ae09 TL |
195 | 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args', { |
196 | description => 'Shows help for a specific command', | |
edf3d572 | 197 | completion => $complete_command_names, |
6627ae09 | 198 | }), |
e143e9d8 DM |
199 | verbose => { |
200 | description => "Verbose output format.", | |
201 | type => 'boolean', | |
202 | optional => 1, | |
203 | }, | |
204 | }, | |
205 | }, | |
206 | returns => { type => 'null' }, | |
3ef20687 | 207 | |
e143e9d8 DM |
208 | code => sub { |
209 | my ($param) = @_; | |
210 | ||
d204696c | 211 | $assert_initialized->(); |
e143e9d8 | 212 | |
6627ae09 | 213 | my $cmd = $param->{'extra-args'}; |
e143e9d8 | 214 | |
6627ae09 | 215 | my $verbose = defined($cmd) && $cmd; |
e143e9d8 DM |
216 | $verbose = $param->{verbose} if defined($param->{verbose}); |
217 | ||
218 | if (!$cmd) { | |
219 | if ($verbose) { | |
220 | print_usage_verbose(); | |
3ef20687 | 221 | } else { |
e143e9d8 DM |
222 | print_usage_short(\*STDOUT); |
223 | } | |
224 | return undef; | |
225 | } | |
226 | ||
4c802a57 TL |
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+//; | |
e143e9d8 | 234 | |
e143e9d8 DM |
235 | if ($verbose) { |
236 | print "$str\n"; | |
237 | } else { | |
238 | print "USAGE: $str\n"; | |
239 | } | |
240 | ||
241 | return undef; | |
242 | ||
243 | }}); | |
244 | ||
0a5a1eee | 245 | sub print_simple_asciidoc_synopsis { |
d204696c | 246 | $assert_initialized->(); |
fe3f1fde | 247 | |
4c802a57 TL |
248 | my $synopsis = "*${exename}* `help`\n\n"; |
249 | $synopsis .= generate_usage_str('asciidoc'); | |
fe3f1fde DM |
250 | |
251 | return $synopsis; | |
252 | } | |
253 | ||
0a5a1eee | 254 | sub print_asciidoc_synopsis { |
d204696c | 255 | $assert_initialized->(); |
fe3f1fde | 256 | |
fe3f1fde DM |
257 | my $synopsis = ""; |
258 | ||
259 | $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n"; | |
260 | ||
4c802a57 | 261 | $synopsis .= generate_usage_str('asciidoc'); |
fe3f1fde DM |
262 | |
263 | $synopsis .= "\n"; | |
264 | ||
265 | return $synopsis; | |
266 | } | |
267 | ||
e143e9d8 | 268 | sub print_usage_verbose { |
d204696c | 269 | $assert_initialized->(); |
891b798d | 270 | |
e143e9d8 DM |
271 | print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; |
272 | ||
4c802a57 | 273 | my $str = generate_usage_str('full'); |
e143e9d8 | 274 | |
4c802a57 | 275 | print "$str\n"; |
e143e9d8 DM |
276 | } |
277 | ||
278 | sub print_usage_short { | |
50b89b88 | 279 | my ($fd, $msg, $cmd) = @_; |
e143e9d8 | 280 | |
d204696c | 281 | $assert_initialized->(); |
891b798d | 282 | |
e143e9d8 DM |
283 | print $fd "ERROR: $msg\n" if $msg; |
284 | print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n"; | |
285 | ||
50b89b88 | 286 | print {$fd} generate_usage_str('short', $cmd, ' ' x 7, "\n", sub { |
4c802a57 TL |
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; | |
50b89b88 TL |
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; | |
4c802a57 | 295 | } else { |
50b89b88 | 296 | # both are either from the same class or subcommands |
4c802a57 TL |
297 | return $a cmp $b; |
298 | } | |
299 | } keys %$h; | |
300 | }); | |
e143e9d8 DM |
301 | } |
302 | ||
d8053c08 | 303 | my $print_bash_completion = sub { |
57e67ea3 | 304 | my ($simple_cmd, $bash_command, $cur, $prev) = @_; |
d8053c08 DM |
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 | ||
58d9e664 | 315 | my $args = PVE::Tools::split_args($cmdline); |
5fa768fc | 316 | shift @$args; # no need for program name |
d8053c08 DM |
317 | my $print_result = sub { |
318 | foreach my $p (@_) { | |
2c48a665 | 319 | print "$p\n" if $p =~ m/^\Q$cur\E/; |
d8053c08 DM |
320 | } |
321 | }; | |
322 | ||
5fa768fc TL |
323 | my ($cmd, $def) = ($simple_cmd, $cmddef); |
324 | if (!$simple_cmd) { | |
0ed80698 | 325 | ($cmd, $def, $args, my $expanded) = resolve_cmd($args); |
50b89b88 | 326 | |
5082a17b WB |
327 | if (defined($expanded) && $prev ne $expanded) { |
328 | print "$expanded\n"; | |
50b89b88 TL |
329 | return; |
330 | } | |
5082a17b WB |
331 | |
332 | if (ref($def) eq 'HASH') { | |
333 | &$print_result(@{$get_commands->($def)}); | |
d8053c08 DM |
334 | return; |
335 | } | |
d8053c08 | 336 | } |
d8053c08 DM |
337 | return if !$def; |
338 | ||
5fa768fc TL |
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; | |
d8053c08 DM |
343 | |
344 | my $skip_param = {}; | |
345 | ||
346 | my ($class, $name, $arg_param, $uri_param) = @$def; | |
347 | $arg_param //= []; | |
348 | $uri_param //= {}; | |
349 | ||
d90a2fd0 DM |
350 | $arg_param = [ $arg_param ] if !ref($arg_param); |
351 | ||
d8053c08 DM |
352 | map { $skip_param->{$_} = 1; } @$arg_param; |
353 | map { $skip_param->{$_} = 1; } keys %$uri_param; | |
354 | ||
d8053c08 DM |
355 | my $info = $class->map_method_by_name($name); |
356 | ||
5fa768fc | 357 | my $prop = $info->{parameters}->{properties}; |
d8053c08 DM |
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') { | |
58d9e664 | 365 | my $res = $d->{completion}->($cmd, $pname, $cur, $args); |
d8053c08 DM |
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 | |
5fa768fc TL |
376 | if ($pos < scalar(@$arg_param)) { |
377 | my $pname = $arg_param->[$pos]; | |
d8053c08 DM |
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 | ||
8042c2cc SI |
402 | # prints a formatted table with a title row. |
403 | # $formatopts is an array of hashes, with the following keys: | |
acd6f383 TL |
404 | # 'key' - key of $data element to print |
405 | # 'title' - column title, defaults to 'key' - won't get cutoff | |
406 | # 'cutoff' - maximal (print) length of this column values, if set | |
407 | # the last column will never be cutoff | |
408 | # 'default' - optional default value for the column | |
409 | # formatopts element order defines column order (left to right) | |
8042c2cc SI |
410 | sub print_text_table { |
411 | my ($formatopts, $data) = @_; | |
8042c2cc | 412 | |
85b9def2 TL |
413 | my ($formatstring, @keys, @titles, %cutoffs, %defaults); |
414 | my $last_col = $formatopts->[$#{$formatopts}]; | |
415 | ||
8042c2cc | 416 | foreach my $col ( @$formatopts ) { |
85b9def2 | 417 | my ($key, $title, $cutoff, $default) = @$col{qw(key title cutoff default)}; |
8042c2cc SI |
418 | $title //= $key; |
419 | ||
420 | push @keys, $key; | |
421 | push @titles, $title; | |
422 | $defaults{$key} = $default; | |
423 | ||
85b9def2 | 424 | # calculate maximal print width and cutoff |
8042c2cc SI |
425 | my $titlelen = length($title); |
426 | ||
427 | my $longest = $titlelen; | |
428 | foreach my $entry (@$data) { | |
429 | my $len = length($entry->{$key}) // 0; | |
430 | $longest = $len if $len > $longest; | |
431 | } | |
432 | ||
433 | $cutoff = (defined($cutoff) && $cutoff < $longest) ? $cutoff : $longest; | |
434 | $cutoffs{$key} = $cutoff; | |
435 | ||
436 | my $printalign = $cutoff > $titlelen ? '-' : ''; | |
437 | if ($col == $last_col) { | |
438 | $formatstring .= "%${printalign}${titlelen}s\n"; | |
439 | } else { | |
440 | $formatstring .= "%${printalign}${cutoff}s "; | |
441 | } | |
442 | } | |
443 | ||
444 | printf $formatstring, @titles; | |
445 | ||
85b9def2 | 446 | foreach my $entry (sort { $a->{$keys[0]} cmp $b->{$keys[0]} } @$data) { |
8042c2cc SI |
447 | printf $formatstring, map { substr(($entry->{$_} // $defaults{$_}), 0 , $cutoffs{$_}) } @keys; |
448 | } | |
449 | } | |
450 | ||
451 | sub print_entry { | |
452 | my $entry = shift; | |
85b9def2 | 453 | |
8042c2cc SI |
454 | #TODO: handle objects/hashes as well |
455 | foreach my $item (sort keys %$entry) { | |
85b9def2 | 456 | if (ref($entry->{$item}) eq 'ARRAY') { |
8042c2cc SI |
457 | printf "%s: [ %s ]\n", $item, join(", ", @{$entry->{$item}}); |
458 | } else { | |
459 | printf "%s: %s\n", $item, $entry->{$item}; | |
460 | } | |
461 | } | |
462 | } | |
463 | ||
acd6f383 | 464 | # prints the result of an API GET call returning an array |
a604bf22 SI |
465 | # and to have the results key of the API call defined. |
466 | sub print_api_list { | |
467 | my ($props_to_print, $data, $returninfo) = @_; | |
85b9def2 | 468 | |
305fc1e1 TL |
469 | die "can only print array result" if $returninfo->{type} ne 'array'; |
470 | ||
a604bf22 | 471 | my $returnprops = $returninfo->{items}->{properties}; |
85b9def2 TL |
472 | |
473 | my $formatopts = []; | |
a604bf22 SI |
474 | foreach my $prop ( @$props_to_print ) { |
475 | my $propinfo = $returnprops->{$prop}; | |
476 | my $colopts = { | |
477 | key => $prop, | |
478 | title => $propinfo->{title}, | |
479 | default => $propinfo->{default}, | |
480 | cutoff => $propinfo->{print_width} // $propinfo->{maxLength}, | |
481 | }; | |
482 | push @$formatopts, $colopts; | |
483 | } | |
484 | ||
485 | print_text_table($formatopts, $data); | |
486 | } | |
487 | ||
1f130ba6 DM |
488 | sub verify_api { |
489 | my ($class) = @_; | |
490 | ||
491 | # simply verify all registered methods | |
492 | PVE::RESTHandler::validate_method_schemas(); | |
493 | } | |
494 | ||
8f3712f8 DM |
495 | my $get_exe_name = sub { |
496 | my ($class) = @_; | |
3ef20687 | 497 | |
8f3712f8 DM |
498 | my $name = $class; |
499 | $name =~ s/^.*:://; | |
500 | $name =~ s/_/-/g; | |
501 | ||
502 | return $name; | |
503 | }; | |
504 | ||
c45707a0 DM |
505 | sub generate_bash_completions { |
506 | my ($class) = @_; | |
507 | ||
508 | # generate bash completion config | |
509 | ||
8f3712f8 | 510 | $exename = &$get_exe_name($class); |
c45707a0 DM |
511 | |
512 | print <<__EOD__; | |
513 | # $exename bash completion | |
514 | ||
515 | # see http://tiswww.case.edu/php/chet/bash/FAQ | |
516 | # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion | |
517 | # this modifies global var, but I found no better way | |
518 | COMP_WORDBREAKS=\${COMP_WORDBREAKS//:} | |
519 | ||
e4a1d8e2 | 520 | complete -o default -C '$exename bashcomplete' $exename |
c45707a0 DM |
521 | __EOD__ |
522 | } | |
523 | ||
fe3f1fde DM |
524 | sub generate_asciidoc_synopsys { |
525 | my ($class) = @_; | |
0a5a1eee FG |
526 | $class->generate_asciidoc_synopsis(); |
527 | }; | |
528 | ||
529 | sub generate_asciidoc_synopsis { | |
530 | my ($class) = @_; | |
fe3f1fde DM |
531 | |
532 | $cli_handler_class = $class; | |
533 | ||
534 | $exename = &$get_exe_name($class); | |
535 | ||
536 | no strict 'refs'; | |
537 | my $def = ${"${class}::cmddef"}; | |
57e67ea3 | 538 | $cmddef = $def; |
fe3f1fde DM |
539 | |
540 | if (ref($def) eq 'ARRAY') { | |
4c802a57 | 541 | print_simple_asciidoc_synopsis(); |
fe3f1fde | 542 | } else { |
fe3f1fde DM |
543 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; |
544 | ||
0a5a1eee | 545 | print_asciidoc_synopsis(); |
fe3f1fde DM |
546 | } |
547 | } | |
548 | ||
7b7f99c9 DM |
549 | # overwrite this if you want to run/setup things early |
550 | sub setup_environment { | |
551 | my ($class) = @_; | |
552 | ||
553 | # do nothing by default | |
554 | } | |
555 | ||
891b798d | 556 | my $handle_cmd = sub { |
8d3eb3ce | 557 | my ($args, $read_password_func, $preparefunc, $param_mapping_func) = @_; |
e143e9d8 | 558 | |
6627ae09 | 559 | $cmddef->{help} = [ __PACKAGE__, 'help', ['extra-args'] ]; |
e143e9d8 | 560 | |
f9326469 | 561 | my ($cmd, $def, $cmd_args, undef, $cmd_str) = resolve_cmd($args); |
7b7f99c9 | 562 | |
918140af TL |
563 | $abort->("no command specified") if !$cmd; |
564 | ||
565 | # call verifyapi before setup_environment(), don't execute any real code in | |
566 | # this case | |
567 | if ($cmd eq 'verifyapi') { | |
e143e9d8 DM |
568 | PVE::RESTHandler::validate_method_schemas(); |
569 | return; | |
7b7f99c9 DM |
570 | } |
571 | ||
572 | $cli_handler_class->setup_environment(); | |
573 | ||
574 | if ($cmd eq 'bashcomplete') { | |
50b89b88 | 575 | &$print_bash_completion(undef, @$cmd_args); |
d8053c08 | 576 | return; |
e143e9d8 DM |
577 | } |
578 | ||
50b89b88 | 579 | # checked special commands, if def is still a hash we got an incomplete sub command |
131f316b | 580 | $abort->("incomplete command '$cmd_str'", $args) if ref($def) eq 'HASH'; |
8e3e9929 | 581 | |
50b89b88 | 582 | &$preparefunc() if $preparefunc; |
e143e9d8 | 583 | |
b8dc4366 | 584 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []}; |
50b89b88 | 585 | $abort->("unknown command '$cmd_str'") if !$class; |
e143e9d8 | 586 | |
f9326469 | 587 | my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $read_password_func, $param_mapping_func); |
2026f4b5 | 588 | |
a604bf22 SI |
589 | if (defined $outsub) { |
590 | my $returninfo = $class->map_method_by_name($name)->{returns}; | |
591 | $outsub->($res, $returninfo); | |
592 | } | |
891b798d | 593 | }; |
2026f4b5 | 594 | |
891b798d | 595 | my $handle_simple_cmd = sub { |
8d3eb3ce | 596 | my ($args, $read_password_func, $preparefunc, $param_mapping_func) = @_; |
2026f4b5 | 597 | |
57e67ea3 | 598 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef}; |
2026f4b5 DM |
599 | die "no class specified" if !$class; |
600 | ||
d8053c08 | 601 | if (scalar(@$args) >= 1) { |
2026f4b5 DM |
602 | if ($args->[0] eq 'help') { |
603 | my $str = "USAGE: $name help\n"; | |
4c802a57 | 604 | $str .= generate_usage_str('long'); |
2026f4b5 DM |
605 | print STDERR "$str\n\n"; |
606 | return; | |
607 | } elsif ($args->[0] eq 'verifyapi') { | |
608 | PVE::RESTHandler::validate_method_schemas(); | |
609 | return; | |
2026f4b5 | 610 | } |
e143e9d8 | 611 | } |
2026f4b5 | 612 | |
7b7f99c9 DM |
613 | $cli_handler_class->setup_environment(); |
614 | ||
615 | if (scalar(@$args) >= 1) { | |
616 | if ($args->[0] eq 'bashcomplete') { | |
617 | shift @$args; | |
57e67ea3 | 618 | &$print_bash_completion($name, @$args); |
7b7f99c9 DM |
619 | return; |
620 | } | |
621 | } | |
622 | ||
7fe1f565 DM |
623 | &$preparefunc() if $preparefunc; |
624 | ||
8d3eb3ce | 625 | my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $read_password_func, $param_mapping_func); |
2026f4b5 | 626 | |
a604bf22 SI |
627 | if (defined $outsub) { |
628 | my $returninfo = $class->map_method_by_name($name)->{returns}; | |
629 | $outsub->($res, $returninfo); | |
630 | } | |
891b798d DM |
631 | }; |
632 | ||
891b798d DM |
633 | sub run_cli_handler { |
634 | my ($class, %params) = @_; | |
635 | ||
636 | $cli_handler_class = $class; | |
637 | ||
638 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
639 | ||
aa9b9af5 | 640 | foreach my $key (keys %params) { |
5ae87c41 | 641 | next if $key eq 'prepare'; |
1042b82c DM |
642 | next if $key eq 'no_init'; # not used anymore |
643 | next if $key eq 'no_rpcenv'; # not used anymore | |
aa9b9af5 DM |
644 | die "unknown parameter '$key'"; |
645 | } | |
646 | ||
5ae87c41 | 647 | my $preparefunc = $params{prepare}; |
891b798d | 648 | |
8d3eb3ce | 649 | my $read_password_func = $class->can('read_password'); |
c44ec0f6 DM |
650 | my $param_mapping_func = $cli_handler_class->can('param_mapping') || |
651 | $class->can('string_param_file_mapping'); | |
891b798d DM |
652 | |
653 | $exename = &$get_exe_name($class); | |
654 | ||
655 | initlog($exename); | |
656 | ||
891b798d | 657 | no strict 'refs'; |
57e67ea3 | 658 | $cmddef = ${"${class}::cmddef"}; |
891b798d | 659 | |
57e67ea3 | 660 | if (ref($cmddef) eq 'ARRAY') { |
8d3eb3ce | 661 | &$handle_simple_cmd(\@ARGV, $read_password_func, $preparefunc, $param_mapping_func); |
891b798d | 662 | } else { |
8d3eb3ce | 663 | &$handle_cmd(\@ARGV, $read_password_func, $preparefunc, $param_mapping_func); |
891b798d DM |
664 | } |
665 | ||
666 | exit 0; | |
e143e9d8 DM |
667 | } |
668 | ||
669 | 1; |