]>
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; | |
10 | use PVE::PodParser; | |
11 | ||
12 | use base qw(PVE::RESTHandler); | |
13 | ||
14 | my $cmddef; | |
15 | my $exename; | |
16 | ||
17 | my $expand_command_name = sub { | |
18 | my ($def, $cmd) = @_; | |
19 | ||
20 | if (!$def->{$cmd}) { | |
21 | my $expanded; | |
22 | for my $k (keys(%$def)) { | |
23 | if ($k =~ m/^$cmd/) { | |
24 | if ($expanded) { | |
25 | $expanded = undef; # more than one match | |
26 | last; | |
27 | } else { | |
28 | $expanded = $k; | |
29 | } | |
30 | } | |
31 | } | |
32 | $cmd = $expanded if $expanded; | |
33 | } | |
34 | return $cmd; | |
35 | }; | |
36 | ||
37 | __PACKAGE__->register_method ({ | |
38 | name => 'help', | |
39 | path => 'help', | |
40 | method => 'GET', | |
41 | description => "Get help about specified command.", | |
42 | parameters => { | |
43 | additionalProperties => 0, | |
44 | properties => { | |
45 | cmd => { | |
46 | description => "Command name", | |
47 | type => 'string', | |
48 | optional => 1, | |
49 | }, | |
50 | verbose => { | |
51 | description => "Verbose output format.", | |
52 | type => 'boolean', | |
53 | optional => 1, | |
54 | }, | |
55 | }, | |
56 | }, | |
57 | returns => { type => 'null' }, | |
58 | ||
59 | code => sub { | |
60 | my ($param) = @_; | |
61 | ||
62 | die "not initialized" if !($cmddef && $exename); | |
63 | ||
64 | my $cmd = $param->{cmd}; | |
65 | ||
66 | my $verbose = defined($cmd) && $cmd; | |
67 | $verbose = $param->{verbose} if defined($param->{verbose}); | |
68 | ||
69 | if (!$cmd) { | |
70 | if ($verbose) { | |
71 | print_usage_verbose(); | |
72 | } else { | |
73 | print_usage_short(\*STDOUT); | |
74 | } | |
75 | return undef; | |
76 | } | |
77 | ||
78 | $cmd = &$expand_command_name($cmddef, $cmd); | |
79 | ||
80 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []}; | |
81 | ||
82 | raise_param_exc({ cmd => "no such command '$cmd'"}) if !$class; | |
83 | ||
84 | ||
85 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ? 'full' : 'short'); | |
86 | if ($verbose) { | |
87 | print "$str\n"; | |
88 | } else { | |
89 | print "USAGE: $str\n"; | |
90 | } | |
91 | ||
92 | return undef; | |
93 | ||
94 | }}); | |
95 | ||
7fe1f565 DM |
96 | sub print_simple_pod_manpage { |
97 | my ($podfn, $class, $name, $arg_param, $uri_param) = @_; | |
98 | ||
99 | my $synopsis = " $name help\n\n"; | |
100 | my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long'); | |
101 | $str =~ s/^USAGE://; | |
102 | $str =~ s/\n/\n /g; | |
103 | $synopsis .= $str; | |
104 | ||
105 | my $parser = PVE::PodParser->new(); | |
106 | $parser->{include}->{synopsis} = $synopsis; | |
107 | $parser->parse_from_file($podfn); | |
108 | } | |
109 | ||
e143e9d8 DM |
110 | sub print_pod_manpage { |
111 | my ($podfn) = @_; | |
112 | ||
113 | die "not initialized" if !($cmddef && $exename); | |
114 | die "no pod file specified" if !$podfn; | |
115 | ||
116 | my $synopsis = ""; | |
117 | ||
118 | $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; | |
119 | ||
120 | my $style = 'full'; # or should we use 'short'? | |
121 | my $oldclass; | |
122 | foreach my $cmd (sorted_commands()) { | |
123 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
124 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, | |
125 | $uri_param, $style); | |
126 | $str =~ s/^USAGE: //; | |
127 | ||
128 | $synopsis .= "\n" if $oldclass && $oldclass ne $class; | |
129 | $str =~ s/\n/\n /g; | |
130 | $synopsis .= " $str\n\n"; | |
131 | $oldclass = $class; | |
132 | } | |
133 | ||
134 | $synopsis .= "\n"; | |
135 | ||
136 | my $parser = PVE::PodParser->new(); | |
137 | $parser->{include}->{synopsis} = $synopsis; | |
138 | $parser->parse_from_file($podfn); | |
139 | } | |
140 | ||
141 | sub print_usage_verbose { | |
142 | ||
143 | die "not initialized" if !($cmddef && $exename); | |
144 | ||
145 | print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; | |
146 | ||
147 | foreach my $cmd (sort keys %$cmddef) { | |
148 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
149 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full'); | |
150 | print "$str\n\n"; | |
151 | } | |
152 | } | |
153 | ||
154 | sub sorted_commands { | |
155 | return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef; | |
156 | } | |
157 | ||
158 | sub print_usage_short { | |
159 | my ($fd, $msg) = @_; | |
160 | ||
161 | die "not initialized" if !($cmddef && $exename); | |
162 | ||
163 | print $fd "ERROR: $msg\n" if $msg; | |
164 | print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n"; | |
165 | ||
166 | my $oldclass; | |
167 | foreach my $cmd (sorted_commands()) { | |
168 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
169 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short'); | |
170 | print $fd "\n" if $oldclass && $oldclass ne $class; | |
171 | print $fd " $str"; | |
172 | $oldclass = $class; | |
173 | } | |
174 | } | |
175 | ||
d8053c08 DM |
176 | my $print_bash_completion = sub { |
177 | my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_; | |
178 | ||
179 | my $debug = 0; | |
180 | ||
181 | return if !(defined($cur) && defined($prev) && defined($bash_command)); | |
182 | return if !defined($ENV{COMP_LINE}); | |
183 | return if !defined($ENV{COMP_POINT}); | |
184 | ||
185 | my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); | |
186 | print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug; | |
187 | ||
188 | # fixme: shell quoting?? | |
189 | my @args = split(/\s+/, $cmdline); | |
190 | my $pos = scalar(@args) - 2; | |
191 | $pos += 1 if $cmdline =~ m/\s+$/; | |
192 | ||
193 | print STDERR "CMDLINE:$pos:$cmdline\n" if $debug; | |
194 | ||
195 | return if $pos < 0; | |
196 | ||
197 | my $print_result = sub { | |
198 | foreach my $p (@_) { | |
199 | print "$p\n" if $p =~ m/^$cur/; | |
200 | } | |
201 | }; | |
202 | ||
203 | my $cmd; | |
204 | if ($simple_cmd) { | |
205 | $cmd = $simple_cmd; | |
206 | } else { | |
207 | if ($pos == 0) { | |
208 | &$print_result(keys %$cmddef); | |
209 | return; | |
210 | } | |
211 | $cmd = $args[1]; | |
212 | } | |
213 | ||
214 | my $def = $cmddef->{$cmd}; | |
215 | return if !$def; | |
216 | ||
217 | print STDERR "CMDLINE1:$pos:$cmdline\n" if $debug; | |
218 | ||
219 | my $skip_param = {}; | |
220 | ||
221 | my ($class, $name, $arg_param, $uri_param) = @$def; | |
222 | $arg_param //= []; | |
223 | $uri_param //= {}; | |
224 | ||
225 | map { $skip_param->{$_} = 1; } @$arg_param; | |
226 | map { $skip_param->{$_} = 1; } keys %$uri_param; | |
227 | ||
228 | my $fpcount = scalar(@$arg_param); | |
229 | ||
230 | my $info = $class->map_method_by_name($name); | |
231 | ||
232 | my $schema = $info->{parameters}; | |
233 | my $prop = $schema->{properties}; | |
234 | ||
235 | my $print_parameter_completion = sub { | |
236 | my ($pname) = @_; | |
237 | my $d = $prop->{$pname}; | |
238 | if ($d->{completion}) { | |
239 | my $vt = ref($d->{completion}); | |
240 | if ($vt eq 'CODE') { | |
241 | my $res = $d->{completion}->($cmd, $pname, $cur); | |
242 | &$print_result(@$res); | |
243 | } | |
244 | } elsif ($d->{type} eq 'boolean') { | |
245 | &$print_result('0', '1'); | |
246 | } elsif ($d->{enum}) { | |
247 | &$print_result(@{$d->{enum}}); | |
248 | } | |
249 | }; | |
250 | ||
251 | # positional arguments | |
252 | $pos += 1 if $simple_cmd; | |
253 | if ($fpcount && $pos <= $fpcount) { | |
254 | my $pname = $arg_param->[$pos -1]; | |
255 | &$print_parameter_completion($pname); | |
256 | return; | |
257 | } | |
258 | ||
259 | my @option_list = (); | |
260 | foreach my $key (keys %$prop) { | |
261 | next if $skip_param->{$key}; | |
262 | push @option_list, "--$key"; | |
263 | } | |
264 | ||
265 | if ($cur =~ m/^-/) { | |
266 | &$print_result(@option_list); | |
267 | return; | |
268 | } | |
269 | ||
270 | if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) { | |
271 | my $pname = $1; | |
272 | &$print_parameter_completion($pname); | |
273 | return; | |
274 | } | |
275 | ||
276 | &$print_result(@option_list); | |
277 | }; | |
278 | ||
1f130ba6 DM |
279 | sub verify_api { |
280 | my ($class) = @_; | |
281 | ||
282 | # simply verify all registered methods | |
283 | PVE::RESTHandler::validate_method_schemas(); | |
284 | } | |
285 | ||
c45707a0 DM |
286 | sub generate_bash_completions { |
287 | my ($class) = @_; | |
288 | ||
289 | # generate bash completion config | |
290 | ||
291 | $exename = $class; | |
292 | $exename =~ s/^.*:://; | |
293 | ||
294 | print <<__EOD__; | |
295 | # $exename bash completion | |
296 | ||
297 | # see http://tiswww.case.edu/php/chet/bash/FAQ | |
298 | # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion | |
299 | # this modifies global var, but I found no better way | |
300 | COMP_WORDBREAKS=\${COMP_WORDBREAKS//:} | |
301 | ||
302 | complete -C '$exename bashcomplete' $exename | |
303 | __EOD__ | |
304 | } | |
305 | ||
7fe1f565 DM |
306 | sub find_cli_class_source { |
307 | my ($exename) = @_; | |
308 | ||
309 | my $filename; | |
310 | ||
311 | my $cpath = "PVE/CLI/${exename}.pm"; | |
312 | foreach my $p (@INC) { | |
313 | my $testfn = "$p/$cpath"; | |
314 | if (-f $testfn) { | |
315 | $filename = $testfn; | |
316 | last; | |
317 | } | |
318 | } | |
319 | ||
320 | return $filename; | |
321 | } | |
322 | ||
1f130ba6 DM |
323 | sub generate_pod_manpage { |
324 | my ($class, $podfn) = @_; | |
325 | ||
1f130ba6 DM |
326 | $exename = $class; |
327 | $exename =~ s/^.*:://; | |
328 | ||
7fe1f565 | 329 | $podfn = find_cli_class_source($exename) if !defined($podfn); |
1f130ba6 DM |
330 | |
331 | die "unable to find source for class '$class'" if !$podfn; | |
332 | ||
7fe1f565 DM |
333 | no strict 'refs'; |
334 | my $def = ${"${class}::cmddef"}; | |
335 | ||
336 | if (ref($def eq 'ARRAY')) { | |
337 | print_simple_pod_manpage($podfn, @$def); | |
338 | } else { | |
339 | $cmddef = $def; | |
340 | print_pod_manpage($podfn); | |
341 | } | |
1f130ba6 DM |
342 | } |
343 | ||
93ddd7bc | 344 | sub run { |
7fe1f565 | 345 | my ($class, $pwcallback, $podfn, $preparefunc) = @_; |
93ddd7bc DM |
346 | |
347 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
348 | ||
93ddd7bc DM |
349 | $exename = $class; |
350 | $exename =~ s/^.*:://; | |
351 | ||
352 | initlog($exename); | |
353 | ||
354 | die "please run as root\n" if $> != 0; | |
355 | ||
356 | PVE::INotify::inotify_init(); | |
357 | ||
358 | my $rpcenv = PVE::RPCEnvironment->init('cli'); | |
359 | $rpcenv->init_request(); | |
360 | $rpcenv->set_language($ENV{LANG}); | |
361 | $rpcenv->set_user('root@pam'); | |
362 | ||
7fe1f565 DM |
363 | no strict 'refs'; |
364 | my $def = ${"${class}::cmddef"}; | |
93ddd7bc | 365 | |
7fe1f565 DM |
366 | if (ref($def) eq 'ARRAY') { |
367 | handle_simple_cmd($def, \@ARGV, $pwcallback, $podfn, $preparefunc); | |
368 | } else { | |
369 | $cmddef = $def; | |
370 | my $cmd = shift @ARGV; | |
371 | handle_cmd($cmddef, $exename, $cmd, \@ARGV, $pwcallback, $podfn, $preparefunc); | |
372 | } | |
93ddd7bc DM |
373 | |
374 | exit 0; | |
375 | } | |
376 | ||
e143e9d8 | 377 | sub handle_cmd { |
8e3e9929 | 378 | my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_; |
e143e9d8 DM |
379 | |
380 | $cmddef = $def; | |
381 | $exename = $cmdname; | |
382 | ||
383 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
384 | ||
385 | if (!$cmd) { | |
386 | print_usage_short (\*STDERR, "no command specified"); | |
387 | exit (-1); | |
388 | } elsif ($cmd eq 'verifyapi') { | |
389 | PVE::RESTHandler::validate_method_schemas(); | |
390 | return; | |
391 | } elsif ($cmd eq 'printmanpod') { | |
392 | print_pod_manpage($podfn); | |
393 | return; | |
d8053c08 DM |
394 | } elsif ($cmd eq 'bashcomplete') { |
395 | &$print_bash_completion($cmddef, 0, @$args); | |
396 | return; | |
e143e9d8 DM |
397 | } |
398 | ||
8e3e9929 WB |
399 | &$preparefunc() if $preparefunc; |
400 | ||
e143e9d8 DM |
401 | $cmd = &$expand_command_name($cmddef, $cmd); |
402 | ||
403 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []}; | |
404 | ||
405 | if (!$class) { | |
406 | print_usage_short (\*STDERR, "unknown command '$cmd'"); | |
407 | exit (-1); | |
408 | } | |
409 | ||
410 | my $prefix = "$exename $cmd"; | |
411 | my $res = $class->cli_handler($prefix, $name, \@ARGV, $arg_param, $uri_param, $pwcallback); | |
2026f4b5 DM |
412 | |
413 | &$outsub($res) if $outsub; | |
414 | } | |
415 | ||
416 | sub handle_simple_cmd { | |
7fe1f565 | 417 | my ($def, $args, $pwcallback, $podfn, $preparefunc) = @_; |
2026f4b5 DM |
418 | |
419 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def}; | |
420 | die "no class specified" if !$class; | |
421 | ||
d8053c08 | 422 | if (scalar(@$args) >= 1) { |
2026f4b5 DM |
423 | if ($args->[0] eq 'help') { |
424 | my $str = "USAGE: $name help\n"; | |
425 | $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long'); | |
426 | print STDERR "$str\n\n"; | |
427 | return; | |
d8053c08 DM |
428 | } elsif ($args->[0] eq 'bashcomplete') { |
429 | shift @$args; | |
430 | &$print_bash_completion({ $name => $def }, $name, @$args); | |
431 | return; | |
2026f4b5 DM |
432 | } elsif ($args->[0] eq 'verifyapi') { |
433 | PVE::RESTHandler::validate_method_schemas(); | |
434 | return; | |
435 | } elsif ($args->[0] eq 'printmanpod') { | |
7fe1f565 DM |
436 | $podfn = find_cli_class_source($name) if !defined($podfn); |
437 | print_simple_pod_manpage($podfn, @$def); | |
2026f4b5 DM |
438 | return; |
439 | } | |
e143e9d8 | 440 | } |
2026f4b5 | 441 | |
7fe1f565 DM |
442 | &$preparefunc() if $preparefunc; |
443 | ||
2026f4b5 DM |
444 | my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $pwcallback); |
445 | ||
446 | &$outsub($res) if $outsub; | |
e143e9d8 DM |
447 | } |
448 | ||
449 | 1; |