]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::CLIHandler; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
340c9862 | 5 | use JSON; |
e143e9d8 | 6 | |
93ddd7bc | 7 | use PVE::SafeSyslog; |
e143e9d8 DM |
8 | use PVE::Exception qw(raise raise_param_exc); |
9 | use PVE::RESTHandler; | |
0da61ab3 | 10 | use PVE::PTY; |
881eb755 | 11 | use PVE::INotify; |
f53ad23a | 12 | use PVE::CLIFormatter; |
e143e9d8 DM |
13 | |
14 | use base qw(PVE::RESTHandler); | |
15 | ||
c1059f7c TL |
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 | |
9da27228 DM |
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. | |
c1059f7c TL |
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 | # } | |
e143e9d8 DM |
41 | my $cmddef; |
42 | my $exename; | |
891b798d | 43 | my $cli_handler_class; |
e143e9d8 | 44 | |
37f010e7 TL |
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 | ||
4842b651 DC |
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 | ||
d204696c TL |
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 | ||
918140af TL |
104 | my $abort = sub { |
105 | my ($reason, $cmd) = @_; | |
106 | print_usage_short (\*STDERR, $reason, $cmd); | |
107 | exit (-1); | |
108 | }; | |
109 | ||
e143e9d8 | 110 | my $expand_command_name = sub { |
6bd0783c | 111 | my ($def, $cmd, $enforce_exact) = @_; |
e143e9d8 | 112 | |
5082a17b WB |
113 | return $cmd if exists $def->{$cmd}; # command is already complete |
114 | ||
ea5a5084 | 115 | my $is_alias = sub { ref($_[0]) eq 'HASH' && exists($_[0]->{alias}) }; |
0b796806 TL |
116 | my @expanded = grep { /^\Q$cmd\E/ && !$is_alias->($def->{$_}) } keys %$def; |
117 | ||
6bd0783c SI |
118 | return @expanded if !$enforce_exact; |
119 | ||
5082a17b WB |
120 | return $expanded[0] if scalar(@expanded) == 1; # enforce exact match |
121 | ||
122 | return undef; | |
e143e9d8 DM |
123 | }; |
124 | ||
50b89b88 TL |
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 ]; | |
edf3d572 DM |
128 | }; |
129 | ||
50b89b88 TL |
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); | |
f9326469 | 141 | my $cmdstr = $exename; |
50b89b88 TL |
142 | |
143 | if (ref($argv) eq 'ARRAY') { | |
5082a17b | 144 | my $expanded_last_arg; |
50b89b88 TL |
145 | my $last_arg_id = scalar(@$argv) - 1; |
146 | ||
147 | for my $i (0..$last_arg_id) { | |
6bd0783c | 148 | $cmd = $expand_command_name->($def, $argv->[$i], 1); |
5082a17b WB |
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. | |
6bd0783c | 155 | $cmd = $argv->[$i]; |
5082a17b | 156 | } |
f9326469 | 157 | $cmdstr .= " $cmd"; |
6bd0783c SI |
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 | } | |
50b89b88 TL |
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] ]; | |
5082a17b | 169 | return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr); |
50b89b88 TL |
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] ]; | |
5082a17b | 182 | return ($argv->[0], $def, $cmd_args, $expanded_last_arg, $cmdstr); |
50b89b88 | 183 | } |
f9326469 | 184 | return ($cmd, $def, undef, undef, $cmdstr); |
50b89b88 TL |
185 | } |
186 | ||
4c802a57 TL |
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 | ||
968bcf45 | 197 | my $param_cb = $gen_param_mapping_func->($cli_handler_class); |
4c802a57 | 198 | |
f9326469 | 199 | my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd); |
4c802a57 TL |
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') { | |
9da27228 | 211 | my ($class, $name, $arg_param, $fixed_param, undef, $formatter_properties) = @{$def->{$cmd}}; |
4c802a57 TL |
212 | |
213 | $str .= $separator if $oldclass && $oldclass ne $class; | |
214 | $str .= $indent; | |
215 | $str .= $class->usage_str($name, "$prefix $cmd", $arg_param, | |
9da27228 | 216 | $fixed_param, $format, $param_cb, $formatter_properties); |
4c802a57 | 217 | $oldclass = $class; |
50b89b88 TL |
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 | ||
7b2ba123 | 226 | my $substr = $generate->($indent, '', $def->{$cmd}, "$prefix $cmd"); |
50b89b88 TL |
227 | if ($substr) { |
228 | $substr .= $separator if $substr !~ /\Q$separator\E{2}/; | |
229 | $str .= $substr; | |
230 | } | |
4c802a57 TL |
231 | } |
232 | ||
233 | } | |
234 | } else { | |
9da27228 | 235 | my ($class, $name, $arg_param, $fixed_param, undef, $formatter_properties) = @$def; |
4c802a57 TL |
236 | $abort->("unknown command '$cmd'") if !$class; |
237 | ||
238 | $str .= $indent; | |
9da27228 | 239 | $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties); |
4c802a57 TL |
240 | } |
241 | return $str; | |
242 | }; | |
243 | ||
50b89b88 | 244 | return $generate->($indent, $separator, $def, $cmdstr); |
4c802a57 TL |
245 | } |
246 | ||
e143e9d8 | 247 | __PACKAGE__->register_method ({ |
3ef20687 | 248 | name => 'help', |
e143e9d8 DM |
249 | path => 'help', |
250 | method => 'GET', | |
251 | description => "Get help about specified command.", | |
252 | parameters => { | |
3ef20687 | 253 | additionalProperties => 0, |
e143e9d8 | 254 | properties => { |
6627ae09 TL |
255 | 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args', { |
256 | description => 'Shows help for a specific command', | |
edf3d572 | 257 | completion => $complete_command_names, |
6627ae09 | 258 | }), |
e143e9d8 DM |
259 | verbose => { |
260 | description => "Verbose output format.", | |
261 | type => 'boolean', | |
262 | optional => 1, | |
263 | }, | |
264 | }, | |
265 | }, | |
266 | returns => { type => 'null' }, | |
3ef20687 | 267 | |
e143e9d8 DM |
268 | code => sub { |
269 | my ($param) = @_; | |
270 | ||
d204696c | 271 | $assert_initialized->(); |
e143e9d8 | 272 | |
6627ae09 | 273 | my $cmd = $param->{'extra-args'}; |
e143e9d8 | 274 | |
6627ae09 | 275 | my $verbose = defined($cmd) && $cmd; |
e143e9d8 DM |
276 | $verbose = $param->{verbose} if defined($param->{verbose}); |
277 | ||
278 | if (!$cmd) { | |
279 | if ($verbose) { | |
280 | print_usage_verbose(); | |
3ef20687 | 281 | } else { |
e143e9d8 DM |
282 | print_usage_short(\*STDOUT); |
283 | } | |
284 | return undef; | |
285 | } | |
286 | ||
4c802a57 TL |
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+//; | |
e143e9d8 | 294 | |
e143e9d8 DM |
295 | if ($verbose) { |
296 | print "$str\n"; | |
297 | } else { | |
298 | print "USAGE: $str\n"; | |
299 | } | |
300 | ||
301 | return undef; | |
302 | ||
303 | }}); | |
304 | ||
0a5a1eee | 305 | sub print_simple_asciidoc_synopsis { |
d204696c | 306 | $assert_initialized->(); |
fe3f1fde | 307 | |
4c802a57 TL |
308 | my $synopsis = "*${exename}* `help`\n\n"; |
309 | $synopsis .= generate_usage_str('asciidoc'); | |
fe3f1fde DM |
310 | |
311 | return $synopsis; | |
312 | } | |
313 | ||
0a5a1eee | 314 | sub print_asciidoc_synopsis { |
d204696c | 315 | $assert_initialized->(); |
fe3f1fde | 316 | |
fe3f1fde DM |
317 | my $synopsis = ""; |
318 | ||
319 | $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n"; | |
320 | ||
4c802a57 | 321 | $synopsis .= generate_usage_str('asciidoc'); |
fe3f1fde DM |
322 | |
323 | $synopsis .= "\n"; | |
324 | ||
325 | return $synopsis; | |
326 | } | |
327 | ||
e143e9d8 | 328 | sub print_usage_verbose { |
d204696c | 329 | $assert_initialized->(); |
891b798d | 330 | |
e143e9d8 DM |
331 | print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; |
332 | ||
4c802a57 | 333 | my $str = generate_usage_str('full'); |
e143e9d8 | 334 | |
4c802a57 | 335 | print "$str\n"; |
e143e9d8 DM |
336 | } |
337 | ||
338 | sub print_usage_short { | |
50b89b88 | 339 | my ($fd, $msg, $cmd) = @_; |
e143e9d8 | 340 | |
d204696c | 341 | $assert_initialized->(); |
891b798d | 342 | |
e143e9d8 | 343 | print $fd "ERROR: $msg\n" if $msg; |
7b2ba123 | 344 | print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; |
e143e9d8 | 345 | |
c701c565 | 346 | print {$fd} generate_usage_str('short', $cmd, ' ' x 7, $cmd ? '' : "\n", sub { |
4c802a57 TL |
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; | |
50b89b88 TL |
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; | |
4c802a57 | 355 | } else { |
50b89b88 | 356 | # both are either from the same class or subcommands |
4c802a57 TL |
357 | return $a cmp $b; |
358 | } | |
359 | } keys %$h; | |
360 | }); | |
e143e9d8 DM |
361 | } |
362 | ||
d8053c08 | 363 | my $print_bash_completion = sub { |
57e67ea3 | 364 | my ($simple_cmd, $bash_command, $cur, $prev) = @_; |
d8053c08 DM |
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 | ||
58d9e664 | 375 | my $args = PVE::Tools::split_args($cmdline); |
5fa768fc | 376 | shift @$args; # no need for program name |
d8053c08 DM |
377 | my $print_result = sub { |
378 | foreach my $p (@_) { | |
2c48a665 | 379 | print "$p\n" if $p =~ m/^\Q$cur\E/; |
d8053c08 DM |
380 | } |
381 | }; | |
382 | ||
5fa768fc TL |
383 | my ($cmd, $def) = ($simple_cmd, $cmddef); |
384 | if (!$simple_cmd) { | |
0ed80698 | 385 | ($cmd, $def, $args, my $expanded) = resolve_cmd($args); |
50b89b88 | 386 | |
5082a17b WB |
387 | if (defined($expanded) && $prev ne $expanded) { |
388 | print "$expanded\n"; | |
50b89b88 TL |
389 | return; |
390 | } | |
5082a17b WB |
391 | |
392 | if (ref($def) eq 'HASH') { | |
393 | &$print_result(@{$get_commands->($def)}); | |
d8053c08 DM |
394 | return; |
395 | } | |
d8053c08 | 396 | } |
d8053c08 DM |
397 | return if !$def; |
398 | ||
5fa768fc TL |
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; | |
d8053c08 DM |
403 | |
404 | my $skip_param = {}; | |
405 | ||
9da27228 | 406 | my ($class, $name, $arg_param, $uri_param, undef, $formatter_properties) = @$def; |
d8053c08 DM |
407 | $arg_param //= []; |
408 | $uri_param //= {}; | |
409 | ||
d90a2fd0 DM |
410 | $arg_param = [ $arg_param ] if !ref($arg_param); |
411 | ||
d8053c08 DM |
412 | map { $skip_param->{$_} = 1; } @$arg_param; |
413 | map { $skip_param->{$_} = 1; } keys %$uri_param; | |
414 | ||
d8053c08 DM |
415 | my $info = $class->map_method_by_name($name); |
416 | ||
9da27228 DM |
417 | my $prop = { %{$info->{parameters}->{properties}} }; # copy |
418 | $prop = { %$prop, %$formatter_properties } if $formatter_properties; | |
d8053c08 DM |
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') { | |
58d9e664 | 426 | my $res = $d->{completion}->($cmd, $pname, $cur, $args); |
d8053c08 DM |
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 | |
5fa768fc TL |
437 | if ($pos < scalar(@$arg_param)) { |
438 | my $pname = $arg_param->[$pos]; | |
d8053c08 DM |
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 | ||
1f130ba6 DM |
463 | sub verify_api { |
464 | my ($class) = @_; | |
465 | ||
466 | # simply verify all registered methods | |
467 | PVE::RESTHandler::validate_method_schemas(); | |
468 | } | |
469 | ||
8f3712f8 DM |
470 | my $get_exe_name = sub { |
471 | my ($class) = @_; | |
3ef20687 | 472 | |
8f3712f8 DM |
473 | my $name = $class; |
474 | $name =~ s/^.*:://; | |
475 | $name =~ s/_/-/g; | |
476 | ||
477 | return $name; | |
478 | }; | |
479 | ||
c45707a0 DM |
480 | sub generate_bash_completions { |
481 | my ($class) = @_; | |
482 | ||
483 | # generate bash completion config | |
484 | ||
8f3712f8 | 485 | $exename = &$get_exe_name($class); |
c45707a0 DM |
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 | ||
e4a1d8e2 | 495 | complete -o default -C '$exename bashcomplete' $exename |
c45707a0 DM |
496 | __EOD__ |
497 | } | |
498 | ||
ad2cc599 CE |
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 | ||
fe3f1fde DM |
523 | sub generate_asciidoc_synopsys { |
524 | my ($class) = @_; | |
0a5a1eee FG |
525 | $class->generate_asciidoc_synopsis(); |
526 | }; | |
527 | ||
528 | sub generate_asciidoc_synopsis { | |
529 | my ($class) = @_; | |
fe3f1fde DM |
530 | |
531 | $cli_handler_class = $class; | |
532 | ||
533 | $exename = &$get_exe_name($class); | |
534 | ||
535 | no strict 'refs'; | |
536 | my $def = ${"${class}::cmddef"}; | |
57e67ea3 | 537 | $cmddef = $def; |
fe3f1fde DM |
538 | |
539 | if (ref($def) eq 'ARRAY') { | |
4c802a57 | 540 | print_simple_asciidoc_synopsis(); |
fe3f1fde | 541 | } else { |
fe3f1fde DM |
542 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; |
543 | ||
0a5a1eee | 544 | print_asciidoc_synopsis(); |
fe3f1fde DM |
545 | } |
546 | } | |
547 | ||
7b7f99c9 DM |
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 | ||
891b798d | 555 | my $handle_cmd = sub { |
968bcf45 | 556 | my ($args, $preparefunc, $param_cb) = @_; |
e143e9d8 | 557 | |
6627ae09 | 558 | $cmddef->{help} = [ __PACKAGE__, 'help', ['extra-args'] ]; |
e143e9d8 | 559 | |
f9326469 | 560 | my ($cmd, $def, $cmd_args, undef, $cmd_str) = resolve_cmd($args); |
7b7f99c9 | 561 | |
918140af TL |
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') { | |
e143e9d8 DM |
567 | PVE::RESTHandler::validate_method_schemas(); |
568 | return; | |
7b7f99c9 DM |
569 | } |
570 | ||
571 | $cli_handler_class->setup_environment(); | |
572 | ||
573 | if ($cmd eq 'bashcomplete') { | |
50b89b88 | 574 | &$print_bash_completion(undef, @$cmd_args); |
d8053c08 | 575 | return; |
e143e9d8 DM |
576 | } |
577 | ||
50b89b88 | 578 | # checked special commands, if def is still a hash we got an incomplete sub command |
131f316b | 579 | $abort->("incomplete command '$cmd_str'", $args) if ref($def) eq 'HASH'; |
8e3e9929 | 580 | |
50b89b88 | 581 | &$preparefunc() if $preparefunc; |
e143e9d8 | 582 | |
9da27228 | 583 | my ($class, $name, $arg_param, $uri_param, $outsub, $formatter_properties) = @{$def || []}; |
50b89b88 | 584 | $abort->("unknown command '$cmd_str'") if !$class; |
e143e9d8 | 585 | |
9da27228 DM |
586 | my ($res, $formatter_params) = $class->cli_handler( |
587 | $cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb, $formatter_properties); | |
2026f4b5 | 588 | |
a604bf22 | 589 | if (defined $outsub) { |
ec3cbded | 590 | my $result_schema = $class->map_method_by_name($name)->{returns}; |
9da27228 | 591 | $outsub->($res, $result_schema, $formatter_params); |
a604bf22 | 592 | } |
891b798d | 593 | }; |
2026f4b5 | 594 | |
891b798d | 595 | my $handle_simple_cmd = sub { |
968bcf45 | 596 | my ($args, $preparefunc, $param_cb) = @_; |
2026f4b5 | 597 | |
9da27228 | 598 | my ($class, $name, $arg_param, $uri_param, $outsub, $formatter_properties) = @{$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 | ||
9da27228 DM |
625 | my ($res, $formatter_params) = $class->cli_handler( |
626 | $name, $name, \@ARGV, $arg_param, $uri_param, $param_cb, $formatter_properties); | |
2026f4b5 | 627 | |
a604bf22 | 628 | if (defined $outsub) { |
ec3cbded | 629 | my $result_schema = $class->map_method_by_name($name)->{returns}; |
9da27228 | 630 | $outsub->($res, $result_schema, $formatter_params); |
a604bf22 | 631 | } |
891b798d DM |
632 | }; |
633 | ||
891b798d DM |
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 | ||
aa9b9af5 | 641 | foreach my $key (keys %params) { |
5ae87c41 | 642 | next if $key eq 'prepare'; |
1042b82c DM |
643 | next if $key eq 'no_init'; # not used anymore |
644 | next if $key eq 'no_rpcenv'; # not used anymore | |
aa9b9af5 DM |
645 | die "unknown parameter '$key'"; |
646 | } | |
647 | ||
5ae87c41 | 648 | my $preparefunc = $params{prepare}; |
891b798d | 649 | |
968bcf45 | 650 | my $param_cb = $gen_param_mapping_func->($cli_handler_class); |
891b798d DM |
651 | |
652 | $exename = &$get_exe_name($class); | |
653 | ||
57d44151 DM |
654 | my $logid = $ENV{PVE_LOG_ID} || $exename; |
655 | initlog($logid); | |
891b798d | 656 | |
891b798d | 657 | no strict 'refs'; |
57e67ea3 | 658 | $cmddef = ${"${class}::cmddef"}; |
891b798d | 659 | |
57e67ea3 | 660 | if (ref($cmddef) eq 'ARRAY') { |
968bcf45 | 661 | $handle_simple_cmd->(\@ARGV, $preparefunc, $param_cb); |
891b798d | 662 | } else { |
968bcf45 | 663 | $handle_cmd->(\@ARGV, $preparefunc, $param_cb); |
891b798d DM |
664 | } |
665 | ||
666 | exit 0; | |
e143e9d8 DM |
667 | } |
668 | ||
669 | 1; |