[pve-devel] [PATCH client 2/3] config: use more xdg compliant search path

Wolfgang Bumiller w.bumiller at proxmox.com
Mon Jun 11 11:30:37 CEST 2018


Read configs from XDG_CONFIG_HOME/pveclient/config first.
Use a default of $HOME/.config for XDG_CONFIG_HOME if it is
not set, and only then try $HOME/.pveclient.

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 PVE/APIClient/Config.pm | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/PVE/APIClient/Config.pm b/PVE/APIClient/Config.pm
index 25ed56b..6497cc0 100644
--- a/PVE/APIClient/Config.pm
+++ b/PVE/APIClient/Config.pm
@@ -3,6 +3,8 @@ package PVE::APIClient::Config;
 use strict;
 use warnings;
 use JSON;
+use File::Basename qw(dirname);
+use File::Path qw(make_path);
 
 use PVE::JSONSchema qw(register_standard_option get_standard_option);
 use PVE::SectionConfig;
@@ -86,14 +88,26 @@ sub private {
     return $defaultData;
 }
 
+# Go through the usual XDG search path, with ~/.pveclient as fallback.
+# If no file is found and an XDG_CONFIG_HOME is set, prefer to return that one.
 sub config_filename {
     my ($class) = @_;
 
-    my $home = $ENV{HOME};
+    my $home = $ENV{HOME} // '';
+    my $xdg = $ENV{XDG_CONFIG_HOME} // '';
 
-    die "environment HOME not set\n" if !defined($home);
+    die "neither XDG_CONFIG_HOME nor HOME environment variable set\n"
+	if !length($xdg) && !length($home);
 
-    return "$home/.pveclient";
+    # If XDG_CONFIG_HOME is not set or empty, its default is $HOME/.config
+    my $base = $xdg || "$home/.config";
+
+    my $filename = "$base/pve/client";
+    return $filename if -e $filename;
+
+    return "$home/.pveclient" if -e "$home/.pveclient";
+
+    return $filename;
 }
 
 sub load {
@@ -119,6 +133,7 @@ sub save {
     my ($class, $cfg) = @_;
 
     my $filename = $class->config_filename();
+    make_path(dirname($filename));
     my $raw = $class->write_config($filename, $cfg);
 
     file_set_contents($filename, $raw, 0600);
-- 
2.11.0




More information about the pve-devel mailing list