using get_config() with default list of config files hs12
authorHeiko Schlittermann <hs@schlittermann.de>
Sun, 16 Jan 2011 17:29:01 +0100
branchhs12
changeset 85 c47953192c5c
parent 84 7c94598b7649
child 86 dc8741bbad33
using get_config() with default list of config files
Build.PL
bin/dnssec-keytool
bin/update-serial
bin/zone-ls
bin/zone-mk
bin/zone-rm
lib/DNStools/Config.pm
t/00-config.t
--- a/Build.PL	Sun Jan 16 17:28:48 2011 +0100
+++ b/Build.PL	Sun Jan 16 17:29:01 2011 +0100
@@ -6,11 +6,13 @@
 
 my $build = Module::Build->new(
     module_name    => "dnstools",
-    author         => "schlittermann.de",
+    dist_author    => "schlittermann.de",
     dist_version   => "0.1",
-    license        => "perl",
+    create_license => 1,
+    license        => "gpl",
     requires       => { perl => "5.10.0", },
     build_requires => { "Pod::Coverage" => 0, },
+    script_files   => [glob "bin/*"],              # avoid .swp files
 );
 
 $build->create_build_script;
--- a/bin/dnssec-keytool	Sun Jan 16 17:28:48 2011 +0100
+++ b/bin/dnssec-keytool	Sun Jan 16 17:29:01 2011 +0100
@@ -3,7 +3,6 @@
 use v5.10;
 use warnings;
 use strict;
-use FindBin;
 use File::Temp;
 use Getopt::Long;
 use Pod::Usage;
@@ -23,7 +22,7 @@
 
 MAIN: {
     ### reading config
-    my %conf = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
+    my %conf = get_config();
 
     my ($cmd, @zones) = read_argv($conf{master_dir});
 
--- a/bin/update-serial	Sun Jan 16 17:28:48 2011 +0100
+++ b/bin/update-serial	Sun Jan 16 17:29:01 2011 +0100
@@ -4,7 +4,6 @@
 use strict;
 use warnings;
 
-use FindBin;
 use File::Basename;
 use Pod::Usage;
 use Getopt::Long;
@@ -51,7 +50,8 @@
     ) or pod2usage;
 
     # merge the config and the defined options from commandline
-    %config = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf", \%opt);
+    %config = get_config("$ENV{DNSTOOLS_CONF}", "dnstools.conf",
+        "$ENV{HOME}/.dnstools.conf", "/etc/dnstools.conf", \%opt);
 
     our $bind_dir = $config{bind_dir};
     our $conf_dir = $config{zone_conf_dir};
--- a/bin/zone-ls	Sun Jan 16 17:28:48 2011 +0100
+++ b/bin/zone-ls	Sun Jan 16 17:29:01 2011 +0100
@@ -5,7 +5,6 @@
 use warnings;
 use Pod::Usage;
 use File::Basename;
-use FindBin;
 use Time::Local;
 use Getopt::Long;
 use if $ENV{DEBUG} => "Smart::Comments";
@@ -29,7 +28,7 @@
         },
     ) or pod2usage;
 
-    %config = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
+    %config = get_config();
     die "$config{master_dir}: $!\n" if not -d $config{master_dir};
 
     foreach my $dir (grep { -d } glob "$config{master_dir}/*") {
--- a/bin/zone-mk	Sun Jan 16 17:28:48 2011 +0100
+++ b/bin/zone-mk	Sun Jan 16 17:29:01 2011 +0100
@@ -13,7 +13,6 @@
 }
 
 # oeffnet Konfigurations- und Templatefiles - relativ oder absolut
-my @configs = ("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
 my @templc = (
     "$FindBin::Bin/templates/named.config",
     "/etc/dnstools/templates/named.config"
@@ -36,7 +35,7 @@
 }
 
 
-%config = get_config(@configs);
+%config = get_config();
 
 my $primary       = $config{primary};
 my $secondary     = $config{secondary};
--- a/bin/zone-rm	Sun Jan 16 17:28:48 2011 +0100
+++ b/bin/zone-rm	Sun Jan 16 17:29:01 2011 +0100
@@ -3,11 +3,10 @@
 use warnings;
 use strict;
 use File::Path;
-use FindBin;
 use DNStools::Config qw(get_config);
 
 # liest die Konfiguration ein
-my %config = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
+my %config = get_config();
 
 my $master_dir = $config{"master_dir"};
 my $conf_dir   = $config{"zone_conf_dir"};
--- a/lib/DNStools/Config.pm	Sun Jan 16 17:28:48 2011 +0100
+++ b/lib/DNStools/Config.pm	Sun Jan 16 17:29:01 2011 +0100
@@ -11,7 +11,13 @@
 sub get_config(@) {
 
     # read configuration
-    my @configs = @_;
+    my @configs =
+        @_
+      ? @_
+      : (
+        $ENV{DNSTOOLS_CONF},         "dnstools.conf",
+        "$ENV{HOME}/.dnstools.conf", "/etc/dnstools.conf"
+      );
     my %config;
 
     # the first config FILE
@@ -47,6 +53,7 @@
 =head1 SYNOPSIS
 
     use DNStools::Config qw(get_config);
+    %config = get_config();
     %config = get_config($file1, $file2, ...);
 
 =head1 DESCRIPTION
@@ -66,6 +73,10 @@
 Read the first file of the list (or dies if none of the files is found).
 Returns a hash with the config keys and values.
 
+If the list is empty, the configuration file is search in some default
+locations: C<$DNSTOOLS_CONF>, F<./dnstools.conf>,
+F<$HOME/.dnstools.conf>, F</etc/dnstools.conf>.
+
 =back
 
 =cut
--- a/t/00-config.t	Sun Jan 16 17:28:48 2011 +0100
+++ b/t/00-config.t	Sun Jan 16 17:29:01 2011 +0100
@@ -12,15 +12,13 @@
 
 can_ok("DNStools::Config" => "get_config");
 
-# should die if there is no config
-eval { get_config() };
-ok($@, "dies on missing file names");
-
 eval { get_config("xxx|xxx", "yyy|yyy") };
 ok($@, "dies on missing config");
 
+my ($tmp, %cf);
+
 # prepare some simple sample config
-my $tmp = File::Temp->new();
+$tmp = File::Temp->new();
 print {$tmp} <<__EOF;
 # comment
 abc = xyz
@@ -28,9 +26,18 @@
 __EOF
 close($tmp);
 
-my %cf = get_config($tmp->filename);
+# the files is specified, it should find the first 
+# existing
+%cf = get_config("xxx|xxx", $tmp->filename);
 ok(%cf, "got config");
+is($cf{abc} => "xyz", "simple value");
+is($cf{other} => "valuewithspace", "spaced value");
 
+# it should find the file specified in $ENV{DNSTOOLS_CONF}
+$ENV{DNSTOOLS_CONF} = $tmp->filename;
+%cf = ();
+%cf = get_config("xxx|xxx", $tmp->filename);
+ok(%cf, "got config from env");
 is($cf{abc} => "xyz", "simple value");
 is($cf{other} => "valuewithspace", "spaced value");