blib/lib/Clustericious/Config.pm

filter: Statementstmt 96.9% 161 / 166 Branchbran 83.3% 50 / 60 Conditioncond 57.1% 4 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 93.5% 29 / 31 Podpod 100% 1 / 1 totaltotal 91.8% 247 / 269 CC 34 SCAR 21.8
CC
34
Cov
92%
new
21.2
__ANON__
11.0
execute
6.0
CRAP 8.8
« blib/lib/Clustericious/Command/generate/app.pm ↑ Summary blib/lib/Clustericious/Client/Object.pm »
1 package Clustericious::Config;
2  
3 99 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 80 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 73 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 73 ▶use Clustericious;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 73 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 72 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 72 ▶use Log::Log4perl qw( :nowarn );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 72 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 72 ▶use Clustericious::Config::Helpers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 72 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 72 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 72 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 72 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # ABSTRACT: Configuration files for Clustericious nodes.
19 72 our $VERSION = '1.29'; # VERSION
20  
21  
22 72 our %singletons;
23  
24 72 our $class_suffix = {};
25 27 ▶sub Clustericious::_config_uncache {
Subroutine _config_uncache: called CC=2 SCAR=6.9
26 27 my($class, $name) = @_;
27 27 delete $singletons{$name};
28 27 ▶ $class_suffix->{$name} //= 1;
Condition100%2/2
$Clustericious::Config::class_suffix->{$name} //= 1
l!l
126
MC/DC100%1/1
$Clustericious::Config::class_suffix->{$name} //= 1
$Clustericious::Config::class_suffix->{$name}
Truth table100%2/2
A
A= $Clustericious::Config::class_suffix->{$name}
Aresult
11
00
29 27 $class_suffix->{$name}++;
30 }
31  
32  
33 150 ▶sub new {
Subroutine new: called CC=21 SCAR=30.5
Pod documented
34 150 my $class = shift;
35  
36 150 my $logger = Log::Log4perl->get_logger(__PACKAGE__);
37  
38 # (undocumented; for now)
39 # callback is used by the configdebug command;
40 # may be used elsewise at a later time
41 150 ▶ my $callback = ref $_[-1] eq 'CODE' ? pop : sub {};
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 150 150
42  
43 150 ▶ my %t_args = (ref $_[-1] eq 'ARRAY' ? @{( pop )} : () );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'ARRAY' ? : 11 139 150
44  
45 150 my $arg = $_[0];
46 150 ▶ ($arg = caller) =~ s/:.*$// unless $arg; # Determine from caller's class
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 150 150
47 150 ▶ return $singletons{$arg} if exists($singletons{$arg});
Branch: 1 branch
BranchTrueFalseTotal
if exists $Clustericious::Config::singletons{$arg} 0 150 150
48  
49 150 my $conf_data;
50  
51 150 state $package_counter = 0;
52 150 my $namespace = "Clustericious::Config::TemplatePackage::Package$package_counter";
53 150 ▶ eval qq{ package $namespace; use Clustericious::Config::Helpers; };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
54 150 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 150 150
55 150 $package_counter++;
56
57 150 my $mt = Mojo::Template->new(namespace => $namespace)->auto_escape(0);
58 150 $mt->prepend( join "\n", map " my \$$_ = q{$t_args{$_}};", sort keys %t_args );
59  
60 150 ▶ if(ref $arg eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 62 88 150
61 {
62 62 $conf_data = Storable::dclone $arg;
63 }
64 else
65 {
66 88 my $filename;
67
68 88 ▶ if($arg =~ /\.conf$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\.conf$/) { } 1 87 88
69 {
70 1 $filename = $arg;
71 }
72 else
73 {
74 87 my $name = $arg;
75 87 $name =~ s/::/-/g;
76 ($filename) =
77 94 ▶ List::Util::first { -f $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
78 87 map { File::Spec->catfile($_, "$name.conf") }
79 Clustericious->_config_path;
80
81 87 ▶ unless($filename)
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 20 67 87
82 {
83 20 ▶ $logger->trace("could not find $name file.") if $logger->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 6 14 20
84 20 $conf_data = {};
85 }
86 }
87  
88 88 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 68 20 88
89 68 ▶ $logger->trace("reading from config file $filename") if $logger->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 21 47 68
90 68 $callback->(pre_rendered => $filename);
91 68 my $rendered = $mt->render_file($filename);
92 68 $callback->(rendered => $filename => $rendered);
93  
94 68 ▶ die $rendered if ( (ref $rendered) =~ /Exception/ );
Branch: 1 branch
BranchTrueFalseTotal
if ref($rendered) =~ /Exception/ 0 68 68
95  
96 68 $conf_data = eval { YAML::XS::Load($rendered) };
97 68 ▶ $logger->logdie("Could not parse\n-------\n$rendered\n---------\n$@\n") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 68 68
98 } else {
99 20 $callback->('not_found' => "$arg");
100 }
101 }
102  
103 150 ▶ $conf_data ||= {};
Condition100%2/2
$conf_data ||= {}
l!l
1419
MC/DC100%1/1
$conf_data ||= {}
$conf_data
Truth table100%2/2
A
A= $conf_data
Aresult
11
00
104 150 Clustericious::Config::Helpers->_do_merges($conf_data);
105  
106 # Use derived classes so that AUTOLOADING keeps namespaces separate
107 # for various apps.
108 150 ▶ if ($class eq __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 90 60 150
109 {
110 90 ▶ if(ref $arg)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg) { } 2 88 90
elsif ($arg =~ s/\.conf$//) { } 1 87 88
111 {
112 2 $arg = "$arg";
113 2 $arg =~ tr/a-zA-Z0-9//cd;
114 }
115 elsif($arg =~ s/\.conf$//)
116 {
117 # NOTE: may revisit this later.
118 1 ▶ $arg = "cwd::$arg" unless $arg =~ s{^/+}{root::};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ s[^/+][root::] 0 1 1
119 1 $arg =~ s{[\\/]}{::}g;
120 1 $arg =~ s{\.\.::}{__up__::}g;
121 1 $arg =~ tr/a-zA-Z0-0_://cd;
122 1 $arg =~ s/:{3,}/::/g;
123 }
124 90 $arg =~ s/-/::/g;
125 90 $class = join '::', $class, 'App', $arg;
126 90 ▶ $class .= $class_suffix->{$arg} if $class_suffix->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if $Clustericious::Config::class_suffix->{$arg} 28 62 90
127 90 my $dome = '# line '. __LINE__ . ' "' . __FILE__ . qq("\n) . '@'."$class"."::ISA = ('".__PACKAGE__. "')";
128 90 eval $dome;
129 90 ▶ die "error setting ISA : $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 90 90
130 }
131 150 bless $conf_data, $class;
132 }
133  
134 # defined so that AUTOLOAD doesn't get called
135 # when config falls out of scope.
136 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
137 }
138  
139 304 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
140 304 my($self, %args) = @_;
141
142 # NOTE: I hope to deprecated and later remove defining defaults in this way in the near future.
143 304 my $default = $args{default};
144 304 my $default_exists = exists $args{default};
145  
146 304 our $AUTOLOAD;
147 304 my $called = $AUTOLOAD;
148 304 $called =~ s/.*:://g;
149  
150 304 my $value = $self->{$called};
151 304 my $invocant = ref $self;
152 304 ▶ my $obj = ref $value eq 'HASH' ? $invocant->new($value) : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 19 285 304
153  
154 417 ▶ my $sub = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
155 417 my $self = shift;
156 417 my $value;
157
158 417 ▶ if(exists $self->{$called})
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{$called}) { } 213 204 417
elsif ($default_exists) { } 201 3 204
159 {
160 213 $value = $self->{$called};
161 }
162 elsif($default_exists)
163 {
164 201 ▶ $value = $self->{$called} = ref $default eq 'CODE' ? $default->() : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 41 160 201
165 201 ▶ $obj = ref $value eq 'HASH' ? $invocant->new($value) : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 41 160 201
166 }
167 else
168 {
169 3 Carp::croak "'$called' configuration item not found. Values present: @{[keys %$self]}";
170 }
171  
172 414 my $ref = ref $value;
173 414 ▶ if($ref)
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 233 181 414
174 {
175 233 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 143 90 233
176 {
177 143 ▶ return %$value if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 3 140 143
178 140 ▶ return @$value if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 140 0 140
179 }
180 90 ▶ return $obj if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 63 27 90
181 27 ▶ $value = $value->execute if eval { $value->can('execute') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $value->can("execute") } } 0 27 27
182 }
183 208 $value;
184 304 };
185 72 ▶ do { no strict 'refs'; *{ $invocant . "::$called" } = $sub };
Subroutine BEGIN: called CC=1 SCAR=0.0
186 304 $sub->($self);
187 }
188  
189  
190 package Clustericious::Config::Callback;
191  
192 72 ▶use JSON::MaybeXS qw( encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 sub new
195 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
196 4 my($class, @args) = @_;
197 4 bless [@args], $class;
198 }
199  
200 1 ▶sub args { @{ shift() } }
Subroutine args: called CC=1 SCAR=0.0
201  
202 1 ▶sub execute { '' }
Subroutine execute: called CC=1 SCAR=0.0
203  
204 sub to_yaml
205 6 ▶{
Subroutine to_yaml: called CC=1 SCAR=0.0
206 6 my($self) = @_;
207 6 "!!perl/array:@{[ ref $self ]} @{[ encode_json [@$self] ]}";
208 }
209  
210 package Clustericious::Config::Callback::Password;
211  
212 72 ▶use base qw( Clustericious::Config::Callback );
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 sub execute
215 0 ▶{
Subroutine execute: not called CC=2 SCAR=17.9
216 0 state $pass;
217 0 ▶ $pass //= do { require Term::Prompt; Term::Prompt::prompt('p', 'Password:', '', '') };
Condition0%0/3
$pass //= do { require Term::Prompt; Term::Prompt::prompt("p", "Password:", "", "") }
l!l&&r!l&&!r
000
MC/DC0%0/2
$pass //= do { require Term::Prompt; Term::Prompt::prompt("p", "Password:", "", "") }
$pass do { require Term::Prompt; Term::Prompt::prompt("p", "Password:", "", "") }
Truth table0%0/3
A //= B
A= $passB= do { require Term::Prompt; Term::Prompt::prompt("p", "Password:", "", "") }
ABresult
1X1
011
000
218 0 $pass;
219 }
220  
221 72 1;
222  
223 __END__
« blib/lib/Clustericious/Command/generate/app.pm ↑ Summary blib/lib/Clustericious/Client/Object.pm »