now using my::Config qw(get_config) hs12
authorHeiko Schlittermann <hs@schlittermann.de>
Mon, 03 Jan 2011 16:49:56 +0100
branchhs12
changeset 71 e25fc893e203
parent 70 bffb3f2cca90
child 72 d3d2108f07da
now using my::Config qw(get_config)
dnssec-keytool.pl
my/Config.pm
update-serial.pl
zone-ls.pl
zone-mk.pl
zone-rm.pl
--- a/dnssec-keytool.pl	Mon Jan 03 16:13:54 2011 +0100
+++ b/dnssec-keytool.pl	Mon Jan 03 16:49:56 2011 +0100
@@ -9,6 +9,7 @@
 use Pod::Usage;
 use File::Basename;
 use if $ENV{DEBUG} => "Smart::Comments";
+use my::Config qw(get_config);
 
 my $ME = basename $0;
 
@@ -22,7 +23,7 @@
 
 MAIN: {
     ### reading config
-    my %conf = read_conf("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
+    my %conf = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
 
     my ($cmd, @zones) = read_argv($conf{master_dir});
 
@@ -69,24 +70,6 @@
     return ($cmd, @zones);
 }
 
-sub read_conf(@) {
-
-    # read configuration
-    my @conffiles = @_;
-    my %return;
-
-    my ($_) = grep { -f } @conffiles;
-    open(my $cf, $_) or die "Can't open $_: $!\n";
-
-    while (<$cf>) {
-        s/#.*//;
-        s/\s//g;
-        next unless length;
-        my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
-        $return{$cname} = $ccont;
-    }
-    return %return;
-}
 
 sub rm_keys ($@) {
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/my/Config.pm	Mon Jan 03 16:49:56 2011 +0100
@@ -0,0 +1,36 @@
+package my::Config;
+use strict;
+use warnings;
+
+use base "Exporter";
+
+our $VERSION   = 0.0;
+our @EXPORT_OK = qw(get_config);
+
+sub get_config(@) {
+
+    # read configuration
+    my @configs = @_;
+    my %config;
+
+    # the first config FILE
+    my ($_) = grep { -f } @configs;
+    open(my $cf, $_) or die "Can't open $_: $!\n";
+
+    while (<$cf>) {
+        s/#.*//;
+        s/\s//g;
+        next unless length;
+        my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
+        $config{$cname} = $ccont;
+    }
+
+    # now merge the config hashes
+    foreach my $o (grep { ref eq "HASH" } @configs) {
+        %config =
+          (%config, map { $_ => $o->{$_} } grep { defined $o->{$_} } keys %$o);
+    }
+    return %config;
+}
+
+1;
--- a/update-serial.pl	Mon Jan 03 16:13:54 2011 +0100
+++ b/update-serial.pl	Mon Jan 03 16:49:56 2011 +0100
@@ -12,9 +12,9 @@
 use IO::File;
 use POSIX qw(strftime);
 use if $ENV{DEBUG} => "Smart::Comments";
+use my::Config qw(get_config);
 
 sub uniq(@);
-sub read_conf(@);
 sub zones(@);
 sub changed_zones();
 sub update_index($);
@@ -51,10 +51,10 @@
     ) or pod2usage;
 
     # merge the config and the defined options from commandline
-    %config = (
-        read_conf("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf"),
-        map { $_ => $opt{$_} } grep { defined $opt{$_} } keys %opt
-    );
+    %config = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf", \%opt);
+
+    ### %config
+    exit;
 
     our $bind_dir = $config{bind_dir};
     our $conf_dir = $config{zone_conf_dir};
@@ -90,26 +90,6 @@
     keys %all;
 }
 
-sub read_conf(@) {
-    my @configs = @_;
-    my %config;
-
-    my ($config) = grep { -f } @configs
-      or die "No config found (looked at @configs)\n";
-
-    open(my $fh => $config) or die "$config: $!\n";
-
-    while (<$fh>) {
-        chomp;
-        s/#.*//;
-        s/\s//g;
-        my ($k, $v) = split(/\s*=\s*/, $_, 2) or next;
-        $config{$k} = $v;
-    }
-
-    return %config;
-}
-
 sub zones(@) {
 
     # check whether the zones in argv are managed zones and
--- a/zone-ls.pl	Mon Jan 03 16:13:54 2011 +0100
+++ b/zone-ls.pl	Mon Jan 03 16:49:56 2011 +0100
@@ -9,6 +9,7 @@
 use Time::Local;
 use Getopt::Long;
 use if $ENV{DEBUG} => "Smart::Comments";
+use my::Config qw(get_config);
 
 my %config;
 my $opt_expiry = undef;
@@ -28,20 +29,7 @@
         },
     ) or pod2usage;
 
-    {    # find and read/parse the config (could use some common config parser)
-        my @configs = ("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
-        ($_) = grep { -f } @configs;
-        open(my $config, $_) or die "Can't open $_: $!\n";
-
-        while (<$config>) {
-            chomp;
-            s/#.*//;
-            s/\s//g;
-            my ($k, $v) = split(/\s*=\s*/, $_, 2) or next;
-            $config{$k} = $v;
-        }
-    }
-
+    %config = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
     die "$config{master_dir}: $!\n" if not -d $config{master_dir};
 
     foreach my $dir (grep { -d } glob "$config{master_dir}/*") {
@@ -101,14 +89,13 @@
         }
     }
 
-
     {    # output
 
         my $sort_by =
           $opt_expiry
           ? sub { ($info{$a}{expiry} // 2**64) <=> ($info{$b}{expiry} // 2**64) }
           : sub { $a cmp $b };
-		 
+
         my $format_h = "%-35s %-8s %1s/%1s %3s %7s\n";
         my $format_l = "%-35s %-8s %1d/%1d %5d %19s\n";
 
@@ -121,7 +108,6 @@
     }
 }
 
-
 __END__
 
 =head1 NAME
--- a/zone-mk.pl	Mon Jan 03 16:13:54 2011 +0100
+++ b/zone-mk.pl	Mon Jan 03 16:49:56 2011 +0100
@@ -3,6 +3,9 @@
 use warnings;
 use strict;
 use FindBin;
+use my::Config qw(get_config);
+
+my %config;
 
 if (@ARGV < 2) {
     print "usage: zone-mk kundennummer domain ... \n";
@@ -17,14 +20,6 @@
 );
 my @templz =
   ("$FindBin::Bin/templates/named.zone", "/etc/dnstools/templates/named.zone");
-my %config;
-
-for (grep { -f } @configs) {
-    open(CONFIG, $_) or die "Can't open $_: $!\n";
-}
-unless (seek(CONFIG, 0, 0)) {
-    die "Can't open config (searched: @configs)\n";
-}
 
 for (grep { -f } @templc) {
     open(TEMPCONF, $_) or die "Can't open $_: $!\n";
@@ -40,16 +35,8 @@
     die "Can't open template (searched: @templz)\n";
 }
 
-while (<CONFIG>) {
-    chomp;
-    s/#.*//;
-    s/\t//g;
-    s/\s//g;
-    next unless length;
-    my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
-    $config{$cname} = $ccont;
-}
-close(CONFIG);
+
+%config = get_config(@configs);
 
 my $primary       = $config{primary};
 my $secondary     = $config{secondary};
--- a/zone-rm.pl	Mon Jan 03 16:13:54 2011 +0100
+++ b/zone-rm.pl	Mon Jan 03 16:49:56 2011 +0100
@@ -4,29 +4,10 @@
 use strict;
 use File::Path;
 use FindBin;
+use my::Config qw(get_config);
 
 # liest die Konfiguration ein
-my @configs = ("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
-my %config;
-
-foreach (grep { -f } @configs) {
-    open(CONFIG, $_) or die "Can't open $_: $!\n";
-}
-
-unless (seek(CONFIG, 0, 0)) {
-    die "Can't open config (searched: @configs)\n";
-}
-
-while (<CONFIG>) {
-    chomp;
-    s/#.*//;
-    s/\t//g;
-    s/\s//g;
-    next unless length;
-    my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
-    $config{$cname} = $ccont;
-}
-close(CONFIG);
+my %config = get_config("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
 
 my $master_dir = $config{"master_dir"};
 my $conf_dir   = $config{"zone_conf_dir"};