lib/Smolder/Conf.pm

filter: Statementstmt 89.1% 66 / 74 Branchbran 50.0% 10 / 20 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 89.4% 17 / 19 Podpod 100% 4 / 4 totaltotal 77.5% 100 / 129 CC 17 SCAR 12.6
CC
17
Cov
77%
init_from_file
7.7
AUTOLOAD
6.0
BEGIN
4.7
CRAP 3.5
« blib/lib/Smolder/Server/PreFork.pm ↑ Summary blib/lib/Smolder/DB/TestFileResult.pm »
1 package Smolder::Conf;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Spec::Functions qw(catfile catdir rel2abs curdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Smolder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub _random_secret {
Subroutine _random_secret: called CC=2 SCAR=6.9
12 1 my $length = int(rand(5) + 10);
13 1 my $secret = '';
14 1 my @chars = ('a'..'z', 'A'..'Z', 0..9);
15 1 $secret .= $chars[int(rand($#chars + 1))] for(0..$length);
16 1 return $secret;
17 }
18  
19 1 my %VALUES;
20  
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=15.5
22 1 my $share_dir;
23 1 my $blib_share_dir = rel2abs(catdir(curdir, 'blib', 'lib', 'auto', 'share', 'dist', 'Smolder'));
24 1 ▶ if( -d $blib_share_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $blib_share_dir) { } 1 0 1
25 1 $share_dir = $blib_share_dir;
26 } else {
27 0 $share_dir = dist_dir('Smolder');
28 }
29  
30 1 ▶ my $default_hostname = $ENV{HOSTNAME} || $ENV{HOST} || 'localhost.localdomain';
Condition40%2/5
$ENV{'HOSTNAME'} || $ENV{'HOST'} || 'localhost.localdomain'
l!l
10
$ENV{"HOSTNAME"} or $ENV{"HOST"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'HOSTNAME'} || $ENV{'HOST'} || 'localhost.localdomain'
$ENV{"HOSTNAME"} $ENV{"HOST"}
Truth table33%1/3
A or B
A= $ENV{"HOSTNAME"}B= $ENV{"HOST"}
ABresult
1X1
011
000
31 1 %VALUES = (
32 Port => 8080,
33 HostName => $default_hostname,
34 FromAddress => "smolder\@$default_hostname",
35 SMTPHost => $default_hostname,
36 LogFile => '',
37 LogLevel => 'warning',
38 PidFile => undef,
39 TemplateDir => catdir($share_dir, 'templates'),
40 DataDir => catdir(File::HomeDir->my_data, '.smolder'),
41 HtdocsDir => catdir($share_dir, 'htdocs'),
42 SQLDir => catdir($share_dir, 'sql'),
43 Secret => _random_secret(),
44 AutoRefreshReports => 0,
45 TruncateTestFilenames => 0,
46 ErrorsToScreen => 0,
47 ReportsPerPage => 5,
48 AutoRedirectToProject => 0,
49 );
50 }
51  
52 =head1 NAME
53  
54 Smolder::Conf - Smolder configuration module
55  
56 =head1 SYNOPSIS
57  
58 # all configuration directives are available as exported subs
59 use Smolder::Conf qw(Secret Port);
60 $secret = Secret;
61  
62 # you can also call get() in Smolder::Conf directly
63 $port = Smolder::Conf->get("Port");
64  
65 # or you can access them as methods in the Smolder::Conf module
66 $port = Smolder::Conf->Port;
67  
68 # give Smolder::Conf some values to override the defaults
69 Smolder::Conf->init(
70 Secret => '1Adxd23023s',
71 Port => 80,
72 HostName => 'smolder.myorg.com',
73 LogFile => '/path/to/logs',
74 );
75  
76 # pull the conf values from a file
77 Smolder::Conf->init_from_file('/path/to/conf/file');
78  
79 =head1 DESCRIPTION
80  
81 This module provides access to the configuration settings in
82 F<smolder.conf>. Smolder tries to have reasonable defaults but they
83 can be overridden when needed.
84  
85 =head1 OPTIONS
86  
87 The following configuration options are available:
88  
89 =head2 Port
90  
91 Port on which Smolder should listen.
92  
93 Default: 8080
94  
95 =head2 HostName
96  
97 Host name to listen to.
98  
99 Default: localhost.localdomain.
100  
101 =head2 FromAddress
102  
103 Email address from which reports should be sent.
104  
105 Default: smolder@localhost.localdomain
106  
107 =head2 SMTPHost
108  
109 Hostname through which SMTP mail should be sent.
110  
111 Default: localhost.localdomain
112  
113 =head2 LogFile
114  
115 Log file to write to.
116  
117 Default: none
118  
119 =head2 LogLevel
120  
121 The level at which things will start to be logged. Only used if LogFile is set.
122  
123 Default: warning
124  
125 =head2 PidFile
126  
127 File in which to write the PID of the Smolder process.
128 A PidFile is required for smolderctl to work.
129  
130 Default: none
131  
132 =head2 TemplateDir
133  
134 Source of Smolder's templates.
135  
136 Default: templates in the share directory
137  
138 =head2 DataDir
139  
140 Directory for all Smolder's stored data.
141  
142 Default: .smolder in the user's home directory
143  
144 =head2 HtdocsDir
145  
146 Smolder's HTML documents directory.
147  
148 Default: htdocs in the share directory
149  
150 =head2 SQLDir
151  
152 Smolder's SQL directory.
153  
154 Default: sql in the share directory
155  
156 =head2 Secret
157  
158 XXX
159  
160 Default: XXX
161  
162 =head2 AutoRefreshReports
163  
164 XXX
165  
166 Default: 0
167  
168 =head2 TruncateTestFilenames
169  
170 XXX
171  
172 Default: 0
173  
174 =head2 ErrorsToScreen
175  
176 XXX
177  
178 Default: 0
179  
180 =head2 ReportsPerPage
181  
182 Number of reports to show per Smolder web screen.
183  
184 Default: 5
185  
186 =head2 AutoRedirectToProject
187  
188 XXX
189  
190 Default: 0
191  
192 =head1 METHODS
193  
194 =head2 init
195  
196 Override the configuration defaults by providing named-value pairs:
197  
198 Smolder::Conf->init(
199 Secret => '1Adxd23023s',
200 Port => 80,
201 HostName => 'smolder.myorg.com',
202 LogFile => '/path/to/logs',
203 );
204  
205 =cut
206  
207 1 ▶sub init {
Subroutine init: called CC=3 SCAR=11.7
Pod documented
208 1 my ($class, %args) = @_;
209 1 foreach my $key (keys %args) {
210 8 ▶ if( exists $VALUES{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $VALUES{$key}) { } 8 0 8
211 8 $VALUES{$key} = $args{$key};
212 } else {
213 0 croak "$key is not a valid Smolder config parameter!";
214 }
215 }
216 }
217  
218 =head2 init_from_file
219  
220 Override the configuration defaults by providing a file. Config files are simple
221 lists of name-values pairs. One pair per-line and each name/value is separated by
222 whitespace:
223  
224 HostName smolder.test
225 DataDir /var/lib/smolder/
226 Port 80
227 FromAddress smolder@smolder.test
228 TemplateDir /var/share/smolder/templates
229 HtdocsDir /var/share/smolder/htdocs
230 SQLDir /var/share/smolder/sql
231  
232 =cut
233  
234 1 ▶sub init_from_file {
Subroutine init_from_file: called CC=7 SCAR=20.4
Pod documented
235 1 my ($class, $file) = @_;
236 1 ▶ croak "Config file $file does not exist!" unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 1 1
237 1 ▶ croak "Config file $file is not readable!" unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 1 1
238  
239 1 ▶ open(my $FH, '<', $file) or croak "Could not open file $file for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, "<", $file or croak("Could not open file $file for reading: $!") 1 0 1
240 1 my %values;
241 1 ▶ while(my $line = <$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $FH)) 8 1 9
242 # only get lines that look like name-value pairs but not comments
243 8 ▶ if( $line !~ /\s*#/ && $line =~ /(\S+)\s+(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /\s*#/ and $line =~ /(\S+)\s+(\S+)/) 8 0 8
Condition33%1/3
not $line =~ /\s*#/ and $line =~ /(\S+)\s+(\S+)/
!ll&&!rl&&r
008
MC/DC0%0/2
not $line =~ /\s*#/ and $line =~ /(\S+)\s+(\S+)/
not $line =~ /\s*#/ $line =~ /(\S+)\s+(\S+)/
Truth table33%1/3
not(A) and B
A= not $line =~ /\s*#/B= $line =~ /(\S+)\s+(\S+)/
ABresult
0X0
100
111
244 # strip off any quotes
245 8 my ($key, $val) = ($1, $2);
246 8 $val =~ s/^'(.*)'$/$1/;
247 8 $val =~ s/^"(.*)"$/$1/;
248 8 $values{$key} = $val;
249 }
250 }
251 1 $class->init(%values);
252 }
253  
254 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
255 1 ▶ __PACKAGE__->init_from_file($ENV{SMOLDER_CONF}) if $ENV{SMOLDER_CONF};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SMOLDER_CONF"} 1 0 1
256 }
257  
258 =head2 get
259  
260 Given a directive name, returns the value (which may be a list) of a configuration directive.
261 Directive names are case-insensitive.
262  
263 $value = Smolder::Conf->get('DirectiveName');
264  
265 =cut
266  
267 1 ▶sub get {
Subroutine get: called CC=2 SCAR=7.6
Pod documented
268 1 my ($class, $key) = @_;
269 1 ▶ if( exists $VALUES{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $VALUES{$key}) { } 1 0 1
270 1 return $VALUES{$key};
271 } else {
272 0 croak "$key is not a valid Smolder config parameter!";
273 }
274 }
275  
276 =head2 test_data_dir
277  
278 The directory path for test data directory for this copy of Smolder
279  
280 =cut
281  
282 0 ▶sub test_data_dir {
Subroutine test_data_dir: not called CC=1 SCAR=6.9
Pod documented
283 0 return rel2abs(catdir(curdir(), 't', 'data'));
284 }
285  
286 =head1 ACCESSOR METHODS
287  
288 All configuration directives can be accessed as methods themselves.
289  
290 my $port = Smolder::Conf->port();
291  
292 Gets the value of a directive using an autoloaded method.
293 Directive names are case-insensitive.
294  
295 =cut
296  
297 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
298 0 our $AUTOLOAD;
299 0 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Smolder::Conf::AUTOLOAD =~ /DESTROY$/ 0 0 0
300 0 my ($name) = $AUTOLOAD =~ /([^:]+)$/;
301  
302 0 return shift->get($name);
303 }
304  
305 =head1 EXPORTING DIRECTIVES
306  
307 Each configuration directive can also be accessed as an exported
308 subroutine.
309  
310 use Smolder::Conf qw(port FromAddress);
311 ...
312 my $port = port();
313 my $from = FromAddress();
314  
315 Directive names are case-insensitive.
316 Gets the value of a variable using an exported, autoloaded method.
317 Case-insensitive.
318  
319 =cut
320  
321 1 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
322 1 my $pkg = shift;
323 1 my $callpkg = caller(0);
324  
325 1 foreach my $name (@_) {
326 1 ▶ no strict 'refs'; # needed for glob refs
Subroutine BEGIN: called CC=1 SCAR=0.0
327 2 ▶ *{"$callpkg\::$name"} = sub () { $pkg->get($name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
328 }
329 }
330  
331 1 1;
« blib/lib/Smolder/Server/PreFork.pm ↑ Summary blib/lib/Smolder/DB/TestFileResult.pm »