]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Commands/config.pm
use new features from pve-common
[pve-client.git] / PVE / APIClient / Commands / config.pm
index d467b4c7102f6d8eee44cc09ba6daee5a54e02d9..b4316d80550ecce889504b0b2ee5bf2a26f11c38 100644 (file)
@@ -4,14 +4,19 @@ use strict;
 use warnings;
 use Data::Dumper;
 
+use PVE::APIClient::Helpers;
 use PVE::APIClient::JSONSchema qw(get_standard_option);
 use PVE::APIClient::Tools qw(extract_param);
 use PVE::APIClient::Config;
 
+use PVE::APIClient::CLIFormatter;
 use PVE::APIClient::CLIHandler;
 
 use base qw(PVE::APIClient::CLIHandler);
 
+my $list_return_props = { %{PVE::APIClient::DefaultsConfig->updateSchema(1)->{properties}} };
+delete $list_return_props->{delete};
+
 __PACKAGE__->register_method ({
     name => 'list',
     path => 'list',
@@ -19,18 +24,21 @@ __PACKAGE__->register_method ({
     description => "Dump default configuration.",
     parameters => {
        additionalProperties => 0,
+       properties => {},
+    },
+    returns => {
+       type => 'object',
+       properties => $list_return_props,
     },
-    returns => { type => 'null' },
     code => sub {
 
        my $config = PVE::APIClient::Config->load();
-       
+
        my $defaults = PVE::APIClient::Config->get_defaults($config);
 
-       
-       print Dumper($config);
-       
-       return undef;
+       $defaults->{digest} = $config->{digest};
+
+       return $defaults;
     }});
 
 __PACKAGE__->register_method ({
@@ -43,35 +51,40 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       # fixme: lock config file
-
        my $digest = extract_param($param, 'digest');
        my $delete = extract_param($param, 'delete');
 
-       my $config = PVE::APIClient::Config->load();
-       my $defaults = PVE::APIClient::Config->get_defaults($config);
-       
-       my $plugin = PVE::APIClient::Config->lookup('defaults');
-       my $opts = $plugin->check_config('defaults', $param, 0, 1);
-
-       foreach my $k (%$opts) {
-           $defaults->{$k} = $opts->{$k};
-       }
-
-       if ($delete) {
-           my $options = $plugin->private()->{options}->{'defaults'};
-           foreach my $k (PVE::APIClient::Tools::split_list($delete)) {
-               my $d = $options->{$k} ||
-                   die "no such option '$k'\n";
-               die "unable to delete required option '$k'\n"
-                   if !$d->{optional};
-               die "unable to delete fixed option '$k'\n"
-                   if $d->{fixed};
-               delete $defaults->{$k};
+       my $code = sub {
+           my $config = PVE::APIClient::Config->load();
+
+           PVE::APIClient::Tools::assert_if_modified($config->{digest}, $digest);
+
+           my $defaults = PVE::APIClient::Config->get_defaults($config);
+
+           my $plugin = PVE::APIClient::Config->lookup('defaults');
+           my $opts = $plugin->check_config('defaults', $param, 0, 1);
+
+           foreach my $k (%$opts) {
+               $defaults->{$k} = $opts->{$k};
            }
-       }
 
-       PVE::APIClient::Config->save($config);
+           if ($delete) {
+               my $options = $plugin->private()->{options}->{'defaults'};
+               foreach my $k (PVE::APIClient::Tools::split_list($delete)) {
+                   my $d = $options->{$k} ||
+                       die "no such option '$k'\n";
+                   die "unable to delete required option '$k'\n"
+                       if !$d->{optional};
+                   die "unable to delete fixed option '$k'\n"
+                       if $d->{fixed};
+                   delete $defaults->{$k};
+               }
+           }
+
+           PVE::APIClient::Config->save($config);
+       };
+
+       PVE::APIClient::Config->lock_config(undef, $code);
 
        return undef;
     }});
@@ -79,7 +92,13 @@ __PACKAGE__->register_method ({
 
 our $cmddef = {
     set => [ __PACKAGE__, 'set',],
-    list => [__PACKAGE__, 'list'],
+    list => [__PACKAGE__, 'list', undef, undef,
+            sub {
+                my ($data, $schema, $options) = @_;
+
+                PVE::APIClient::CLIFormatter::print_api_result($data, $schema, undef, $options);
+            }
+       ],
 };
 
 1;