Backups Blow - BackupsBlow/Config.pm
#!/usr/bin/perl -w
# Backups Blow by Brandon Low 
#
# Copyright 2007 Brandon Low 
# Released under the terms of the GPL v2
#
# http://lostlogicx.com/backupsblow/

package BackupsBlow::Config;

use strict;
use Exporter;
our (@EXPORT_OK, %EXPORT_TAGS, @ISA);
@ISA = qw(Exporter);
@EXPORT_OK = qw(&configEnabled &config &needConfig
    &configArray &needConfigArray &readConfig &configWhole);
%EXPORT_TAGS = (
    "setup" => [qw(&readConfig)],
    "all" => [@EXPORT_OK],
    "read" => [qw(&config &needConfig
        &configArray &needConfigArray &configWhole)]
);

use Log::Log4perl qw(:easy);
my $logger;

my %config;

sub config($;$) {
    my ($key,$default)=@_;
    return $config{$key}||$default;
}

sub needConfig($) {
    my $retval=$config{$_[0]};
    die("Config parameter $_[0] is required") unless ($retval);
    return $retval;
}

sub valueToArray($) {
    return $_[0]?split("[[:space:]]*,[[:space:]]*",$_[0]):();
}

sub configArray($;$) {
    my ($key,$default)=@_;
    return valueToArray(config($key,$default));
}

sub needConfigArray($) {
    return valueToArray(needConfig($_[0]));
}

sub configEnabled($;$) {
    my ($key,$default)=@_;
    return config($key,$default) =~ m/^(t(rue)?|1)$/i;
}

sub configWhole($;$) {
    my ($key,$default)=@_;
    my $retval=$config{$key};
    return $retval if ($retval && $retval == abs(int($retval)));
    return $default if ($default && $default == abs(int($default)));
    die("Config parameter $key and default $default nonwhole");
}

# Reads a config file and logging configuration
# First required parameter is the program name
# Second optional is an explicit config file other than the default
sub readConfig ($;$) {
    my $baseName;
    $baseName=$_[0];
    $baseName=~s/^.*\///;
    $baseName=~s/\.[^\.]*$//;

    my $configFile;
    if ($_[1] && -f $_[1]) {
        $configFile = $_[1];
    } else {
        if ( -f "/etc/$baseName.cfg" ) {
            $configFile="/etc/$baseName.cfg";
        } elsif ( -f "/etc/$baseName/$baseName.cfg" ) {
            $configFile="/etc/$baseName/$baseName.cfg";
        }
    }
    unless ($configFile && open(FILE,"<$configFile")) {
        print STDERR "Unable to open config file";
        exit(1);
    }
    while () {
        next if ( m/^[[:space:]]*(#.*)?$/ );
        chomp;
        s/[[:space:]]*(#.*)?$//;
        if ( m/^[^=]*=[^=]*$/ ) {
            s/^[[:space:]]*(.*)[[:space:]]*$/$1/;
            my @parts=split("[[:space:]]*=[[:space:]]*");
            $config{uc($parts[0])}=$parts[1];
        } else {
            # Do not use logger here, it's not configured
            print STDERR "Bad line in $configFile, ignored: @_\n";
        }
    }
    my $logConf=config("LOGCONF");
    unless ($logConf) {
        $logConf="/etc/$baseName/$baseName.log4perl";
    }
    if ( -f $logConf ) {
        Log::Log4perl->init($logConf);
    } else {
        Log::Log4perl->easy_init($DEBUG);
    }
    $logger=Log::Log4perl->get_logger();
}

1;
"You give me Governor Ventura, myself and eight more of my fellow Navy SEALS -- and we could paralyze the entire country of the United States of America" --Richard Marcinko
Google
 
© 2002-2008 Brandon Low 14 hits since September 7, 2008