use Storable;
use JSON;
+use File::Path qw(make_path);
+
use PVE::APIClient::Exception qw(raise);
use Encode::Locale;
use Encode;
}
};
-my $default_output_format = 'table';
+my $default_output_format = 'text';
my $client_output_format = $default_output_format;
sub set_output_format {
return $client_output_format;
}
+sub print_result {
+ my ($data, $result_schema) = @_;
+
+ my $format = get_output_format();
+
+ return if $result_schema->{type} eq 'null';
+
+ # TODO: implement different output formats ($format)
+
+ if ($format eq 'json') {
+ print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
+ } elsif ($format eq 'text') {
+ my $type = $result_schema->{type};
+ if ($type eq 'object') {
+ die "implement me";
+ } elsif ($type eq 'array') {
+ my $item_type = $result_schema->{items}->{type};
+ if ($item_type eq 'object') {
+ die "implement me";
+ } elsif ($item_type eq 'array') {
+ die "implement me";
+ } else {
+ foreach my $el (@$data) {
+ print "$el\n"
+ }
+ }
+ } else {
+ print "$data\n";
+ }
+ } else {
+ die "internal error: unknown output format"; # should not happen
+ }
+}
+
sub get_api_definition {
if (!defined($pve_api_definition)) {
return $pve_api_definition;
}
-sub lookup_api_method {
- my ($path, $method, $noerr) = @_;
-
- get_api_definition(); # make sure API data is loaded
+my $map_path_to_info = sub {
+ my ($child_list, $stack, $uri_param) = @_;
- my $info = $pve_api_path_hash->{$path};
+ while (defined(my $comp = shift @$stack)) {
+ foreach my $child (@$child_list) {
+ my $text = $child->{text};
- if (!$info) {
- return undef if $noerr;
- die "unable to find API info for path '$path'\n";
+ if ($text eq $comp) {
+ # found
+ } elsif ($text =~ m/^\{(\S+)\}$/) {
+ # found
+ $uri_param->{$1} = $comp;
+ } else {
+ next; # text next child
+ }
+ if ($child->{leaf} || !scalar(@$stack)) {
+ return $child;
+ } else {
+ $child_list = $child->{children};
+ last; # test next path component
+ }
+ }
}
+ return undef;
+};
+
+sub find_method_info {
+ my ($path, $method, $uri_param, $noerr) = @_;
+
+ $uri_param //= {};
+
+ my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
- my $data = $info->{info}->{$method};
+ my $child = $map_path_to_info->(get_api_definition(), $stack, $uri_param);
- if (!$data) {
+ if (!($child && $child->{info} && $child->{info}->{$method})) {
return undef if $noerr;
die "unable to find API method '$method' for path '$path'\n";
}
- return $data;
+ return $child->{info}->{$method};
}
sub complete_api_call_options {
$dir = '';
$info = { children => $pve_api_definition };
} else {
- $info = $pve_api_path_hash->{"/$dir"};
+ my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
+ $info = $map_path_to_info->($pve_api_definition, $stack, {});
}
my $res = [];
if ($info) {
+ my $prefix = length($dir) ? "/$dir/" : '/';
if (my $children = $info->{children}) {
foreach my $c (@$children) {
- if ($c->{path} =~ m!\Q$dir/$rest!) {
- push @$res, $c->{path};
- push @$res, "$c->{path}/" if $c->{children};
+ my $ctext = $c->{text};
+ if ($ctext =~ m/^\{(\S+)\}$/) {
+ push @$res, "$prefix$ctext";
+ push @$res, "$prefix$ctext/";
+ if (length($rest)) {
+ push @$res, "$prefix$rest";
+ push @$res, "$prefix$rest/";
+ }
+ } elsif ($ctext =~ m/^\Q$rest/) {
+ push @$res, "$prefix$ctext";
+ push @$res, "$prefix$ctext/" if $c->{children};
}
}
}
}
+
return $res;
}
# test for command lines with api calls (or similar bash completion calls):
# example1: pveclient api get remote1 /cluster
sub extract_path_info {
+ my ($uri_param) = @_;
my $info;
my $path = $args->[4];
if (my $method = $method_map->{$args->[2]}) {
- $info = lookup_api_method($path, $method, 1);
+ $info = find_method_info($path, $method, $uri_param, 1);
}
};
return $task_status->{exitstatus};
}
+sub configuration_directory {
+
+ my $home = $ENV{HOME} // '';
+ my $xdg = $ENV{XDG_CONFIG_HOME} // '';
+
+ my $subdir = "pveclient";
+
+ return "$xdg/$subdir" if length($xdg);
+
+ return "$home/.config/$subdir" if length($home);
+
+ die "neither XDG_CONFIG_HOME nor HOME environment variable set\n";
+}
+
+my $ticket_cache_filename = "/.tickets";
+
+sub ticket_cache_lookup {
+ my ($remote) = @_;
+
+ my $dir = configuration_directory();
+ my $filename = "$dir/$ticket_cache_filename";
+
+ my $data = {};
+ eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
+ # ignore errors
+
+ my $ticket = $data->{$remote};
+ return undef if !defined($ticket);
+
+ my $min_age = - 60;
+ my $max_age = 3600*2 - 60;
+
+ if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
+ my $ttime = hex($1);
+ my $ctime = time();
+ my $age = $ctime - $ttime;
+
+ return $ticket if ($age > $min_age) && ($age < $max_age);
+ }
+
+ return undef;
+}
+
+sub ticket_cache_update {
+ my ($remote, $ticket) = @_;
+
+ my $dir = configuration_directory();
+ my $filename = "$dir/$ticket_cache_filename";
+
+ my $code = sub {
+ make_path($dir);
+ my $data = {};
+ if (-f $filename) {
+ my $raw = PVE::APIClient::Tools::file_get_contents($filename);
+ eval { $data = from_json($raw); };
+ # ignore errors
+ }
+ $data->{$remote} = $ticket;
+
+ PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
+ };
+
+ PVE::APIClient::Tools::lock_file($filename, undef, $code);
+ die $@ if $@;
+}
+
+
1;