blib/lib/HPC/Runner/Command/Utils/ManyConfigs.pm

filter: Statementstmt 95.6% 88 / 92 Branchbran 66.6% 16 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 6 totaltotal 86.2% 125 / 145 CC 15 SCAR 10.1
CC
15
Cov
90%
apply_attributes
5.1
search_configs
5.0
apply_configs
4.0
CRAP 2.8
↑ Summary Build »
1 package HPC::Runner::Command::Utils::ManyConfigs;
2  
3 2 our $VERSION = '0.02';
4  
5 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use MooseX::App::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use MooseX::Types::Path::Tiny qw/Path Paths AbsPaths AbsFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 option 'no_configs' => (
20 is => 'rw',
21 isa => 'Bool',
22 default => 0,
23 documentation => '--no_configs tells HPC::Runner not to load any configs',
24 );
25  
26 2 option 'config' => (
27 isa => 'Str',
28 is => 'rw',
29 required => 0,
30 documentation => 'Override the search paths and supply your own config.',
31 isa => AbsFile,
32 coerce => 1,
33 predicate => 'has_config',
34 );
35  
36 2 option 'config_base' => (
37 is => 'rw',
38 isa => 'Str',
39 default => '.config',
40 documentation => 'Basename of config files',
41 );
42  
43 2 option 'search' => (
44 traits => ['Bool'],
45 is => 'rw',
46 isa => 'Bool',
47 default => 1,
48 handles => {
49 'no_search' => 'unset',
50 },
51 documentation =>
52 'Search for config files in ~/.config.(ext) and in your current working directory.'
53 );
54  
55 option 'search_path' => (
56 traits => ['Array'],
57 is => 'rw',
58 isa => 'ArrayRef[Str]',
59 0 ▶ default => sub { [ File::HomeDir->my_home, getcwd() ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 2 handles => {
61 all_search_path => 'elements',
62 append_search_path => 'push',
63 prepend_search_path => 'unshift',
64 map_search_path => 'map',
65 has_search_path => 'count',
66 },
67 documentation => 'Enable a search path for configs. Default is the home dir and your cwd.'
68 );
69  
70 has 'filter_keys' => (
71 is => 'rw',
72 traits => ['Array'],
73 is => 'rw',
74 isa => 'ArrayRef[Str]',
75 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 2 handles => {
77 all_filter_keys => 'elements',
78 append_filter_keys => 'push',
79 prepend_filter_keys => 'unshift',
80 map_filter_keys => 'map',
81 has_filter_keys => 'count',
82 },
83 );
84  
85 has 'config_files' => (
86 traits => ['Array'],
87 is => 'rw',
88 isa => AbsPaths,
89 coerce => 1,
90 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 2 handles => {
92 all_config_files => 'elements',
93 append_config_files => 'push',
94 prepend_config_files => 'unshift',
95 map_config_files => 'map',
96 has_config_files => 'count',
97 count_config_files => 'count',
98 },
99 );
100  
101 has '_config_data' => (
102 is => 'rw',
103 isa => 'ArrayRef',
104 predicate => 'has_config_data',
105 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
106 2 );
107  
108 ▶sub BUILD { }
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
109  
110 after 'BUILD' => sub {
111 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
112  
113 1 ▶ return if $self->no_configs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_configs 0 1 1
114 1 $self->decide_search_configs;
115 2 };
116  
117 sub decide_search_configs {
118 1 ▶ my $self = shift;
Subroutine decide_search_configs: called CC=3 SCAR=13.3
Pod undocumented
119  
120 1 ▶ if ( $self->has_config ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_config) { } 0 1 1
elsif ($self->search) { } 1 0 1
121 0 $self->append_config_files( $self->config );
122 0 $self->load_configs;
123 }
124 elsif ( $self->search ) {
125 1 $self->search_configs;
126 }
127 }
128  
129 sub search_configs {
130 1 ▶ my $self = shift;
Subroutine search_configs: called CC=5 SCAR=16.1
Pod undocumented
131 1 my $extension = shift;
132  
133 1 foreach my $extension ( Config::Any->extensions ) {
134 10 foreach my $dir ( $self->all_search_path ) {
135 10 my $dir = $dir . '/';
136  
137 #We will check with and without extensions
138 10 my $check_file;
139 10 $check_file = File::Spec->catfile( $dir . $self->config_base );
140 10 ▶ $self->append_config_files($check_file) if -f $check_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $check_file 0 10 10
141  
142 10 $check_file =
143 File::Spec->catfile(
144 $dir . $self->config_base . '.' . $extension );
145 10 ▶ $self->append_config_files($check_file) if -f $check_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $check_file 1 9 10
146 }
147 }
148  
149 1 $self->load_configs;
150  
151 }
152  
153 sub load_configs {
154 1 ▶ my $self = shift;
Subroutine load_configs: called CC=1 SCAR=0.0
Pod undocumented
155  
156 1 my $command_name = $self->{_original_class_name};
157 1 my $cfg =
158 Config::Any->load_files( { files => $self->config_files, use_ext => 1 } );
159  
160 1 $self->_config_data($cfg);
161  
162 1 $self->apply_configs;
163 }
164  
165 sub apply_configs {
166 1 ▶ my $self = shift;
Subroutine apply_configs: called CC=4 SCAR=13.9
Pod undocumented
167  
168 1 my $command_name = $self->{_original_class_name};
169 1 ▶ for ( my $x = 0 ; $x < $self->count_config_files ; $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->count_config_files) 1 1 2
170 1 my $c = $self->_config_data->[$x]->{ $self->config_files->[$x] };
171 1 my @keys = keys %{$c};
172  
173 1 ▶ if ( !$self->has_filter_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_filter_keys) 1 0 1
174 1 my @filter_keys = grep { $_ ne 'global' } @keys;
175 1 $self->filter_keys( \@filter_keys );
176 }
177  
178 #Global is always applied
179 1 ▶ $self->apply_attributes( $c, 'global' ) if exists $c->{global};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{"global"} 1 0 1
180  
181 1 map { $self->apply_attributes( $c, $_ ) } $self->all_filter_keys;
182 }
183  
184 }
185  
186 sub apply_attributes {
187 2 ▶ my $self = shift;
Subroutine apply_attributes: called CC=5 SCAR=16.3
Pod undocumented
188 2 my $conf = shift;
189 2 my $attr = shift;
190  
191 2 ▶ return unless ref($conf) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf eq "HASH" 0 2 2
192 2 ▶ return unless ref( $conf->{$attr} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf->{$attr} eq "HASH" 0 2 2
193  
194 2 ▶ while ( my ( $key, $value ) = each %{ $conf->{$attr} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$conf{$attr};}) 2 2 4
195  
196 2 ▶ if ( $self->can($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($key)) 1 1 2
197 try {
198 1 ▶ $self->$key($value);
Subroutine __ANON__: called CC=1 SCAR=0.0
199 }
200 catch {
201 0 ▶ warn 'You tried to assign ' . $key . ' to ' . $value . "\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 #TODO Add logging
203 1 };
204 }
205 }
206 }
207  
208 2 1;
209  
210 __END__
211  
212 =encoding utf-8
213  
214 =head1 NAME
215  
216 HPC::Runner::Command::Utils::ManyConfigs - Load many layered configs
217  
218 =head1 SYNOPSIS
219  
220 use Moose;
221 with 'HPC::Runner::Command::Utils::ManyConfigs';
222  
223 #Specify search_paths with --search_path
224 #Specify config base with --config_base
225  
226  
227 =head1 DESCRIPTION
228  
229 HPC::Runner::Command::Utils::ManyConfigs are just some helper utilities to make it easier to layer config files
230  
231 It is in the HPC::Runner::Command namespace, but it is meant to be portable for any MooseX::App.
232  
233 =head1 AUTHOR
234  
235 Jillian Rowe E<lt>jillian.e.rowe@gmail.comE<gt>
236  
237 =head1 COPYRIGHT
238  
239 Copyright 2017- Jillian Rowe
240  
241 =head1 LICENSE
242  
243 This library is free software; you can redistribute it and/or modify
244 it under the same terms as Perl itself.
245  
246 =head1 SEE ALSO
247  
248 =cut
↑ Summary Build »