1 package PVE
::APIClient
::Helpers
;
8 use PVE
::APIClient
::Exception
qw(raise);
11 use HTTP
::Status
qw(:constants);
13 my $pve_api_definition;
14 my $pve_api_path_hash;
16 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
25 my $build_pve_api_path_hash;
26 $build_pve_api_path_hash = sub {
29 my $class = ref($tree);
30 return $tree if !$class;
32 if ($class eq 'ARRAY') {
33 foreach my $el (@$tree) {
34 $build_pve_api_path_hash->($el);
36 } elsif ($class eq 'HASH') {
37 if (defined($tree->{leaf
}) && defined(my $path = $tree->{path
})) {
38 $pve_api_path_hash->{$path} = $tree;
40 foreach my $k (keys %$tree) {
41 $build_pve_api_path_hash->($tree->{$k});
46 sub get_api_definition
{
48 if (!defined($pve_api_definition)) {
49 open(my $fh, '<', $pve_api_definition_fn) ||
50 die "unable to open '$pve_api_definition_fn' - $!\n";
51 $pve_api_definition = Storable
::fd_retrieve
($fh);
52 $build_pve_api_path_hash->($pve_api_definition);
55 return $pve_api_definition;
58 sub lookup_api_method
{
59 my ($path, $method, $noerr) = @_;
61 get_api_definition
(); # make sure API data is loaded
63 my $info = $pve_api_path_hash->{$path};
66 return undef if $noerr;
67 die "unable to find API info for path '$path'\n";
70 my $data = $info->{info
}->{$method};
73 return undef if $noerr;
74 die "unable to find API method '$method' for path '$path'\n";
80 sub complete_api_call_options
{
81 my ($cmd, $prop, $prev, $cur, $args) = @_;
83 my $print_result = sub {
85 print "$p\n" if $p =~ m/^$cur/;
89 my $print_parameter_completion = sub {
91 my $d = $prop->{$pname};
92 if ($d->{completion
}) {
93 my $vt = ref($d->{completion
});
95 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
96 &$print_result(@$res);
98 } elsif ($d->{type
} eq 'boolean') {
99 &$print_result('0', '1');
100 } elsif ($d->{enum
}) {
101 &$print_result(@{$d->{enum
}});
105 my @option_list = ();
106 foreach my $key (keys %$prop) {
107 push @option_list, "--$key";
111 &$print_result(@option_list);
115 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
117 &$print_parameter_completion($pname);
121 &$print_result(@option_list);
124 sub complete_api_path
{
127 get_api_definition
(); # make sure API data is loaded
131 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
134 if (!defined($dir)) {
136 $info = { children
=> $pve_api_definition };
138 $info = $pve_api_path_hash->{"/$dir"};
143 if (my $children = $info->{children
}) {
144 foreach my $c (@$children) {
145 if ($c->{path
} =~ m!\Q$dir/$rest!) {
146 push @$res, $c->{path
};
147 push @$res, "$c->{path}/" if $c->{children
};
155 # test for command lines with api calls (or similar bash completion calls):
156 # example1: pveclient api get remote1 /cluster
157 sub extract_path_info
{
161 my $test_path_properties = sub {
164 return if scalar(@$args) < 5;
165 return if $args->[1] ne 'api';
167 my $path = $args->[4];
168 if (my $method = $method_map->{$args->[2]}) {
169 $info = lookup_api_method
($path, $method, 1);
173 if (defined(my $cmd = $ARGV[0])) {
175 $test_path_properties->([$0, @ARGV]);
176 } elsif ($cmd eq 'bashcomplete') {
177 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
178 my $args = PVE
::Tools
::split_args
($cmdline);
179 $test_path_properties->($args);