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