Kaynağa Gözat

Dpkg::Conf: Add support for system and user config loading

Guillem Jover 11 yıl önce
ebeveyn
işleme
73c307b0d0
2 değiştirilmiş dosya ile 59 ekleme ve 0 silme
  1. 1 0
      debian/changelog
  2. 58 0
      scripts/Dpkg/Conf.pm

+ 1 - 0
debian/changelog

@@ -19,6 +19,7 @@ dpkg (1.18.8) UNRELEASED; urgency=medium
       important for dpkg.cfg files, and duplicate option names stopped being
       important for dpkg.cfg files, and duplicate option names stopped being
       supported. Add regression tests to avoid similar changes in the future.
       supported. Add regression tests to avoid similar changes in the future.
       Closes: #824938
       Closes: #824938
+    - Add support for system and user config loading in Dpkg::Conf.
   * Test suite:
   * Test suite:
     - Bump perlcritic ValuesAndExpressions::RequireNumberSeparators minimum
     - Bump perlcritic ValuesAndExpressions::RequireNumberSeparators minimum
       to 99999.
       to 99999.

+ 58 - 0
scripts/Dpkg/Conf.pm

@@ -100,6 +100,61 @@ sub set {
 
 
 Read options from a file. Return the number of options parsed.
 Read options from a file. Return the number of options parsed.
 
 
+=item $conf->load_system_config($file)
+
+Read options from a system configuration file.
+
+Return the number of options parsed.
+
+=cut
+
+sub load_system_config {
+    my ($self, $file) = @_;
+
+    return 0 unless -e "$Dpkg::CONFDIR/$file";
+    return $self->load("$Dpkg::CONFDIR/$file");
+}
+
+=item $conf->load_user_config($file)
+
+Read options from a user configuration file. It will try to use the XDG
+directory, either $XDG_CONFIG_HOME/dpkg/ or $HOME/.config/dpkg/.
+
+Return the number of options parsed.
+
+=cut
+
+sub load_user_config {
+    my ($self, $file) = @_;
+
+    my $confdir = $ENV{XDG_CONFIG_HOME};
+    $confdir ||= $ENV{HOME} . '/.config' if length $ENV{HOME};
+
+    return 0 unless length $confdir;
+    return 0 unless -e "$confdir/dpkg/$file";
+    return $self->load("$confdir/dpkg/$file") if length $confdir;
+    return 0;
+}
+
+=item $conf->load_config($file)
+
+Read options from system and user configuration files.
+
+Return the number of options parsed.
+
+=cut
+
+sub load_config {
+    my ($self, $file) = @_;
+
+    my $nopts = 0;
+
+    $nopts += $self->load_system_config($file);
+    $nopts += $self->load_user_config($file);
+
+    return $nopts;
+}
+
 =item $conf->parse($fh)
 =item $conf->parse($fh)
 
 
 Parse options from a file handle. Return the number of options parsed.
 Parse options from a file handle. Return the number of options parsed.
@@ -196,6 +251,9 @@ Obsolete option: 'format_argv' in $conf->filter().
 
 
 Obsolete methods: $conf->get(), $conf->set().
 Obsolete methods: $conf->get(), $conf->set().
 
 
+New methods: $conf->load_system_config(), $conf->load_system_user(),
+$conf->load_config().
+
 =head2 Version 1.02 (dpkg 1.18.5)
 =head2 Version 1.02 (dpkg 1.18.5)
 
 
 New option: Accept new option 'format_argv' in $conf->filter().
 New option: Accept new option 'format_argv' in $conf->filter().