Crane/Config.pm

filter: Statementstmt 86.5% 58 / 67 Branchbran 59.3% 19 / 32 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 73.4% 94 / 128 CC 23 SCAR 19.6
CC
23
Cov
74%
write_config
12.5
read_config
7.7
load_config
7.0
CRAP 7.1
« Crane/Options.pm ↑ Summary Crane/Logger.pm »
1 # -*- coding: utf-8 -*-
2  
3  
4 package Crane::Config;
5  
6  
7 3 ▶use Crane::Base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use File::Spec::Functions qw( catdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use YAML::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 3 our @EXPORT = qw(
15 &config
16 );
17  
18 3 our @EXPORT_OK = qw(
19 &merge_config
20 &read_config
21 &write_config
22 &load_config
23 );
24  
25  
26 3 my $DEFAULT_FILENAME = catdir('etc', 'default.conf');
27  
28 3 my $DEFAULT_CONFIG = {
29 'log' => {
30 'level' => undef, # Default log level
31
32 'filename' => catdir('log', 'messages.log'), # Path to log file (undef -> stdout)
33 'error_filename' => catdir('log', 'errors.log'), # Path to error log file (undef -> stderr)
34 },
35 };
36  
37  
38 9 ▶sub config {
Subroutine config: called CC=3 SCAR=11.0
39
40 9 return state $config = do {
41 2 my ( $config, @filenames ) = @_;
42
43 Readonly::Hash(my %config => %{
44 2 ▶ load_config(
Branch: 2 branches
BranchTrueFalseTotal
ref $config eq 'HASH' ? : 1 1 2
scalar @filenames ? : 0 2 2
45 merge_config(
46 $DEFAULT_CONFIG,
47 ref $config eq 'HASH' ? $config : {},
48 ),
49
50 scalar @filenames ? @filenames : $DEFAULT_FILENAME,
51 )
52 });
53
54 2 \%config;
55 };
56
57 }
58  
59  
60 4 ▶sub merge_config {
Subroutine merge_config: called CC=5 SCAR=16.1
Pod documented
61
62 4 my ( $original, $config ) = @_;
63
64 4 my $type_original = ref $original;
65 4 my $type_config = ref $config;
66
67 4 ▶ if ( $type_original eq $type_config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_original eq $type_config) { } 3 1 4
68 3 ▶ if ( $type_config eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_config eq "HASH") 3 0 3
69 3 foreach my $key ( keys %{ $config } ) {
70 3 ▶ if ( exists $original->{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $original->{$key}) { } 1 2 3
71 1 $original->{ $key } = merge_config($original->{ $key }, $config->{ $key });
72 } else {
73 2 $original->{ $key } = $config->{ $key };
74 }
75 }
76 }
77 } else {
78 1 $original = $config;
79 }
80
81 4 return $original;
82
83 }
84  
85  
86 1 ▶sub read_config {
Subroutine read_config: called CC=6 SCAR=20.5
Pod documented
87
88 1 my ( $filename ) = @_;
89
90 1 ▶ if ( not defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 1 1
91 0 confess('No file name given');
92 }
93
94 1 my $config = {};
95
96 1 ▶ if ( open my $fh, '<:encoding(UTF-8)', $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<:encoding(UTF-8)', $filename) { } 1 0 1
97 $config = eval {
98 1 local $INPUT_RECORD_SEPARATOR = undef;
99 1 ▶ return ( YAML::Load(<$fh>) )[0] || {};
Condition50%1/2
(YAML::Load(readline $fh))[0] || {}
l!l
10
MC/DC0%0/1
(YAML::Load(readline $fh))[0] || {}
(YAML::Load(readline $fh))[0]
Truth table50%1/2
A
A= (YAML::Load(readline $fh))[0]
Aresult
11
00
100 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
$config = eval { do { local $Crane::Config::INPUT_RECORD_SEPARATOR = undef; return (YAML::Load(readline $fh))[0] || {} } } or do { confess("Incorrect syntax in '${filename}': $Crane::Config::EVAL_ERROR") } 1 0 1
101 0 confess("Incorrect syntax in '$filename': $EVAL_ERROR");
102 };
103
104 1 ▶ close $fh or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or confess($Crane::Config::OS_ERROR) 1 0 1
105 } else {
106 0 confess("Unable to read config '$filename': $OS_ERROR");
107 }
108
109 1 return $config;
110
111 }
112  
113  
114 1 ▶sub write_config {
Subroutine write_config: called CC=8 SCAR=25.3
Pod documented
115
116 1 my ( $config, $filename ) = @_;
117
118 1 ▶ if ( ref $config ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config ne "HASH") 0 1 1
119 0 confess('Configuration should be a hash reference');
120 }
121
122 1 ▶ if ( not defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 1 1
123 0 confess('No file name given');
124 }
125
126 # Init YAML
127 1 state $yaml = YAML::Dumper->new(
128 'indent_width' => 4,
129 'sort_keys' => 1,
130 'use_header' => 0,
131 'use_version' => 0,
132 'use_block' => 1,
133 'use_fold' => 1,
134 'use_aliases' => 0,
135 );
136
137 # Dump configuration
138 1 ▶ if ( open my $fh, '>:encoding(UTF-8)', $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>:encoding(UTF-8)', $filename) { } 1 0 1
139 1 ▶ if ( not eval { print { $fh } $yaml->dump($config) or confess($OS_ERROR) } or $EVAL_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not eval { do { print {$fh;} $yaml->dump($config) or confess($Crane::Config::OS_ERROR) } } or $Crane::Config::EVAL_ERROR) 0 1 1
Condition40%2/5
not eval { do { print {$fh;} $yaml->dump($config) or confess($Crane::Config::OS_ERROR) } } or $Crane::Config::EVAL_ERROR
l!l&&r!l&&!r
001
print({$fh;} $yaml->dump($config)) || confess($Crane::Config::OS_ERROR)
l!l
10
MC/DC0%0/2
not eval { do { print {$fh;} $yaml->dump($config) or confess($Crane::Config::OS_ERROR) } } or $Crane::Config::EVAL_ERROR
not eval { do { print {$fh;} $yaml->dump($config) or confess($Crane::Config::OS_ERROR) } } $Crane::Config::EVAL_ERROR
MC/DC0%0/1
print({$fh;} $yaml->dump($config)) || confess($Crane::Config::OS_ERROR)
print({$fh;} $yaml->dump($config))
Truth table33%1/3
not(A) or B
A= not eval { do { print {$fh;} $yaml->dump($config) or confess($Crane::Config::OS_ERROR) } }B= $Crane::Config::EVAL_ERROR
ABresult
1X1
011
000
Truth table50%1/2
A
A= print({$fh;} $yaml->dump($config))
Aresult
11
00
140 0 confess("YAML error while writing '$filename': $EVAL_ERROR");
141 }
142
143 1 ▶ close $fh or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or confess($Crane::Config::OS_ERROR) 1 0 1
144 } else {
145 0 confess("Unable to write config '$filename': $OS_ERROR");
146 }
147
148 1 return;
149
150 }
151  
152  
153 2 ▶sub load_config {
Subroutine load_config: called CC=5 SCAR=19.4
Pod documented
154
155 2 my ( $config, @filenames ) = @_;
156
157 2 ▶ if ( ref $config ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config ne "HASH") 0 2 2
158 0 confess('Configuration should be a hash reference');
159 }
160
161 2 foreach my $filename ( @filenames ) {
162 2 ▶ if ( defined $filename and -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and -e $filename) 0 2 2
Condition33%1/3
defined $filename and -e $filename
!ll&&!rl&&r
020
MC/DC0%0/2
defined $filename and -e $filename
defined $filename -e $filename
Truth table33%1/3
A and B
A= defined $filenameB= -e $filename
ABresult
0X0
100
111
163 0 $config = merge_config($config, read_config($filename));
164 }
165 }
166
167 2 return $config;
168
169 }
170  
171  
172 3 1;
173  
174  
175 =head1 NAME
176  
177 Crane::Config - Configuration manager
178  
179  
180 =head1 SYNOPSIS
181  
182 use Crane::Config;
183
184 my $filename = config->{'log'}->{'filename'};
185  
186  
187 =head1 DESCRIPTION
188  
189 Configuration manager which operates with YAML configurations. Settings are
190 available as a hash reference returned by L<config|/"config ($config,
191 @filenames)"> function.
192  
193 You can specify default configuration and filename by passing it to
194 L<config|/"config ($config, @filenames)"> function when first call (see
195 description below).
196  
197  
198 =head1 EXPORTED FUNCTIONS
199  
200 =over
201  
202 =item B<config> (I<$config>, I<@filenames>)
203  
204 Returns link to current configuration.
205  
206 At first call you can specify default configuration I<$config> and/or list of
207 config file names I<@filenames>.
208  
209  
210 =head1 FUNCTIONS
211  
212 =over
213  
214 =item B<merge_config> (I<$original>, I<$config>)
215  
216 Merge two configs (I<$config> to I<$original>).
217  
218 =item B<read_config> (I<$filename>)
219  
220 Reads confugration from file named I<$filename>.
221  
222 =item B<write_config> (I<$config>, I<$filename>)
223  
224 Saves configuration I<$config> to file named I<$filename>.
225  
226 =item B<load_config> (I<$config>, I<@filenames>)
227  
228 Load configurations from files named I<@filenames> and merges them to
229 configuration I<$config> and I<default> configuration.
230  
231 =back
232  
233  
234 =head1 ERRORS
235  
236 =over
237  
238 =item Incorrect syntax in 'I<%s>': I<%s>
239  
240 Where I<%s> is file name and I<%s> is error message.
241  
242 Invalid YAML configuration file.
243  
244 =item Unable to read config 'I<%s>': I<%s>
245  
246 Where I<%s> is file name and I<%s> is error message.
247  
248 Fires when unable to open configuration for read.
249  
250 =item Unable to write config 'I<%s>': I<%s>
251  
252 Where I<%s> is file name and I<%s> is error message.
253  
254 Fires when unable to open configuration for write.
255  
256 =item YAML error while writing 'I<%s>': I<%s>
257  
258 Where I<%s> is file name and I<%s> is error message.
259  
260 =item Configuration should be a hash reference
261  
262 Fires when function required hash reference as a configuration.
263  
264 =item No filename given
265  
266 Fires when function required name of file but it is undefined.
267  
268 =back
269  
270  
271 =head1 EXAMPLES
272  
273 Configuration file
274  
275 domain: "production"
276
277 log:
278 level: 0
279 filename: "/var/log/example/messages.log"
280 error_filename: "/var/log/example/errors.log"
281
282 servers:
283 - "127.0.0.1:3001"
284 - "127.0.0.1:3002"
285  
286 Which results to hash reference:
287  
288 {
289 'domain' => 'production',
290
291 'log' => {
292 'level' => '0',
293
294 'filename' => '/var/log/example/messages.log',
295 'error_filename' => '/var/log/example/errors.log',
296 },
297
298 'servers' => [
299 '127.0.0.1:3001',
300 '127.0.0.1:3002',
301 ],
302 }
303  
304  
305 =head1 ENVIRONMENT
306  
307 See L<Crane::Base|Crane::Base/"ENVIRONMENT">.
308  
309  
310 =head1 FILES
311  
312 =over
313  
314 =item F<etc/default.conf>
315  
316 Default configuration file (may not exist).
317  
318 =back
319  
320  
321 =head1 BUGS
322  
323 Please report any bugs or feature requests to
324 L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Crane> or to
325 L<https://github.com/temoon/crane/issues>.
326  
327  
328 =head1 AUTHOR
329  
330 Tema Novikov, <novikov.tema@gmail.com>
331  
332  
333 =head1 COPYRIGHT AND LICENSE
334  
335 Copyright (C) 2013-2014 Tema Novikov.
336  
337 This library is free software; you can redistribute it and/or modify it under
338 the terms of the Artistic License 2.0. For details, see the full text of the
339 license in the file LICENSE.
340  
341  
342 =head1 SEE ALSO
343  
344 =over
345  
346 =item * B<RT Cpan>
347  
348 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Crane>
349  
350 =item * B<Github>
351  
352 L<https://github.com/temoon/crane>
353  
354 =back
« Crane/Options.pm ↑ Summary Crane/Logger.pm »