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