| 1 | package Ubic::Settings; | ||||||||||||||||||
| 2 | 162 | $Ubic::Settings::VERSION = '1.59'; | |||||||||||||||||
| 3 | 162 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 162 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | # ABSTRACT: ubic settings | ||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 162 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 162 | ▶ | use Ubic::Settings::ConfigFile; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 162 | our $settings; | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | sub _file_settings { | ||||||||||||||||||
| 16 | 157 | ▶ | my $file_settings = {}; | ||||||||||||||||
|
Subroutine
_file_settings: called CC=4 SCAR=14.2
| |||||||||||||||||||
| 17 | 157 | my @files; | |||||||||||||||||
| 18 | 157 | ▶ | if ($ENV{HOME}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 19 | 157 | push @files, "$ENV{HOME}/.ubic.cfg"; | |||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | 157 | push @files, '/etc/ubic/ubic.cfg'; | |||||||||||||||||
| 22 | 157 | push @files, '/usr/local/etc/ubic/ubic.cfg'; | |||||||||||||||||
| 23 | 157 | for my $file (@files) { | |||||||||||||||||
| 24 | 471 | ▶ | next unless -e $file; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 25 | 0 | return Ubic::Settings::ConfigFile->read($file); | |||||||||||||||||
| 26 | } | ||||||||||||||||||
| 27 | 157 | 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 | 157 | ▶ | ($ENV{UBIC_DEFAULT_USER} ? (default_user => $ENV{UBIC_DEFAULT_USER}) : ()), | ||||||||||||||||
|
Subroutine
_env_settings: called CC=4 SCAR=13.9
Branch: 3 branches
| |||||||||||||||||||
| 35 | }; | ||||||||||||||||||
| 36 | } | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | sub _load { | ||||||||||||||||||
| 39 | 1019 | ▶ | return $settings if $settings; | ||||||||||||||||
|
Subroutine
_load: called CC=5 SCAR=16.2
Branch: 1 branch
| |||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 157 | my $file_settings = _file_settings; | |||||||||||||||||
| 42 | 157 | my $env_settings = _env_settings; | |||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 157 | $settings = { | |||||||||||||||||
| 45 | %$file_settings, | ||||||||||||||||||
| 46 | %$env_settings, | ||||||||||||||||||
| 47 | }; | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 157 | ▶ | unless (keys %$settings) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | 1 | die "ubic is not configured, run 'ubic-admin setup' first\n"; | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | 156 | for (qw/ service_dir data_dir default_user /) { | |||||||||||||||||
| 53 | 468 | ▶ | unless (defined $settings->{$_}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 54 | 0 | die "incomplete ubic configuration, setting '$_' is not defined"; | |||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 156 | return $settings; | |||||||||||||||||
| 59 | } | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | sub service_dir { | ||||||||||||||||||
| 62 | 206 | ▶ | my ($class, $value) = validate_pos(@_, 1, 0); | ||||||||||||||||
|
Subroutine
service_dir: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 63 | 206 | ▶ | if (defined $value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 64 | 58 | $ENV{UBIC_SERVICE_DIR} = $value; | |||||||||||||||||
| 65 | 58 | undef $settings; | |||||||||||||||||
| 66 | 58 | return; | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | 148 | return _load->{service_dir}; | |||||||||||||||||
| 69 | } | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | sub data_dir { | ||||||||||||||||||
| 72 | 275 | ▶ | my ($class, $value) = validate_pos(@_, 1, 0); | ||||||||||||||||
|
Subroutine
data_dir: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 73 | 275 | ▶ | if (defined $value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 74 | 58 | $ENV{UBIC_DIR} = $value; | |||||||||||||||||
| 75 | 58 | undef $settings; | |||||||||||||||||
| 76 | 58 | return; | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | 217 | return _load->{data_dir}; | |||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | sub default_user { | ||||||||||||||||||
| 82 | 574 | ▶ | my ($class, $value) = validate_pos(@_, 1, 0); | ||||||||||||||||
|
Subroutine
default_user: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 83 | 574 | ▶ | if (defined $value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 58 | $ENV{UBIC_DEFAULT_USER} = $value; | |||||||||||||||||
| 85 | 58 | undef $settings; | |||||||||||||||||
| 86 | 58 | return; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | 516 | return _load->{default_user}; | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | sub check_settings { | ||||||||||||||||||
| 92 | 138 | ▶ | _load; | ||||||||||||||||
|
Subroutine
check_settings: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 162 | 1; | |||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||