| 1 | package RackMan::Config; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Config::IniFiles; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use File::Spec::Functions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use RackMan; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | LOCAL_CONFIG_FILE => "rack.local.conf", | |||||||||||||||||||||||||||
| 12 | 2 | }; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | my %defaults = ( | |||||||||||||||||||||||||||
| 16 | general => { | ||||||||||||||||||||||||||||
| 17 | default_scm => "none", | ||||||||||||||||||||||||||||
| 18 | ntp_servers => "0.0.0.0 0.0.0.0", | ||||||||||||||||||||||||||||
| 19 | }, | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | has _config => ( | |||||||||||||||||||||||||||
| 24 | is => "ro", | ||||||||||||||||||||||||||||
| 25 | isa => "Config::IniFiles", | ||||||||||||||||||||||||||||
| 26 | handles => { | ||||||||||||||||||||||||||||
| 27 | exists => "exists", | ||||||||||||||||||||||||||||
| 28 | setval => "setval", | ||||||||||||||||||||||||||||
| 29 | newval => "newval", | ||||||||||||||||||||||||||||
| 30 | delval => "delval", | ||||||||||||||||||||||||||||
| 31 | ReadConfig => "ReadConfig", | ||||||||||||||||||||||||||||
| 32 | WriteConfig => "WriteConfig", | ||||||||||||||||||||||||||||
| 33 | RewriteConfig => "RewriteConfig", | ||||||||||||||||||||||||||||
| 34 | GetFileName => "GetFileName", | ||||||||||||||||||||||||||||
| 35 | SetFileName => "SetFileName", | ||||||||||||||||||||||||||||
| 36 | Sections => "Sections", | ||||||||||||||||||||||||||||
| 37 | SectionExists => "SectionExists", | ||||||||||||||||||||||||||||
| 38 | AddSection => "AddSection", | ||||||||||||||||||||||||||||
| 39 | DeleteSection => "DeleteSection", | ||||||||||||||||||||||||||||
| 40 | Parameters => "Parameters", | ||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | has _local_config => ( | |||||||||||||||||||||||||||
| 45 | is => "rw", | ||||||||||||||||||||||||||||
| 46 | isa => "Config::IniFiles", | ||||||||||||||||||||||||||||
| 47 | clearer => "clear_local_config", | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | has _rackobject => ( | |||||||||||||||||||||||||||
| 51 | is => "rw", | ||||||||||||||||||||||||||||
| 52 | isa => "RackMan::Device", | ||||||||||||||||||||||||||||
| 53 | clearer => "clear_rackobject", | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | # BUILDARGS() | ||||||||||||||||||||||||||||
| 59 | # --------- | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=6 SCAR=21.8
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 62 | 1 | my %params; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | if (@_ % 2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 65 | 1 | %params = @_; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | elsif (ref $_[0] eq "HASH") { | ||||||||||||||||||||||||||||
| 68 | 0 | %params = %{$_[0]}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 0 | Rackman->error("invalid argument: ", | |||||||||||||||||||||||||||
| 72 | lc ref $_[0], "ref instead of hashref"); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | RackMan->error("no such file '$params{'-file'}'") | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | unless -f $params{"-file"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | my $config = Config::IniFiles->new(%params) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | or RackMan->error("can't parse config file '$params{'-file'}': ", | ||||||||||||||||||||||||||||
| 80 | @Config::IniFiles::errors); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | RackMan->error("parameter [general]/path is missing") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | unless $config->val(general => "path"); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | return { _config => $config } | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | # val() | ||||||||||||||||||||||||||||
| 91 | # --- | ||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub val { | ||||||||||||||||||||||||||
|
Subroutine
val: called CC=11 SCAR=30.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 4 | my ($self, $section, $parameter, $default) = @_; | |||||||||||||||||||||||||||
| 94 | 4 | my ($value, @values) = (""); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # if no default is given, check if we have one | ||||||||||||||||||||||||||||
| 97 | $default = $defaults{$section}{$parameter} | ||||||||||||||||||||||||||||
| 98 | 4 | ▶ | if not defined $default and exists $defaults{$section}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $default and exists $defaults{$section}
MC/DC0%0/2
not defined $default and exists $defaults{$section}
not defined $default
exists $defaults{$section}
Truth table33%1/3
not(A) and B
A= not defined $defaultB= exists $defaults{$section}
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # propagate context up to Config::IniFiles object | ||||||||||||||||||||||||||||
| 101 | 4 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | @values = $self->_config->val($section, $parameter, $default); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||
| 105 | 4 | $value = $self->_config->val($section, $parameter, $default); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # check if there's a local config for this object to override | ||||||||||||||||||||||||||||
| 109 | # values from the global config | ||||||||||||||||||||||||||||
| 110 | 4 | ▶ | if ($self->_local_config) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | # propagate context up to Config::IniFiles object | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | @values = $self->_local_config->val($section, $parameter, @values); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 0 | $value = $self->_local_config->val($section, $parameter, $value); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # post-process the values | ||||||||||||||||||||||||||||
| 121 | 4 | ▶ | my $name = eval { $self->_rackobject->object_name } || "unknown"; | ||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
$self->_rackobject->object_name
}
} || 'unknown'
MC/DC100%1/1
eval {
do {
$self->_rackobject->object_name
}
} || 'unknown'
eval {
do {
$self->_rackobject->object_name
}
}
Truth table100%2/2
A
A= eval {
do {
$self->_rackobject->object_name
}
}
| |||||||||||||||||||||||||||||
| 122 | 4 | ▶ | if (@values or defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@values or defined $value
MC/DC0%0/2
@values or defined $value
@values
defined $value
Truth table33%1/3
A or B
A= @valuesB= defined $value
| |||||||||||||||||||||||||||||
| 123 | 4 | s/%name%/$name/g for @values, $value; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 4 | ▶ | return wantarray ? @values : $value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||
| 131 | # set_current_rackobject() | ||||||||||||||||||||||||||||
| 132 | # ---------------------- | ||||||||||||||||||||||||||||
| 133 | 1 | ▶ | sub set_current_rackobject { | ||||||||||||||||||||||||||
|
Subroutine
set_current_rackobject: called CC=3 SCAR=13.5
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 1 | my ($self, $rackobject) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # associate the given RackObject to the config | ||||||||||||||||||||||||||||
| 137 | 1 | $self->_rackobject($rackobject); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # see if there's a config specific to this device | ||||||||||||||||||||||||||||
| 140 | # (we assume that [general]/path contains a %name% paramter | ||||||||||||||||||||||||||||
| 141 | # that will now be correctly interpolated) | ||||||||||||||||||||||||||||
| 142 | 1 | my $path = $self->val(general => "path"); | |||||||||||||||||||||||||||
| 143 | 1 | my $local_cfg_path = catfile($path, LOCAL_CONFIG_FILE); | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | return unless -f $local_cfg_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # if that's the case, parse it and associate it to the config | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my $local_config = Config::IniFiles->new(-file => $local_cfg_path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | or RackMan->error("can't parse config file '$local_cfg_path': ", | ||||||||||||||||||||||||||||
| 149 | @Config::IniFiles::errors); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | $self->_local_config($local_config); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # | ||||||||||||||||||||||||||||
| 156 | # clear_current_rackobject() | ||||||||||||||||||||||||||||
| 157 | # ------------------------ | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub clear_current_rackobject { | ||||||||||||||||||||||||||
|
Subroutine
clear_current_rackobject: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 160 | 0 | $self->clear_rackobject; | |||||||||||||||||||||||||||
| 161 | 0 | $self->clear_local_config; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | __PACKAGE__->meta->make_immutable | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||