]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
1 package PVE
::APIClient
::Config
;
7 use PVE
::APIClient
::JSONSchema
;
8 use PVE
::APIClient
::SectionConfig
;
9 use PVE
::APIClient
::Tools
qw(file_get_contents file_set_contents);
11 use base
qw(PVE::APIClient::SectionConfig);
13 my $remote_namne_regex = qw(\w+);
15 my $defaults_section = '!DEFAULTS';
17 my $complete_remote_name = sub {
19 my $config = PVE
::APIClient
::Config-
>load();
21 foreach my $name (keys %{$config->{ids
}}) {
22 push @$list, $name if $name ne $defaults_section;
27 PVE
::APIClient
::JSONSchema
::register_standard_option
('pveclient-output-format', {
29 description
=> 'Output format.',
30 enum
=> [ 'table', 'json' ],
35 PVE
::APIClient
::JSONSchema
::register_standard_option
('pveclient-remote-name', {
36 description
=> "The name of the remote.",
38 pattern
=> $remote_namne_regex,
39 completion
=> $complete_remote_name,
45 description
=> "Section type.",
58 my $home = $ENV{HOME
};
60 die "environment HOME not set\n" if !defined($home);
62 return "$home/.pveclient";
65 sub format_section_header
{
66 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
68 if ($type eq 'defaults') {
71 return "$type: $sectionId\n";
75 sub parse_section_header
{
76 my ($class, $line) = @_;
78 if ($line =~ m/^defaults:\s*$/) {
79 return ('defaults', $defaults_section, undef, {});
80 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
81 my ($type, $name) = (lc($1), $2);
83 die "invalid remote name '$name'\n"
84 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
86 return ($type, $name, $@, {});
94 my $filename = $class->config_filename();
99 my $filemode = (stat($filename))[2] & 07777;
100 if ($filemode != 0600) {
101 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
104 $raw = file_get_contents
($filename);
107 return $class->parse_config($filename, $raw);
111 my ($class, $cfg) = @_;
113 my $filename = $class->config_filename();
115 $cfg->{order
}->{$defaults_section} = -1; # write as first section
116 my $raw = $class->write_config($filename, $cfg);
118 file_set_contents
($filename, $raw, 0600);
122 my ($class, $cfg) = @_;
124 $cfg->{ids
}->{$defaults_section} //= {};
126 return $cfg->{ids
}->{$defaults_section};
130 my ($class, $cfg, $name, $noerr) = @_;
132 my $data = $cfg->{ids
}->{$name};
134 return $data if $noerr || defined($data);
136 die "unknown remote \"$name\"\n";
140 my ($class, $cfg, $remote) = @_;
142 my $section = $class->lookup_remote($cfg, $remote);
144 my $password = $section->{password};
145 if (!defined($password)) {
146 $password = PVE::PTY::read_password("Remote password
: ")
149 my $conn = PVE::APIClient::LWP->new(
150 username => $section->{username},
151 password => $password,
152 host => $section->{host},
153 port => $section->{port} // 8006,
154 cached_fingerprints => {
155 $section->{fingerprint} => 1,
164 package PVE::APIClient::RemoteConfig;
169 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
170 use PVE
::APIClient
::SectionConfig
;
172 use base
qw( PVE::APIClient::Config);
180 name
=> get_standard_option
('pveclient-remote-name'),
182 description
=> "The host.",
183 type
=> 'string', format
=> 'address',
187 description
=> "The username.",
192 description
=> "The users password.",
197 description
=> "The port.",
203 description
=> "Fingerprint.",
208 description
=> "Description.",
218 name
=> { optional
=> 0 },
219 host
=> { optional
=> 0 },
220 comment
=> { optional
=> 1 },
221 username
=> { optional
=> 0 },
222 password
=> { optional
=> 1 },
223 port
=> { optional
=> 1 },
224 fingerprint
=> { optional
=> 1 },
228 __PACKAGE__-
>register();
231 package PVE
::APIClient
::DefaultsConfig
;
236 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
238 use base
qw( PVE::APIClient::Config);
247 name
=> { optional
=> 1 },
248 username
=> { optional
=> 1 },
249 port
=> { optional
=> 1 },
253 __PACKAGE__-
>register();
256 PVE
::APIClient
::Config-
>init();