]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Config.pm
rename defaut output format from 'table' to 'text'
[pve-client.git] / PVE / APIClient / Config.pm
index 40caed86f92f3869a79be0ebfc78b14df48c6ff0..249ef83ceaebb801700232c6a925a76440c4c270 100644 (file)
@@ -4,140 +4,154 @@ use strict;
 use warnings;
 use JSON;
 
-use File::HomeDir ();
-use PVE::JSONSchema qw(register_standard_option get_standard_option);
-use PVE::Tools qw(file_get_contents file_set_contents);
+use PVE::APIClient::JSONSchema;
+use PVE::APIClient::SectionConfig;
+use PVE::APIClient::PTY;
+use PVE::APIClient::Tools qw(file_get_contents file_set_contents);
+
+use base qw(PVE::APIClient::SectionConfig);
+
+my $remote_namne_regex = qw(\w+);
+
+my $defaults_section = '!DEFAULTS';
 
 my $complete_remote_name = sub {
 
-    my $config = PVE::APIClient::Config->new();
-    return $config->remote_names;
+    my $config = PVE::APIClient::Config->load();
+    my $list = [];
+    foreach my $name (keys %{$config->{ids}}) {
+       push @$list, $name if $name ne $defaults_section;
+    }
+    return $list;
 };
 
-register_standard_option('pveclient-remote-name', {
+PVE::APIClient::JSONSchema::register_standard_option('pveclient-output-format', {
+    type => 'string',
+    description => 'Output format.',
+    enum => [ 'text', 'json' ],
+    optional => 1,
+    default => 'text',
+});
+
+PVE::APIClient::JSONSchema::register_standard_option('pveclient-remote-name', {
     description => "The name of the remote.",
     type => 'string',
-    pattern => qr(\w+),
+    pattern => $remote_namne_regex,
     completion => $complete_remote_name,
 });
 
-sub new {
+my $defaultData = {
+    propertyList => {
+       type => {
+           description => "Section type.",
+           optional => 1,
+       },
+    },
+};
+
+sub private {
+    return $defaultData;
+}
+
+sub config_filename {
     my ($class) = @_;
 
-    my $self = {
-       file         => File::HomeDir::home() . '/.pveclient',
-    };
-    bless $self => $class;
+    my $home = $ENV{HOME};
 
-    $self->load();
+    die "environment HOME not set\n" if !defined($home);
 
-    return $self;
+    return "$home/.pveclient";
 }
 
-sub load {
-    my ($self) = @_;
+sub format_section_header {
+    my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
 
-    if (-e $self->{file}) {
-       my $filemode = (stat($self->{file}))[2] & 07777;
-       if ($filemode != 0600) {
-           die sprintf "wrong permissions on '$self->{file}' %04o (expected 0600)\n", $filemode;
-       }
-
-       my $contents = file_get_contents($self->{file});
-       $self->{data} = from_json($contents);
+    if ($type eq 'defaults') {
+       return "defaults:\n";
     } else {
-       $self->{data} = {};
-    }
-
-    if (!exists($self->{data}->{remotes})) {
-       $self->{data}->{remotes} = {};
+       return "$type: $sectionId\n";
     }
+}
 
-    # Verify config
-    for my $name (@{$self->remote_names}) {
-       my $cfg = $self->{data}->{remotes}->{$name};
+sub parse_section_header {
+    my ($class, $line) = @_;
 
-       foreach my $opt (qw(host port username fingerprint)) {
-         die "missing option '$opt' (remote '$name')" if !defined($cfg->{$opt});
-       }
+    if ($line =~ m/^defaults:\s*$/) {
+       return ('defaults', $defaults_section, undef, {});
+    } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
+       my ($type, $name) = (lc($1), $2);
+       eval {
+           die "invalid remote name '$name'\n"
+               if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
+       };
+       return ($type, $name, $@, {});
     }
+    return undef;
 }
 
-sub save {
-    my ($self) = @_;
+sub load {
+    my ($class) = @_;
 
-    my $contents = to_json($self->{data}, {pretty => 1, canonical => 1});
-    file_set_contents($self->{file}, $contents, 0600);
-}
+    my $filename = $class->config_filename();
 
-sub add_remote {
-    my ($self, $name, $host, $port, $fingerprint, $username, $password) = @_;
+    my $raw = '';
 
-    $self->{data}->{remotes}->{$name} = {
-       host => $host,
-       port => $port,
-       fingerprint => $fingerprint,
-       username => $username,
-    };
+    if (-e $filename) {
+       my $filemode = (stat($filename))[2] & 07777;
+       if ($filemode != 0600) {
+           die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
+       }
 
-    if (defined($password)) {
-       $self->{data}->{remotes}->{$name}->{password} = $password;
+       $raw = file_get_contents($filename);
     }
-}
 
-sub remote_names {
-    my ($self) = @_;
-
-    return [keys %{$self->{data}->{remotes}}];
+    return $class->parse_config($filename, $raw);
 }
 
-sub lookup_remote {
-    my ($self, $name) = @_;
+sub save {
+    my ($class, $cfg) = @_;
+
+    my $filename = $class->config_filename();
 
-    die "Unknown remote \"$name\" given"
-      if (!exists($self->{data}->{remotes}->{$name}));
+    $cfg->{order}->{$defaults_section} = -1; # write as first section
+    my $raw = $class->write_config($filename, $cfg);
 
-    return $self->{data}->{remotes}->{$name};
+    file_set_contents($filename, $raw, 0600);
 }
 
-sub remotes {
-    my ($self) = @_;
+sub get_defaults {
+    my ($class, $cfg) = @_;
 
-    my $res = {};
+    $cfg->{ids}->{$defaults_section} //= {};
 
-    # Remove the password from each remote.
-    for my $name ($self->remote_names) {
-       my $cfg = $self->{data}->{remotes}->{$name};
-       $res->{$name} = {
-           host        => $cfg->{host},
-           port        => $cfg->{port},
-           username    => $cfg->{username},
-           fingerprint => $cfg->{fingerprint},
-       };
-    }
-
-    return $res;
+    return $cfg->{ids}->{$defaults_section};
 }
 
-sub remove_remote {
-    my ($self, $remote) = @_;
+sub lookup_remote {
+    my ($class, $cfg, $name, $noerr) = @_;
 
-    $self->lookup_remote($remote);
+    my $data = $cfg->{ids}->{$name};
 
-    delete($self->{data}->{remotes}->{$remote});
+    return $data if $noerr || defined($data);
 
-    $self->save();
+    die "unknown remote \"$name\"\n";
 }
 
 sub remote_conn {
-    my ($self, $remote) = @_;
+    my ($class, $cfg, $remote) = @_;
+
+    my $section = $class->lookup_remote($cfg, $remote);
+
+    my $password = $section->{password};
+    if (!defined($password)) {
+       $password = PVE::APIClient::PTY::read_password("Remote password: ")
+    }
 
-    my $section = $self->lookup_remote($remote);
     my $conn = PVE::APIClient::LWP->new(
        username                => $section->{username},
-       password                => $section->{password},
+       password                => $password,
        host                    => $section->{host},
-       port                    => $section->{port},
+       port                    => $section->{port} // 8006,
        cached_fingerprints     => {
            $section->{fingerprint} => 1,
        }
@@ -148,4 +162,98 @@ sub remote_conn {
     return $conn;
 }
 
+package PVE::APIClient::RemoteConfig;
+
+use strict;
+use warnings;
+
+use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
+use PVE::APIClient::SectionConfig;
+
+use base qw( PVE::APIClient::Config);
+
+sub type {
+    return 'remote';
+}
+
+sub properties {
+    return {
+       name => get_standard_option('pveclient-remote-name'),
+       host => {
+           description => "The host.",
+           type => 'string', format => 'address',
+           optional => 1,
+       },
+       username => {
+           description => "The username.",
+           type => 'string',
+           optional => 1,
+       },
+       password => {
+           description => "The users password.",
+           type => 'string',
+           optional => 1,
+       },
+       port => {
+           description => "The port.",
+           type => 'integer',
+           optional => 1,
+           default => 8006,
+       },
+       fingerprint => {
+           description => "Fingerprint.",
+           type => 'string',
+           optional => 1,
+       },
+       comment => {
+           description => "Description.",
+           type => 'string',
+           optional => 1,
+           maxLength => 4096,
+       },
+    };
+}
+
+sub options {
+    return {
+       name => { optional => 0 },
+       host => { optional => 0 },
+       comment => { optional => 1 },
+       username => { optional => 0 },
+       password => { optional => 1 },
+       port => { optional => 1 },
+       fingerprint => { optional => 1 },
+   };
+}
+
+__PACKAGE__->register();
+
+
+package PVE::APIClient::DefaultsConfig;
+
+use strict;
+use warnings;
+
+use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
+
+use base qw( PVE::APIClient::Config);
+
+
+sub type {
+    return 'defaults';
+}
+
+sub options {
+    return {
+       name => { optional => 1 },
+       username => { optional => 1 },
+       port => { optional => 1 },
+   };
+}
+
+__PACKAGE__->register();
+
+
+PVE::APIClient::Config->init();
+
 1;