]>
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
::PTY
;
10 use PVE
::APIClient
::Tools
qw(file_get_contents file_set_contents);
12 use base
qw(PVE::APIClient::SectionConfig);
14 my $remote_namne_regex = qw(\w+);
16 my $defaults_section = '!DEFAULTS';
18 my $complete_remote_name = sub {
20 my $config = PVE
::APIClient
::Config-
>load();
22 foreach my $name (keys %{$config->{ids
}}) {
23 push @$list, $name if $name ne $defaults_section;
28 PVE
::APIClient
::JSONSchema
::register_standard_option
('pveclient-output-format', {
30 description
=> 'Output format.',
31 enum
=> [ 'table', 'json' ],
36 PVE
::APIClient
::JSONSchema
::register_standard_option
('pveclient-remote-name', {
37 description
=> "The name of the remote.",
39 pattern
=> $remote_namne_regex,
40 completion
=> $complete_remote_name,
46 description
=> "Section type.",
59 my $home = $ENV{HOME
};
61 die "environment HOME not set\n" if !defined($home);
63 return "$home/.pveclient";
66 sub format_section_header
{
67 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
69 if ($type eq 'defaults') {
72 return "$type: $sectionId\n";
76 sub parse_section_header
{
77 my ($class, $line) = @_;
79 if ($line =~ m/^defaults:\s*$/) {
80 return ('defaults', $defaults_section, undef, {});
81 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
82 my ($type, $name) = (lc($1), $2);
84 die "invalid remote name '$name'\n"
85 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
87 return ($type, $name, $@, {});
95 my $filename = $class->config_filename();
100 my $filemode = (stat($filename))[2] & 07777;
101 if ($filemode != 0600) {
102 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
105 $raw = file_get_contents
($filename);
108 return $class->parse_config($filename, $raw);
112 my ($class, $cfg) = @_;
114 my $filename = $class->config_filename();
116 $cfg->{order
}->{$defaults_section} = -1; # write as first section
117 my $raw = $class->write_config($filename, $cfg);
119 file_set_contents
($filename, $raw, 0600);
123 my ($class, $cfg) = @_;
125 $cfg->{ids
}->{$defaults_section} //= {};
127 return $cfg->{ids
}->{$defaults_section};
131 my ($class, $cfg, $name, $noerr) = @_;
133 my $data = $cfg->{ids
}->{$name};
135 return $data if $noerr || defined($data);
137 die "unknown remote \"$name\"\n";
141 my ($class, $cfg, $remote) = @_;
143 my $section = $class->lookup_remote($cfg, $remote);
145 my $password = $section->{password};
146 if (!defined($password)) {
147 $password = PVE::APIClient::PTY::read_password("Remote password
: ")
150 my $conn = PVE::APIClient::LWP->new(
151 username => $section->{username},
152 password => $password,
153 host => $section->{host},
154 port => $section->{port} // 8006,
155 cached_fingerprints => {
156 $section->{fingerprint} => 1,
165 package PVE::APIClient::RemoteConfig;
170 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
171 use PVE
::APIClient
::SectionConfig
;
173 use base
qw( PVE::APIClient::Config);
181 name
=> get_standard_option
('pveclient-remote-name'),
183 description
=> "The host.",
184 type
=> 'string', format
=> 'address',
188 description
=> "The username.",
193 description
=> "The users password.",
198 description
=> "The port.",
204 description
=> "Fingerprint.",
209 description
=> "Description.",
219 name
=> { optional
=> 0 },
220 host
=> { optional
=> 0 },
221 comment
=> { optional
=> 1 },
222 username
=> { optional
=> 0 },
223 password
=> { optional
=> 1 },
224 port
=> { optional
=> 1 },
225 fingerprint
=> { optional
=> 1 },
229 __PACKAGE__-
>register();
232 package PVE
::APIClient
::DefaultsConfig
;
237 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
239 use base
qw( PVE::APIClient::Config);
248 name
=> { optional
=> 1 },
249 username
=> { optional
=> 1 },
250 port
=> { optional
=> 1 },
254 __PACKAGE__-
>register();
257 PVE
::APIClient
::Config-
>init();