]> Frank Brehm's Git Trees - my-stuff/postfix.git/commitdiff
Konfiguration gelesen
authorFrank Brehm <frank@brehm-online.com>
Mon, 3 Nov 2008 21:16:13 +0000 (21:16 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 3 Nov 2008 21:16:13 +0000 (21:16 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/postfix@21 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

get-lookup-tables.pl

index 81e7a3d891d414f1d709e1a5cd66cd6e6a73e261..eac5a8640d47d1a1553ff0fc3c7733ebb77c0a19 100755 (executable)
@@ -10,23 +10,180 @@ use File::Spec::Functions;
 use Config::General;
 use File::Basename;
 use Cwd qw( abs_path );
+use Data::Dumper;
+use Getopt::Long;
 use DBI;
 
 $| = 1;
 
+my $verbose = 0;
 
 my $basename    = basename($0);
 $basename       =~ s/\.pl$//i;
 my $postfix_dir = abs_path( catfile( '', 'etc', 'postfix' ) );
-my $conf        = abs_path( catfile( $postfix_dir, $basename . ".conf" ) );
+my $cfg_file    = abs_path( catfile( $postfix_dir, $basename . ".conf" ) );
+
+GetOptions(
+    "verbose+"              => \$verbose,
+    "conf-file|conf|c=s"    => \$cfg_file,
+);
 
 unless ( -d $postfix_dir ) {
-    die "Verzeichnis '$postfix_dir' existiert nicht.\n";
+    fatal( "Verzeichnis '$postfix_dir' existiert nicht." );
+}
+
+fatal( "Datei '$cfg_file' existiert nicht." ) unless -f $cfg_file;
+my $config = read_config($cfg_file);
+
+
+
+
+#--------------------------------------------------------------------------------------
+
+=head2 read_config( $file )
+
+=cut
+
+sub read_config {
+
+    my $cfg_file = shift;
+    
+    debug( 2, "Lese Config-Datei '" . $cfg_file . "' ..." );
+
+    my $config = {};
+
+    eval {
+        my $conf = new Config::General(
+            '-ConfigFile'       => $cfg_file,
+            '-ApacheCompatible' => 1,
+        );
+        %$config = $conf->getall();
+    };
+    if ( $@ ) {
+        fatal( "Fehler beim Lesen der Konfigurationsdatei '" . $cfg_file . "': ", $@ );
+    }
+
+    debug( 2, "Gelesene Konfiguration: ", $config );
+
+    return $config;
 }
 
-die "Datei '$conf' existiert nicht.\n" unless -f $conf;
+#--------------------------------------------------------------------------------------
+
+=head2 msg_line( $text1, [$text2, ... ] )
+
+=cut
+
+sub msg_line {
+
+    my $out = '';
+
+    for my $p ( @_ ) {
+        my $text = ' <undef>';
+        if ( defined $p ) {
+            if ( ref($p) ) {
+                {
+                    local $Data::Dumper::Purity   = 1;
+                    local $Data::Dumper::Indent   = 1;
+                    local $Data::Dumper::Sortkeys = 1;
+                    $text = Dumper($p);
+                    $text =~ s/^\s*\$[a-zA-Z][a-zA-Z0-9]*\s*=\s*//;
+                }
+            }
+            else {
+                $text = $p;
+            }
+        }
+        $out .= $text;
+    }
+
+    $out =~ s/^\s+//;
+    $out =~ s/\s+$//;
+    return $out;
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 debug( $debug_level, $text1, [$text2, ... ] )
+
+=cut
+
+sub debug {
+
+    my $debug_level = shift;
+    return undef unless $verbose >= $debug_level;
+
+    my $out = msg_line(@_);
+
+    my @C0 = caller(0);
+    my @C1 = caller(1);
+    my $k = sprintf( "%s() Zeile %d: ", ( @C1 ? $C1[3] : 'main' ), $C0[2] );
+
+    warn $k . $out . "\n";
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 error( $error_level, $text1, [$text2, ... ] )
+
+=cut
+
+sub error {
+
+    my $error_level = shift;
+    my $out = msg_line(@_);
+
+    my @C0 = caller(0);
+    my @C1 = caller(1);
+    my $k = sprintf( "%s() Zeile %d %s: ", ( @C1 ? $C1[3] : 'main' ), $C0[2], $error_level );
+    $k = $error_level . ": " unless $verbose;
+
+    warn $k . $out . "\n";
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 notice( $text1, [$text2, ... ] )
+
+=cut
+
+sub notice {
+
+    my $out = msg_line(@_);
+
+    my @C0 = caller(0);
+    my @C1 = caller(1);
+    my $k = sprintf( "%s() Zeile %d: ", ( @C1 ? $C1[3] : 'main' ), $C0[2] );
+    $k = "" unless $verbose;
+
+    warn $k . $out . "\n";
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 fatal( $text1, [$text2, ... ] )
+
+=cut
+
+sub fatal {
+
+    my $out = msg_line(@_);
+
+    my @C0 = caller(0);
+    my @C1 = caller(1);
+    my $k = sprintf( "%s() Zeile %d: ", ( @C1 ? $C1[3] : 'main' ), $C0[2] );
+    $k = "" unless $verbose;
+
+    warn $k . $out . "\n";
+    exit 88;
+
+}
 
 
+#--------------------------------------------------------------------------------------
 
 __END__