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