]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Helpers.pm
add more helper for bash completion
[pve-client.git] / PVE / APIClient / Helpers.pm
1 package PVE::APIClient::Helpers;
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7 use JSON;
8 use PVE::APIClient::Exception qw(raise);
9 use Encode::Locale;
10 use Encode;
11 use HTTP::Status qw(:constants);
12
13 my $pve_api_definition;
14 my $pve_api_path_hash;
15
16 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.js";
17
18 my $build_pve_api_path_hash;
19 $build_pve_api_path_hash = sub {
20 my ($tree) = @_;
21
22 my $class = ref($tree);
23 return $tree if !$class;
24
25 if ($class eq 'ARRAY') {
26 foreach my $el (@$tree) {
27 $build_pve_api_path_hash->($el);
28 }
29 } elsif ($class eq 'HASH') {
30 if (defined($tree->{leaf}) && defined(my $path = $tree->{path})) {
31 $pve_api_path_hash->{$path} = $tree;
32 }
33 foreach my $k (keys %$tree) {
34 $build_pve_api_path_hash->($tree->{$k});
35 }
36 }
37 };
38
39 sub get_api_definition {
40
41 if (!defined($pve_api_definition)) {
42 local $/;
43 open(my $fh, '<', $pve_api_definition_fn) ||
44 die "unable to open '$pve_api_definition_fn' - $!\n";
45 my $json_text = <$fh>;
46 $pve_api_definition = decode_json($json_text);
47
48 $build_pve_api_path_hash->($pve_api_definition);
49 }
50
51
52 return $pve_api_definition;
53 }
54
55 sub lookup_api_method {
56 my ($path, $method, $noerr) = @_;
57
58 get_api_definition(); # make sure API data is loaded
59
60 my $info = $pve_api_path_hash->{$path};
61
62 if (!$info) {
63 return undef if $noerr;
64 die "unable to find API info for path '$path'\n";
65 }
66
67 my $data = $info->{info}->{$method};
68
69 if (!$data) {
70 return undef if $noerr;
71 die "unable to find API method '$method' for path '$path'\n";
72 }
73
74 return $data;
75 }
76
77 sub complete_api_call_options {
78 my ($cmd, $prop, $prev, $cur, $args) = @_;
79
80 my $print_result = sub {
81 foreach my $p (@_) {
82 print "$p\n" if $p =~ m/^$cur/;
83 }
84 };
85
86 my $print_parameter_completion = sub {
87 my ($pname) = @_;
88 my $d = $prop->{$pname};
89 if ($d->{completion}) {
90 my $vt = ref($d->{completion});
91 if ($vt eq 'CODE') {
92 my $res = $d->{completion}->($cmd, $pname, $cur, $args);
93 &$print_result(@$res);
94 }
95 } elsif ($d->{type} eq 'boolean') {
96 &$print_result('0', '1');
97 } elsif ($d->{enum}) {
98 &$print_result(@{$d->{enum}});
99 }
100 };
101
102 my @option_list = ();
103 foreach my $key (keys %$prop) {
104 push @option_list, "--$key";
105 }
106
107 if ($cur =~ m/^-/) {
108 &$print_result(@option_list);
109 return;
110 }
111
112 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
113 my $pname = $1;
114 &$print_parameter_completion($pname);
115 return;
116 }
117
118 &$print_result(@option_list);
119 }
120
121 sub complete_api_path {
122 my ($text) = @_;
123
124 get_api_definition(); # make sure API data is loaded
125
126 $text =~ s!^/!!;
127
128 my ($dir, $rest) = $text =~ m|^(?:(.*)/)?(?:([^/]*))?$|;
129
130 my $info;
131 if (!defined($dir)) {
132 $dir = '';
133 $info = { children => $pve_api_definition };
134 } else {
135 $info = $pve_api_path_hash->{"/$dir"};
136 }
137
138 if ($info) {
139 if (my $children = $info->{children}) {
140 foreach my $c (@$children) {
141 if ($c->{path} =~ m!\Q$dir/$rest!) {
142 print "$c->{path}\n";
143 print "$c->{path}/\n"if $c->{children};
144 }
145 }
146 }
147 }
148 }
149
150 1;