]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::CLIHandler; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
d8053c08 | 5 | use Data::Dumper; |
e143e9d8 | 6 | |
93ddd7bc | 7 | use PVE::SafeSyslog; |
e143e9d8 DM |
8 | use PVE::Exception qw(raise raise_param_exc); |
9 | use PVE::RESTHandler; | |
881eb755 | 10 | use PVE::INotify; |
e143e9d8 DM |
11 | |
12 | use base qw(PVE::RESTHandler); | |
13 | ||
14 | my $cmddef; | |
15 | my $exename; | |
891b798d | 16 | my $cli_handler_class; |
e143e9d8 DM |
17 | |
18 | my $expand_command_name = sub { | |
19 | my ($def, $cmd) = @_; | |
20 | ||
21 | if (!$def->{$cmd}) { | |
22 | my $expanded; | |
23 | for my $k (keys(%$def)) { | |
24 | if ($k =~ m/^$cmd/) { | |
25 | if ($expanded) { | |
26 | $expanded = undef; # more than one match | |
27 | last; | |
28 | } else { | |
29 | $expanded = $k; | |
30 | } | |
31 | } | |
32 | } | |
33 | $cmd = $expanded if $expanded; | |
34 | } | |
35 | return $cmd; | |
36 | }; | |
37 | ||
edf3d572 DM |
38 | my $complete_command_names = sub { |
39 | my $res = []; | |
40 | ||
41 | return if ref($cmddef) ne 'HASH'; | |
42 | ||
43 | foreach my $cmd (keys %$cmddef) { | |
44 | next if $cmd eq 'help'; | |
45 | push @$res, $cmd; | |
46 | } | |
47 | ||
48 | return $res; | |
49 | }; | |
50 | ||
e143e9d8 DM |
51 | __PACKAGE__->register_method ({ |
52 | name => 'help', | |
53 | path => 'help', | |
54 | method => 'GET', | |
55 | description => "Get help about specified command.", | |
56 | parameters => { | |
57 | additionalProperties => 0, | |
58 | properties => { | |
59 | cmd => { | |
60 | description => "Command name", | |
61 | type => 'string', | |
62 | optional => 1, | |
edf3d572 | 63 | completion => $complete_command_names, |
e143e9d8 DM |
64 | }, |
65 | verbose => { | |
66 | description => "Verbose output format.", | |
67 | type => 'boolean', | |
68 | optional => 1, | |
69 | }, | |
70 | }, | |
71 | }, | |
72 | returns => { type => 'null' }, | |
73 | ||
74 | code => sub { | |
75 | my ($param) = @_; | |
76 | ||
891b798d | 77 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); |
e143e9d8 DM |
78 | |
79 | my $cmd = $param->{cmd}; | |
80 | ||
81 | my $verbose = defined($cmd) && $cmd; | |
82 | $verbose = $param->{verbose} if defined($param->{verbose}); | |
83 | ||
84 | if (!$cmd) { | |
85 | if ($verbose) { | |
86 | print_usage_verbose(); | |
87 | } else { | |
88 | print_usage_short(\*STDOUT); | |
89 | } | |
90 | return undef; | |
91 | } | |
92 | ||
93 | $cmd = &$expand_command_name($cmddef, $cmd); | |
94 | ||
95 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []}; | |
96 | ||
97 | raise_param_exc({ cmd => "no such command '$cmd'"}) if !$class; | |
98 | ||
891b798d | 99 | my $pwcallback = $cli_handler_class->can('read_password'); |
4845032a | 100 | my $stringfilemap = $cli_handler_class->can('string_param_file_mapping'); |
e143e9d8 | 101 | |
891b798d | 102 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, |
4845032a FG |
103 | $verbose ? 'full' : 'short', $pwcallback, |
104 | $stringfilemap); | |
e143e9d8 DM |
105 | if ($verbose) { |
106 | print "$str\n"; | |
107 | } else { | |
108 | print "USAGE: $str\n"; | |
109 | } | |
110 | ||
111 | return undef; | |
112 | ||
113 | }}); | |
114 | ||
0a5a1eee | 115 | sub print_simple_asciidoc_synopsis { |
fe3f1fde DM |
116 | my ($class, $name, $arg_param, $uri_param) = @_; |
117 | ||
118 | die "not initialized" if !$cli_handler_class; | |
119 | ||
120 | my $pwcallback = $cli_handler_class->can('read_password'); | |
4845032a | 121 | my $stringfilemap = $cli_handler_class->can('string_param_file_mapping'); |
fe3f1fde DM |
122 | |
123 | my $synopsis = "*${name}* `help`\n\n"; | |
124 | ||
4845032a FG |
125 | $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param, |
126 | 'asciidoc', $pwcallback, $stringfilemap); | |
fe3f1fde DM |
127 | |
128 | return $synopsis; | |
129 | } | |
130 | ||
0a5a1eee | 131 | sub print_asciidoc_synopsis { |
fe3f1fde DM |
132 | |
133 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); | |
134 | ||
135 | my $pwcallback = $cli_handler_class->can('read_password'); | |
4845032a | 136 | my $stringfilemap = $cli_handler_class->can('string_param_file_mapping'); |
fe3f1fde DM |
137 | |
138 | my $synopsis = ""; | |
139 | ||
140 | $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n"; | |
141 | ||
142 | my $oldclass; | |
143 | foreach my $cmd (sort keys %$cmddef) { | |
144 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
145 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, | |
4845032a FG |
146 | $uri_param, 'asciidoc', $pwcallback, |
147 | $stringfilemap); | |
fe3f1fde DM |
148 | $synopsis .= "\n" if $oldclass && $oldclass ne $class; |
149 | ||
150 | $synopsis .= "$str\n\n"; | |
151 | $oldclass = $class; | |
152 | } | |
153 | ||
154 | $synopsis .= "\n"; | |
155 | ||
156 | return $synopsis; | |
157 | } | |
158 | ||
e143e9d8 DM |
159 | sub print_usage_verbose { |
160 | ||
891b798d DM |
161 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); |
162 | ||
163 | my $pwcallback = $cli_handler_class->can('read_password'); | |
4845032a | 164 | my $stringfilemap = $cli_handler_class->can('string_param_file_mapping'); |
e143e9d8 DM |
165 | |
166 | print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; | |
167 | ||
168 | foreach my $cmd (sort keys %$cmddef) { | |
169 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
891b798d | 170 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, |
4845032a | 171 | 'full', $pwcallback, $stringfilemap); |
e143e9d8 DM |
172 | print "$str\n\n"; |
173 | } | |
174 | } | |
175 | ||
176 | sub sorted_commands { | |
177 | return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef; | |
178 | } | |
179 | ||
180 | sub print_usage_short { | |
181 | my ($fd, $msg) = @_; | |
182 | ||
891b798d DM |
183 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); |
184 | ||
185 | my $pwcallback = $cli_handler_class->can('read_password'); | |
4845032a | 186 | my $stringfilemap = $cli_handler_class->can('string_param_file_mapping'); |
e143e9d8 DM |
187 | |
188 | print $fd "ERROR: $msg\n" if $msg; | |
189 | print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n"; | |
190 | ||
191 | my $oldclass; | |
192 | foreach my $cmd (sorted_commands()) { | |
193 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
4845032a | 194 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap); |
e143e9d8 DM |
195 | print $fd "\n" if $oldclass && $oldclass ne $class; |
196 | print $fd " $str"; | |
197 | $oldclass = $class; | |
198 | } | |
199 | } | |
200 | ||
d8053c08 DM |
201 | my $print_bash_completion = sub { |
202 | my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_; | |
203 | ||
204 | my $debug = 0; | |
205 | ||
206 | return if !(defined($cur) && defined($prev) && defined($bash_command)); | |
207 | return if !defined($ENV{COMP_LINE}); | |
208 | return if !defined($ENV{COMP_POINT}); | |
209 | ||
210 | my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); | |
211 | print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug; | |
212 | ||
58d9e664 DM |
213 | my $args = PVE::Tools::split_args($cmdline); |
214 | my $pos = scalar(@$args) - 2; | |
d8053c08 DM |
215 | $pos += 1 if $cmdline =~ m/\s+$/; |
216 | ||
217 | print STDERR "CMDLINE:$pos:$cmdline\n" if $debug; | |
218 | ||
219 | return if $pos < 0; | |
220 | ||
221 | my $print_result = sub { | |
222 | foreach my $p (@_) { | |
223 | print "$p\n" if $p =~ m/^$cur/; | |
224 | } | |
225 | }; | |
226 | ||
227 | my $cmd; | |
228 | if ($simple_cmd) { | |
229 | $cmd = $simple_cmd; | |
230 | } else { | |
231 | if ($pos == 0) { | |
232 | &$print_result(keys %$cmddef); | |
233 | return; | |
234 | } | |
58d9e664 | 235 | $cmd = $args->[1]; |
d8053c08 DM |
236 | } |
237 | ||
238 | my $def = $cmddef->{$cmd}; | |
239 | return if !$def; | |
240 | ||
241 | print STDERR "CMDLINE1:$pos:$cmdline\n" if $debug; | |
242 | ||
243 | my $skip_param = {}; | |
244 | ||
245 | my ($class, $name, $arg_param, $uri_param) = @$def; | |
246 | $arg_param //= []; | |
247 | $uri_param //= {}; | |
248 | ||
d90a2fd0 DM |
249 | $arg_param = [ $arg_param ] if !ref($arg_param); |
250 | ||
d8053c08 DM |
251 | map { $skip_param->{$_} = 1; } @$arg_param; |
252 | map { $skip_param->{$_} = 1; } keys %$uri_param; | |
253 | ||
254 | my $fpcount = scalar(@$arg_param); | |
255 | ||
256 | my $info = $class->map_method_by_name($name); | |
257 | ||
258 | my $schema = $info->{parameters}; | |
259 | my $prop = $schema->{properties}; | |
260 | ||
261 | my $print_parameter_completion = sub { | |
262 | my ($pname) = @_; | |
263 | my $d = $prop->{$pname}; | |
264 | if ($d->{completion}) { | |
265 | my $vt = ref($d->{completion}); | |
266 | if ($vt eq 'CODE') { | |
58d9e664 | 267 | my $res = $d->{completion}->($cmd, $pname, $cur, $args); |
d8053c08 DM |
268 | &$print_result(@$res); |
269 | } | |
270 | } elsif ($d->{type} eq 'boolean') { | |
271 | &$print_result('0', '1'); | |
272 | } elsif ($d->{enum}) { | |
273 | &$print_result(@{$d->{enum}}); | |
274 | } | |
275 | }; | |
276 | ||
277 | # positional arguments | |
278 | $pos += 1 if $simple_cmd; | |
279 | if ($fpcount && $pos <= $fpcount) { | |
280 | my $pname = $arg_param->[$pos -1]; | |
281 | &$print_parameter_completion($pname); | |
282 | return; | |
283 | } | |
284 | ||
285 | my @option_list = (); | |
286 | foreach my $key (keys %$prop) { | |
287 | next if $skip_param->{$key}; | |
288 | push @option_list, "--$key"; | |
289 | } | |
290 | ||
291 | if ($cur =~ m/^-/) { | |
292 | &$print_result(@option_list); | |
293 | return; | |
294 | } | |
295 | ||
296 | if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) { | |
297 | my $pname = $1; | |
298 | &$print_parameter_completion($pname); | |
299 | return; | |
300 | } | |
301 | ||
302 | &$print_result(@option_list); | |
303 | }; | |
304 | ||
1f130ba6 DM |
305 | sub verify_api { |
306 | my ($class) = @_; | |
307 | ||
308 | # simply verify all registered methods | |
309 | PVE::RESTHandler::validate_method_schemas(); | |
310 | } | |
311 | ||
8f3712f8 DM |
312 | my $get_exe_name = sub { |
313 | my ($class) = @_; | |
314 | ||
315 | my $name = $class; | |
316 | $name =~ s/^.*:://; | |
317 | $name =~ s/_/-/g; | |
318 | ||
319 | return $name; | |
320 | }; | |
321 | ||
c45707a0 DM |
322 | sub generate_bash_completions { |
323 | my ($class) = @_; | |
324 | ||
325 | # generate bash completion config | |
326 | ||
8f3712f8 | 327 | $exename = &$get_exe_name($class); |
c45707a0 DM |
328 | |
329 | print <<__EOD__; | |
330 | # $exename bash completion | |
331 | ||
332 | # see http://tiswww.case.edu/php/chet/bash/FAQ | |
333 | # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion | |
334 | # this modifies global var, but I found no better way | |
335 | COMP_WORDBREAKS=\${COMP_WORDBREAKS//:} | |
336 | ||
e4a1d8e2 | 337 | complete -o default -C '$exename bashcomplete' $exename |
c45707a0 DM |
338 | __EOD__ |
339 | } | |
340 | ||
7fe1f565 | 341 | sub find_cli_class_source { |
8f3712f8 | 342 | my ($name) = @_; |
7fe1f565 DM |
343 | |
344 | my $filename; | |
345 | ||
8f3712f8 DM |
346 | $name =~ s/-/_/g; |
347 | ||
348 | my $cpath = "PVE/CLI/${name}.pm"; | |
349 | my $spath = "PVE/Service/${name}.pm"; | |
7fe1f565 | 350 | foreach my $p (@INC) { |
edf3d572 DM |
351 | foreach my $s (($cpath, $spath)) { |
352 | my $testfn = "$p/$s"; | |
353 | if (-f $testfn) { | |
354 | $filename = $testfn; | |
355 | last; | |
356 | } | |
7fe1f565 | 357 | } |
edf3d572 | 358 | last if defined($filename); |
7fe1f565 DM |
359 | } |
360 | ||
361 | return $filename; | |
362 | } | |
363 | ||
fe3f1fde DM |
364 | sub generate_asciidoc_synopsys { |
365 | my ($class) = @_; | |
0a5a1eee FG |
366 | $class->generate_asciidoc_synopsis(); |
367 | }; | |
368 | ||
369 | sub generate_asciidoc_synopsis { | |
370 | my ($class) = @_; | |
fe3f1fde DM |
371 | |
372 | $cli_handler_class = $class; | |
373 | ||
374 | $exename = &$get_exe_name($class); | |
375 | ||
376 | no strict 'refs'; | |
377 | my $def = ${"${class}::cmddef"}; | |
378 | ||
379 | if (ref($def) eq 'ARRAY') { | |
0a5a1eee | 380 | print_simple_asciidoc_synopsis(@$def); |
fe3f1fde DM |
381 | } else { |
382 | $cmddef = $def; | |
383 | ||
384 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
385 | ||
0a5a1eee | 386 | print_asciidoc_synopsis(); |
fe3f1fde DM |
387 | } |
388 | } | |
389 | ||
7b7f99c9 DM |
390 | # overwrite this if you want to run/setup things early |
391 | sub setup_environment { | |
392 | my ($class) = @_; | |
393 | ||
394 | # do nothing by default | |
395 | } | |
396 | ||
891b798d | 397 | my $handle_cmd = sub { |
bf27456b | 398 | my ($def, $cmdname, $cmd, $args, $pwcallback, $preparefunc, $stringfilemap) = @_; |
e143e9d8 DM |
399 | |
400 | $cmddef = $def; | |
401 | $exename = $cmdname; | |
402 | ||
403 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
404 | ||
7b7f99c9 DM |
405 | # call verifyapi before setup_environment(), because we do not want to |
406 | # execute any real code in this case | |
407 | ||
408 | if (!$cmd) { | |
e143e9d8 DM |
409 | print_usage_short (\*STDERR, "no command specified"); |
410 | exit (-1); | |
411 | } elsif ($cmd eq 'verifyapi') { | |
412 | PVE::RESTHandler::validate_method_schemas(); | |
413 | return; | |
7b7f99c9 DM |
414 | } |
415 | ||
416 | $cli_handler_class->setup_environment(); | |
417 | ||
418 | if ($cmd eq 'bashcomplete') { | |
d8053c08 DM |
419 | &$print_bash_completion($cmddef, 0, @$args); |
420 | return; | |
e143e9d8 DM |
421 | } |
422 | ||
8e3e9929 WB |
423 | &$preparefunc() if $preparefunc; |
424 | ||
e143e9d8 DM |
425 | $cmd = &$expand_command_name($cmddef, $cmd); |
426 | ||
427 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []}; | |
428 | ||
429 | if (!$class) { | |
430 | print_usage_short (\*STDERR, "unknown command '$cmd'"); | |
431 | exit (-1); | |
432 | } | |
433 | ||
434 | my $prefix = "$exename $cmd"; | |
408976c6 | 435 | my $res = $class->cli_handler($prefix, $name, \@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap); |
2026f4b5 DM |
436 | |
437 | &$outsub($res) if $outsub; | |
891b798d | 438 | }; |
2026f4b5 | 439 | |
891b798d | 440 | my $handle_simple_cmd = sub { |
bf27456b | 441 | my ($def, $args, $pwcallback, $preparefunc, $stringfilemap) = @_; |
2026f4b5 DM |
442 | |
443 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def}; | |
444 | die "no class specified" if !$class; | |
445 | ||
d8053c08 | 446 | if (scalar(@$args) >= 1) { |
2026f4b5 DM |
447 | if ($args->[0] eq 'help') { |
448 | my $str = "USAGE: $name help\n"; | |
4845032a | 449 | $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap); |
2026f4b5 DM |
450 | print STDERR "$str\n\n"; |
451 | return; | |
452 | } elsif ($args->[0] eq 'verifyapi') { | |
453 | PVE::RESTHandler::validate_method_schemas(); | |
454 | return; | |
2026f4b5 | 455 | } |
e143e9d8 | 456 | } |
2026f4b5 | 457 | |
7b7f99c9 DM |
458 | $cli_handler_class->setup_environment(); |
459 | ||
460 | if (scalar(@$args) >= 1) { | |
461 | if ($args->[0] eq 'bashcomplete') { | |
462 | shift @$args; | |
463 | &$print_bash_completion({ $name => $def }, $name, @$args); | |
464 | return; | |
465 | } | |
466 | } | |
467 | ||
7fe1f565 DM |
468 | &$preparefunc() if $preparefunc; |
469 | ||
408976c6 | 470 | my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap); |
2026f4b5 DM |
471 | |
472 | &$outsub($res) if $outsub; | |
891b798d DM |
473 | }; |
474 | ||
475 | sub run_cli { | |
476 | my ($class, $pwcallback, $podfn, $preparefunc) = @_; | |
477 | ||
b7ef63ab DM |
478 | # Note: "depreciated function run_cli - use run_cli_handler instead"; |
479 | # silently ignore $podfn , which is no longer supported. | |
480 | ||
481 | die "password callback is no longer supported" if $pwcallback; | |
482 | ||
483 | run_cli_handler($class, prepare => $preparefunc); | |
891b798d DM |
484 | } |
485 | ||
486 | sub run_cli_handler { | |
487 | my ($class, %params) = @_; | |
488 | ||
489 | $cli_handler_class = $class; | |
490 | ||
491 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
492 | ||
aa9b9af5 | 493 | foreach my $key (keys %params) { |
5ae87c41 | 494 | next if $key eq 'prepare'; |
1042b82c DM |
495 | next if $key eq 'no_init'; # not used anymore |
496 | next if $key eq 'no_rpcenv'; # not used anymore | |
aa9b9af5 DM |
497 | die "unknown parameter '$key'"; |
498 | } | |
499 | ||
5ae87c41 | 500 | my $preparefunc = $params{prepare}; |
891b798d DM |
501 | |
502 | my $pwcallback = $class->can('read_password'); | |
408976c6 | 503 | my $stringfilemap = $class->can('string_param_file_mapping'); |
891b798d DM |
504 | |
505 | $exename = &$get_exe_name($class); | |
506 | ||
507 | initlog($exename); | |
508 | ||
891b798d DM |
509 | no strict 'refs'; |
510 | my $def = ${"${class}::cmddef"}; | |
511 | ||
512 | if (ref($def) eq 'ARRAY') { | |
bf27456b | 513 | &$handle_simple_cmd($def, \@ARGV, $pwcallback, $preparefunc, $stringfilemap); |
891b798d DM |
514 | } else { |
515 | $cmddef = $def; | |
516 | my $cmd = shift @ARGV; | |
bf27456b | 517 | &$handle_cmd($cmddef, $exename, $cmd, \@ARGV, $pwcallback, $preparefunc, $stringfilemap); |
891b798d DM |
518 | } |
519 | ||
520 | exit 0; | |
e143e9d8 DM |
521 | } |
522 | ||
523 | 1; |