File Coverage

lib/Ubic/Settings.pm
Criterion Covered Total %
statement 49 52 94.2
branch 15 22 68.1
condition n/a
subroutine 11 11 100.0
pod 4 4 100.0
total 79 89 88.7


line stmt bran cond sub pod time code
1             package Ubic::Settings;
2             $Ubic::Settings::VERSION = '1.59';
3 26     26   1751 use strict;
  26         28  
  26         600  
4 26     26   74 use warnings;
  26         44  
  26         572  
5              
6             # ABSTRACT: ubic settings
7              
8              
9 26     26   84 use Params::Validate qw(:all);
  26         702  
  26         3416  
10              
11 26     26   6659 use Ubic::Settings::ConfigFile;
  26         49  
  26         10968  
12              
13             our $settings;
14              
15             sub _file_settings {
16 23     23   74 my $file_settings = {};
17 23         41 my @files;
18 23 50       154 if ($ENV{HOME}) {
19 23         96 push @files, "$ENV{HOME}/.ubic.cfg";
20             }
21 23         120 push @files, '/etc/ubic/ubic.cfg';
22 23         68 push @files, '/usr/local/etc/ubic/ubic.cfg';
23 23         75 for my $file (@files) {
24 69 50       1001 next unless -e $file;
25 0         0 return Ubic::Settings::ConfigFile->read($file);
26             }
27 23         161 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 23 50   23   282 ($ENV{UBIC_DEFAULT_USER} ? (default_user => $ENV{UBIC_DEFAULT_USER}) : ()),
    50          
    50          
35             };
36             }
37              
38             sub _load {
39 141 100   141   729 return $settings if $settings;
40              
41 23         137 my $file_settings = _file_settings;
42 23         86 my $env_settings = _env_settings;
43              
44 23         136 $settings = {
45             %$file_settings,
46             %$env_settings,
47             };
48              
49 23 50       405 unless (keys %$settings) {
50 0         0 die "ubic is not configured, run 'ubic-admin setup' first\n";
51             }
52 23         227 for (qw/ service_dir data_dir default_user /) {
53 69 50       179 unless (defined $settings->{$_}) {
54 0         0 die "incomplete ubic configuration, setting '$_' is not defined";
55             }
56             }
57              
58 23         136 return $settings;
59             }
60              
61             sub service_dir {
62 71     71 1 4044 my ($class, $value) = validate_pos(@_, 1, 0);
63 71 100       291 if (defined $value) {
64 51         294 $ENV{UBIC_SERVICE_DIR} = $value;
65 51         143 undef $settings;
66 51         191 return;
67             }
68 20         46 return _load->{service_dir};
69             }
70              
71             sub data_dir {
72 71     71 1 854 my ($class, $value) = validate_pos(@_, 1, 0);
73 71 100       428 if (defined $value) {
74 51         913 $ENV{UBIC_DIR} = $value;
75 51         238 undef $settings;
76 51         242 return;
77             }
78 20         49 return _load->{data_dir};
79             }
80              
81             sub default_user {
82 143     143 1 850 my ($class, $value) = validate_pos(@_, 1, 0);
83 143 100       595 if (defined $value) {
84 51         262 $ENV{UBIC_DEFAULT_USER} = $value;
85 51         87 undef $settings;
86 51         681 return;
87             }
88 92         203 return _load->{default_user};
89             }
90              
91             sub check_settings {
92 9     9 1 31 _load;
93             }
94              
95              
96             1;
97              
98             __END__