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