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.58_01'; # TRIAL
3 28     28   1274 use strict;
  28         39  
  28         806  
4 28     28   120 use warnings;
  28         37  
  28         938  
5              
6             # ABSTRACT: ubic settings
7              
8              
9 28     28   124 use Params::Validate qw(:all);
  28         35  
  28         4489  
10              
11 28     28   9804 use Ubic::Settings::ConfigFile;
  28         56  
  28         14605  
12              
13             our $settings;
14              
15             sub _file_settings {
16 26     26   71 my $file_settings = {};
17 26         47 my @files;
18 26 50       201 if ($ENV{HOME}) {
19 26         145 push @files, "$ENV{HOME}/.ubic.cfg";
20             }
21 26         106 push @files, '/etc/ubic/ubic.cfg';
22 26         89 push @files, '/usr/local/etc/ubic/ubic.cfg';
23 26         94 for my $file (@files) {
24 78 50       1453 next unless -e $file;
25 0         0 return Ubic::Settings::ConfigFile->read($file);
26             }
27 26         119 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 26 50   26   422 ($ENV{UBIC_DEFAULT_USER} ? (default_user => $ENV{UBIC_DEFAULT_USER}) : ()),
    50          
    50          
35             };
36             }
37              
38             sub _load {
39 156 100   156   1014 return $settings if $settings;
40              
41 26         182 my $file_settings = _file_settings;
42 26         103 my $env_settings = _env_settings;
43              
44 26         174 $settings = {
45             %$file_settings,
46             %$env_settings,
47             };
48              
49 26 50       694 unless (keys %$settings) {
50 0         0 die "ubic is not configured, run 'ubic-admin setup' first\n";
51             }
52 26         82 for (qw/ service_dir data_dir default_user /) {
53 78 50       236 unless (defined $settings->{$_}) {
54 0         0 die "incomplete ubic configuration, setting '$_' is not defined";
55             }
56             }
57              
58 26         178 return $settings;
59             }
60              
61             sub service_dir {
62 81     81 1 5174 my ($class, $value) = validate_pos(@_, 1, 0);
63 81 100       459 if (defined $value) {
64 60         1101 $ENV{UBIC_SERVICE_DIR} = $value;
65 60         121 undef $settings;
66 60         289 return;
67             }
68 21         56 return _load->{service_dir};
69             }
70              
71             sub data_dir {
72 84     84 1 1096 my ($class, $value) = validate_pos(@_, 1, 0);
73 84 100       915 if (defined $value) {
74 60         2141 $ENV{UBIC_DIR} = $value;
75 60         373 undef $settings;
76 60         289 return;
77             }
78 24         80 return _load->{data_dir};
79             }
80              
81             sub default_user {
82 161     161 1 1263 my ($class, $value) = validate_pos(@_, 1, 0);
83 161 100       763 if (defined $value) {
84 60         424 $ENV{UBIC_DEFAULT_USER} = $value;
85 60         175 undef $settings;
86 60         1075 return;
87             }
88 101         288 return _load->{default_user};
89             }
90              
91             sub check_settings {
92 10     10 1 47 _load;
93             }
94              
95              
96             1;
97              
98             __END__