blib/lib/Ubic/Settings.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 97 SCAR 50.5
CC
12
Cov
0%
_env_settings
20.0
_load
20.0
_file_settings
12.0
CRAP 156.0
1 package Ubic::Settings;
2 $Ubic::Settings::VERSION = '1.59';
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: ubic settings
7  
8  
9 use Params::Validate qw(:all);
10  
11 use Ubic::Settings::ConfigFile;
12  
13 our $settings;
14  
15 sub _file_settings {
16 my $file_settings = {};
17 my @files;
18 if ($ENV{HOME}) {
19 push @files, "$ENV{HOME}/.ubic.cfg";
20 }
21 push @files, '/etc/ubic/ubic.cfg';
22 push @files, '/usr/local/etc/ubic/ubic.cfg';
23 for my $file (@files) {
24 next unless -e $file;
25 return Ubic::Settings::ConfigFile->read($file);
26 }
27 return {};
28 }
29  
30 sub _env_settings {
31 return {
32 ($ENV{UBIC_SERVICE_DIR} ? (service_dir => $ENV{UBIC_SERVICE_DIR}) : ()),
33 ($ENV{UBIC_DIR} ? (data_dir => $ENV{UBIC_DIR}) : ()),
34 ($ENV{UBIC_DEFAULT_USER} ? (default_user => $ENV{UBIC_DEFAULT_USER}) : ()),
35 };
36 }
37  
38 sub _load {
39 return $settings if $settings;
40  
41 my $file_settings = _file_settings;
42 my $env_settings = _env_settings;
43  
44 $settings = {
45 %$file_settings,
46 %$env_settings,
47 };
48  
49 unless (keys %$settings) {
50 die "ubic is not configured, run 'ubic-admin setup' first\n";
51 }
52 for (qw/ service_dir data_dir default_user /) {
53 unless (defined $settings->{$_}) {
54 die "incomplete ubic configuration, setting '$_' is not defined";
55 }
56 }
57  
58 return $settings;
59 }
60  
61 sub service_dir {
62 my ($class, $value) = validate_pos(@_, 1, 0);
63 if (defined $value) {
64 $ENV{UBIC_SERVICE_DIR} = $value;
65 undef $settings;
66 return;
67 }
68 return _load->{service_dir};
69 }
70  
71 sub data_dir {
72 my ($class, $value) = validate_pos(@_, 1, 0);
73 if (defined $value) {
74 $ENV{UBIC_DIR} = $value;
75 undef $settings;
76 return;
77 }
78 return _load->{data_dir};
79 }
80  
81 sub default_user {
82 my ($class, $value) = validate_pos(@_, 1, 0);
83 if (defined $value) {
84 $ENV{UBIC_DEFAULT_USER} = $value;
85 undef $settings;
86 return;
87 }
88 return _load->{default_user};
89 }
90  
91 sub check_settings {
92 _load;
93 }
94  
95  
96 1;
97  
98 __END__