From: Dietmar Maurer Date: Tue, 29 May 2018 07:20:25 +0000 (+0200) Subject: use new PVE::APIClient::Config class, register_standard_option 'pveclient-remote... X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=commitdiff_plain;h=3454a319f2bbd606609971ff438cd4fe05c815d0 use new PVE::APIClient::Config class, register_standard_option 'pveclient-remote-name' and implement command line completion for 'remote' names. --- diff --git a/PVE/APIClient/Commands/lxc.pm b/PVE/APIClient/Commands/lxc.pm index 67af931..41ff4db 100644 --- a/PVE/APIClient/Commands/lxc.pm +++ b/PVE/APIClient/Commands/lxc.pm @@ -2,31 +2,18 @@ package PVE::APIClient::Commands::lxc; use strict; use warnings; -use JSON; -use File::HomeDir; use PVE::Tools; +use PVE::JSONSchema qw(get_standard_option); use PVE::CLIHandler; use base qw(PVE::CLIHandler); - -my $load_config = sub { - - my $filename = home() . '/.pveclient'; - my $conf_str = PVE::Tools::file_get_contents($filename); - - my $filemode = (stat($filename))[2] & 07777; - if ($filemode != 0600) { - die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode; - } - - return decode_json($conf_str); -}; +use PVE::APIClient::Config; my $load_remote_config = sub { my ($remote) = @_; - my $conf = $load_config->(); + my $conf = PVE::APIClient::Config::load_config(); my $remote_conf = $conf->{"remote_$remote"} || die "no such remote '$remote'\n"; @@ -52,7 +39,6 @@ my $get_remote_connection = sub { }); }; - __PACKAGE__->register_method ({ name => 'enter', path => 'enter', @@ -61,10 +47,7 @@ __PACKAGE__->register_method ({ parameters => { additionalProperties => 0, properties => { - remote => { - description => "The name of the remote.", - type => 'string', - }, + remote => get_standard_option('pveclient-remote-name'), vmid => { description => "The container ID", type => 'string', @@ -95,10 +78,7 @@ __PACKAGE__->register_method ({ parameters => { additionalProperties => 0, properties => { - remote => { - description => "The remote name.", - type => 'string', - }, + remote => get_standard_option('pveclient-remote-name'), }, }, returns => { type => 'null'}, diff --git a/PVE/APIClient/Commands/remote.pm b/PVE/APIClient/Commands/remote.pm index 7b40739..a6eb512 100644 --- a/PVE/APIClient/Commands/remote.pm +++ b/PVE/APIClient/Commands/remote.pm @@ -3,10 +3,37 @@ package PVE::APIClient::Commands::remote; use strict; use warnings; +use PVE::JSONSchema qw(register_standard_option get_standard_option); +use PVE::APIClient::Config; + use PVE::CLIHandler; use base qw(PVE::CLIHandler); +my $remote_name_regex = qr(\w+); + +my $complete_remote_name = sub { + + my $conf = PVE::APIClient::Config::load_config(); + + my $res = []; + + foreach my $k (keys %$conf) { + if ($k =~ m/^remote_($remote_name_regex)$/) { + push @$res, $1; + } + } + + return $res; +}; + +register_standard_option('pveclient-remote-name', { + description => "The name of the remote.", + type => 'string', + pattern => $remote_name_regex, + completion => $complete_remote_name, +}); + __PACKAGE__->register_method ({ name => 'add', path => 'add', @@ -15,16 +42,13 @@ __PACKAGE__->register_method ({ parameters => { additionalProperties => 0, properties => { - name => { - description => "The name of the remote.", - type => 'string', - }, + name => get_standard_option('pveclient-remote-name', { completion => sub {} }), host => { description => "The host, either host, host:port or https://host:port", type => 'string', }, username => { - description => "The username.", + description => "The username.", type => 'string', optional => 1, }, @@ -46,10 +70,7 @@ __PACKAGE__->register_method ({ parameters => { additionalProperties => 0, properties => { - name => { - description => "The name of the remote.", - type => 'string', - }, + name => get_standard_option('pveclient-remote-name'), }, }, returns => { type => 'null'},