diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-02-28 17:52:37 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-02-28 17:52:37 +0200 |
commit | bdc4ce4b346a08c67969b848ec69996ea9c46e00 (patch) | |
tree | ed0e6bcace653c711a56731050e08d3d298e5e4a /lib/App/Beam/Config.pm | |
parent | 2f40b28caabbd90f72c3e4faf7bbd46dc4d9ec88 (diff) | |
download | beam-bdc4ce4b346a08c67969b848ec69996ea9c46e00.tar.gz beam-bdc4ce4b346a08c67969b848ec69996ea9c46e00.tar.bz2 |
Improve run-time configuration framework; provide boilerplate for the backup subcommand
Diffstat (limited to 'lib/App/Beam/Config.pm')
-rw-r--r-- | lib/App/Beam/Config.pm | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/lib/App/Beam/Config.pm b/lib/App/Beam/Config.pm index d86b258..46624bb 100644 --- a/lib/App/Beam/Config.pm +++ b/lib/App/Beam/Config.pm @@ -32,6 +32,13 @@ sub error { print STDERR "$err\n"; } +sub debug { + my $self = shift; + my $lev = shift; + return unless $self->{debug} >= $lev; + $self->logger('debug', "config:", @_); +} + sub new { my $class = shift; my $filename = shift; @@ -41,12 +48,7 @@ sub new { my $err; if (defined($v = delete $_{debug})) { - if (ref($v) eq 'CODE') { - $self->{debug} = $v; - } else { - carp "debug must refer to a CODE"; - ++$err; - } + $self->{debug} = $v; } if (defined($v = delete $_{ci})) { @@ -101,8 +103,7 @@ sub writecache { return unless exists $self->{cachefile}; return unless exists $self->{conf}; return unless $self->{updated}; - &{$self->{debug}}("storing cache file $self->{cachefile}") - if exists $self->{debug}; + $self->debug(1, "storing cache file $self->{cachefile}"); store $self->{conf}, $self->{cachefile}; } @@ -190,7 +191,7 @@ sub readconfig { my $conf = shift; local %_ = @_; - &{$self->{debug}}("reading file $file") if exists $self->{debug}; + $self->debug(1, "reading file $file"); open(my $fd, "<", $file) or do { $self->error("can't open configuration file $file: $!"); @@ -301,6 +302,19 @@ sub readconfig { return $err; } +sub fixup { + my $self = shift; + my $params = shift; + while (my ($kv, $descr) = each %$params) { + next unless ref($descr) eq 'HASH'; + if (exists($descr->{section})) { + $self->fixup($descr->{section}, @_, $kv); + } elsif (exists($descr->{default}) && !$self->isset(@_, $kv)) { + $self->set(@_, $kv, $descr->{default}); + } + } +} + sub file_up_to_date { my ($self, $file) = @_; my $st_conf = stat($self->{filename}) or return 1; @@ -316,8 +330,7 @@ sub parse { if (exists($self->{cachefile}) and -f $self->{cachefile}) { if ($self->file_up_to_date($self->{cachefile})) { my $ref; - &{$self->{debug}}("reading from cache file $self->{cachefile}") - if exists $self->{debug}; + $self->debug(1, "reading from cache file $self->{cachefile}"); eval { $ref = retrieve($self->{cachefile}); }; if (defined($ref)) { $self->{conf} = $ref; @@ -330,11 +343,12 @@ sub parse { unlink $self->{cachefile}; } - &{$self->{debug}}("parsing $self->{filename}") if exists $self->{debug}; + $self->debug(1, "parsing $self->{filename}"); my $err = $self->readconfig($self->{filename}, \%conf); if ($err == 0) { $self->{conf} = \%conf ; $self->{updated} = 1; + $self->fixup($self->{parameters}) if exists $self->{parameters}; } return !$err; } |