| 1 | package Enbld::App::Configuration; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | our $envname = 'myenv'; | |||||||||
| 11 | |||||||||||
| 12 | 3 | our $config_ref; | |||||||||
| 13 | 3 | our $rcfile_ref; | |||||||||
| 14 | |||||||||||
| 15 | 3 | our $CONFIGURATIONFILE; | |||||||||
| 16 | |||||||||||
| 17 | 3 | our $dirty; | |||||||||
| 18 | |||||||||||
| 19 | sub is_dirty { | ||||||||||
| 20 | 3 | ▶ | return $dirty; | ||||||||
|
Subroutine
is_dirty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub config { | ||||||||||
| 24 | 1 | ▶ | return $config_ref; | ||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | sub rcfile { | ||||||||||
| 28 | 1 | ▶ | return $rcfile_ref; | ||||||||
|
Subroutine
rcfile: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | # method for only test. | ||||||||||
| 32 | sub destroy { | ||||||||||
| 33 | 37 | ▶ | undef $CONFIGURATIONFILE; | ||||||||
|
Subroutine
destroy: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 34 | |||||||||||
| 35 | 37 | undef $envname; | |||||||||
| 36 | 37 | undef $dirty; | |||||||||
| 37 | |||||||||||
| 38 | 37 | undef $config_ref; | |||||||||
| 39 | 37 | undef $rcfile_ref; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | sub _configuration_file_path { | ||||||||||
| 43 | 6 | ▶ | require Enbld::Home; | ||||||||
|
Subroutine
_configuration_file_path: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 6 | return File::Spec->catfile( Enbld::Home->conf, 'enbld.conf' ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | sub read_file { | ||||||||||
| 49 | 4 | ▶ | my $pkg = shift; | ||||||||
|
Subroutine
read_file: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 50 | |||||||||||
| 51 | 4 | my $path = _configuration_file_path(); | |||||||||
| 52 | |||||||||||
| 53 | 4 | ▶ | return if ( ! -e $path ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 2 | open my $fh, '<', $path; | |||||||||
| 56 | 2 | my $str = do { local $/; <$fh> }; | |||||||||
| 57 | 2 | close $fh; | |||||||||
| 58 | |||||||||||
| 59 | 2 | eval $str; | |||||||||
| 60 | |||||||||||
| 61 | 2 | $envname = $CONFIGURATIONFILE->{envname}; | |||||||||
| 62 | |||||||||||
| 63 | 2 | require Enbld::Config; | |||||||||
| 64 | 2 | foreach my $name ( keys %{ $CONFIGURATIONFILE->{config} } ) { | |||||||||
| 65 | $config_ref->{$name} = | ||||||||||
| 66 | 1 | Enbld::Config->new( %{ $CONFIGURATIONFILE->{config}{$name} } ); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 2 | require Enbld::RcFile; | |||||||||
| 70 | 2 | foreach my $filename ( keys %{ $CONFIGURATIONFILE->{rcfile} } ) { | |||||||||
| 71 | $rcfile_ref->{$filename} = | ||||||||||
| 72 | 1 | Enbld::RcFile->new( %{ $CONFIGURATIONFILE->{rcfile}{$filename} }); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 2 | return $envname; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub write_file { | ||||||||||
| 79 | |||||||||||
| 80 | 4 | ▶ | require Enbld::Feature; | ||||||||
|
Subroutine
write_file: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 81 | 4 | ▶ | return if Enbld::Feature->is_deploy_mode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | 3 | ▶ | return unless is_dirty(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 2 | $CONFIGURATIONFILE = { | |||||||||
| 86 | envname => $envname, | ||||||||||
| 87 | config => {}, | ||||||||||
| 88 | rcfile => {}, | ||||||||||
| 89 | format => 'v2.0', # for future...configuration format version. | ||||||||||
| 90 | }; | ||||||||||
| 91 | |||||||||||
| 92 | 2 | foreach my $name ( keys %{ $config_ref } ) { | |||||||||
| 93 | 1 | $CONFIGURATIONFILE->{config}{$name} = $config_ref->{$name}->serialize; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 2 | foreach my $filename ( keys %{ $rcfile_ref } ) { | |||||||||
| 97 | $CONFIGURATIONFILE->{rcfile}{$filename} = | ||||||||||
| 98 | 1 | $rcfile_ref->{$filename}->serialize; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 2 | my $path = _configuration_file_path(); | |||||||||
| 102 | |||||||||||
| 103 | 2 | require Data::Dumper; | |||||||||
| 104 | 2 | open my $fh, '>', $path; | |||||||||
| 105 | 2 | my $dump = Data::Dumper->new( | |||||||||
| 106 | [ $CONFIGURATIONFILE ], | ||||||||||
| 107 | [ 'Enbld::App::Configuration::CONFIGURATIONFILE' ] | ||||||||||
| 108 | ); | ||||||||||
| 109 | 2 | print $fh $dump->Dump; | |||||||||
| 110 | 2 | close $fh; | |||||||||
| 111 | |||||||||||
| 112 | 2 | return $envname; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | sub set_config { | ||||||||||
| 116 | 7 | ▶ | my ( $pkg, $config ) = @_; | ||||||||
|
Subroutine
set_config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 117 | |||||||||||
| 118 | 7 | my $name = $config->name; | |||||||||
| 119 | 7 | $config_ref->{$name} = $config; | |||||||||
| 120 | |||||||||||
| 121 | 7 | $dirty++; | |||||||||
| 122 | |||||||||||
| 123 | 7 | return $config->name; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | sub set_rcfile { | ||||||||||
| 127 | 2 | ▶ | my ( $pkg, $rcfile ) = @_; | ||||||||
|
Subroutine
set_rcfile: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 128 | |||||||||||
| 129 | 2 | my $filename = $rcfile->filename; | |||||||||
| 130 | 2 | $rcfile_ref->{$filename} = $rcfile; | |||||||||
| 131 | |||||||||||
| 132 | 2 | $dirty++; | |||||||||
| 133 | |||||||||||
| 134 | 2 | return $rcfile->filename; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | sub search_config { | ||||||||||
| 138 | 9 | ▶ | my ( $pkg, $name ) = @_; | ||||||||
|
Subroutine
search_config: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 139 | |||||||||||
| 140 | 9 | ▶ | return $config_ref->{$name} if ( exists $config_ref->{$name} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 3 | return; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | sub search_rcfile { | ||||||||||
| 145 | 3 | ▶ | my ( $pkg, $filename ) = @_; | ||||||||
|
Subroutine
search_rcfile: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 146 | |||||||||||
| 147 | 3 | ▶ | return $rcfile_ref->{$filename} if ( exists $rcfile_ref->{$filename} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 1 | return; | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | sub envname { | ||||||||||
| 152 | 1 | ▶ | return $envname; | ||||||||
|
Subroutine
envname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | sub set_envname { | ||||||||||
| 156 | 1 | ▶ | my ( $pkg, $name ) = @_; | ||||||||
|
Subroutine
set_envname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 157 | |||||||||||
| 158 | 1 | $envname = $name; | |||||||||
| 159 | |||||||||||
| 160 | 1 | return $envname; | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 3 | 1; | |||||||||
| 164 | |||||||||||