Qgoda/Init/config.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 76 CC 6 SCAR 31.7
CC
6
Cov
0%
run
42.0
new
2.0
BEGIN
2.0
CRAP 23.8
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Init::config;
20 $Qgoda::Init::config::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain qw('qgoda');
24 use YAML;
25  
26 use Qgoda::Util qw(merge_data read_file write_file);
27  
28 sub new {
29 my ($class, $init) = @_;
30  
31 bless {
32 __init => $init,
33 }, $class;
34 }
35  
36 sub run {
37 my ($self, $new) = @_;
38  
39 my $q = Qgoda->new;
40 my $logger = $q->logger;
41  
42 # Make a shallow copy of the new configuration ...
43 $new = {%$new};
44  
45 # ... and delete all keys that are not meant to be kept.
46 foreach my $key (keys %$new) {
47 delete $new->{$key} if $key =~ /^_/;
48 }
49  
50 my $filename;
51 if (-e '_config.yaml') {
52 $filename = '_config.yaml';
53 } elsif (-e '_config.yml') {
54 $filename = '_config.yml';
55 }
56  
57 if ($filename && !$self->{__init}->getOption('force')) {
58 $logger->warning(__x("not overwriting '{file}' without '--force'",
59 file => $filename));
60 $logger->warning(__"This can lead to malfunctions.");
61 $logger->warning(__"Please merge '_config.new.yaml' manually!");
62 my $yaml = YAML::Dump($new);
63 write_file '_config.new.yaml', $yaml
64 or $logger->fatal(__x("cannot write '{filename}': {error}",
65 filename => '_config.new.yaml',
66 error => $!));
67 }
68  
69 # Reread the current configuration. This allows us to preserve the order
70 # of the keys. Note that the XS version does not support that option.
71 my $old = {};
72 if ($filename) {
73 my $yaml = read_file $filename
74 or $logger->fatal(__x("cannot read '{filename}': {error}",
75 filename => $filename,
76 error => $!));
77  
78 local $YAML::Preserve = 1;
79 $old = YAML::Load($yaml);
80 }
81 my $data = merge_data $old, $new;
82  
83 $filename ||= '_config.yaml';
84  
85 my $yaml = YAML::Dump($data);
86 write_file $filename, $yaml
87 or $logger->fatal(__x("cannot write '{filename}': {error}",
88 filename => $filename,
89 error => $!));
90  
91 # Reload the new configuration.
92 $logger->info(__"force reload of configuration");
93 $q->_reload;
94  
95 return $self;
96 }
97  
98 1;