| 1 | package DBIx::Class::DeploymentHandler::CLI::ConfigReader; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Config::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use File::HomeDir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Types::Standard qw/ArrayRef HashRef Maybe/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Types::Common::String qw/NonEmptyStr/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | =head1 NAME | ||||||||||
| 15 | |||||||||||
| 16 | DBIx::Class::DeploymentHandler::CLI::ConfigReader - Config reader class for Deployment Handler CLI. | ||||||||||
| 17 | |||||||||||
| 18 | =head1 Description | ||||||||||
| 19 | |||||||||||
| 20 | Searches the configuration file in the following locations: | ||||||||||
| 21 | |||||||||||
| 22 | =over 4 | ||||||||||
| 23 | |||||||||||
| 24 | =item C<$ENV{DBIX_CONFIG_DIR}> | ||||||||||
| 25 | |||||||||||
| 26 | =item current directory | ||||||||||
| 27 | |||||||||||
| 28 | =item home directory | ||||||||||
| 29 | |||||||||||
| 30 | =item C</etc> directory | ||||||||||
| 31 | |||||||||||
| 32 | =back | ||||||||||
| 33 | |||||||||||
| 34 | The config accessor will return undefined if a configuration file is absent in all these locations. | ||||||||||
| 35 | |||||||||||
| 36 | =head1 Attributes | ||||||||||
| 37 | |||||||||||
| 38 | =head2 config | ||||||||||
| 39 | |||||||||||
| 40 | Configuration values as a hash reference. Generated by builder. | ||||||||||
| 41 | |||||||||||
| 42 | =head2 config_name | ||||||||||
| 43 | |||||||||||
| 44 | Name for the configuration file without the suffix. Defaults to C<dh-cli>. | ||||||||||
| 45 | |||||||||||
| 46 | =head2 config_files | ||||||||||
| 47 | |||||||||||
| 48 | List of configuration files. | ||||||||||
| 49 | |||||||||||
| 50 | =head2 config_paths | ||||||||||
| 51 | |||||||||||
| 52 | List of configuration paths. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 2 | has config => ( | |||||||||
| 57 | is => 'ro', | ||||||||||
| 58 | isa => Maybe[HashRef], | ||||||||||
| 59 | lazy => 1, | ||||||||||
| 60 | builder => '_build_config', | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | 2 | has config_name => ( | |||||||||
| 64 | is => 'ro', | ||||||||||
| 65 | isa => NonEmptyStr, | ||||||||||
| 66 | default => 'dh-cli', | ||||||||||
| 67 | ); | ||||||||||
| 68 | |||||||||||
| 69 | has config_files => ( | ||||||||||
| 70 | is => 'ro', | ||||||||||
| 71 | isa => ArrayRef, | ||||||||||
| 72 | 5 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 2 | ); | |||||||||
| 74 | |||||||||||
| 75 | 2 | has config_paths => ( | |||||||||
| 76 | is => 'ro', | ||||||||||
| 77 | isa => ArrayRef, | ||||||||||
| 78 | lazy => 1, | ||||||||||
| 79 | builder => '_builder_config_paths', | ||||||||||
| 80 | ); | ||||||||||
| 81 | |||||||||||
| 82 | 3 | ▶ | sub _build_config { | ||||||||
|
Subroutine
_build_config: called CC=4 SCAR=13.9
| |||||||||||
| 83 | 3 | my ($self) = @_; | |||||||||
| 84 | |||||||||||
| 85 | # If we have ->config_files, we'll use those and load_files | ||||||||||
| 86 | # instead of the default load_stems. | ||||||||||
| 87 | 3 | my %cf_opts = ( use_ext => 1 ); | |||||||||
| 88 | 3 | ▶ | my $configs = @{$self->config_files} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | ? Config::Any->load_files({ files => $self->config_files, %cf_opts }) | ||||||||||
| 90 | : Config::Any->load_stems({ stems => $self->config_paths, %cf_opts }); | ||||||||||
| 91 | |||||||||||
| 92 | # return configuration from first existing configuration file | ||||||||||
| 93 | 3 | for my $cf (@$configs) { | |||||||||
| 94 | 2 | for my $filename ( keys %$cf ) { | |||||||||
| 95 | 2 | return $cf->{$filename}; | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 1 | return undef; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 5 | ▶ | sub _builder_config_paths { | ||||||||
|
Subroutine
_builder_config_paths: called CC=1 SCAR=0.0
| |||||||||||
| 103 | 5 | my ($self) = @_; | |||||||||
| 104 | 5 | my $config_name = $self->config_name; | |||||||||
| 105 | |||||||||||
| 106 | return [ | ||||||||||
| 107 | 5 | $self->get_env_vars, | |||||||||
| 108 | ".$config_name", | ||||||||||
| 109 | File::HomeDir->my_home . '/.' . $config_name, | ||||||||||
| 110 | "/etc/$config_name", | ||||||||||
| 111 | ]; | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | =head1 Methods | ||||||||||
| 115 | |||||||||||
| 116 | =head2 get_env_vars | ||||||||||
| 117 | |||||||||||
| 118 | Returns stem for environment variable C<DBIX_CONFIG_DIR>. | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | 5 | ▶ | sub get_env_vars { | ||||||||
|
Subroutine
get_env_vars: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 123 | 5 | my ($self) = @_; | |||||||||
| 124 | |||||||||||
| 125 | 5 | ▶ | return $ENV{DBIX_CONFIG_DIR} . "/" . $self->config_name if exists $ENV{DBIX_CONFIG_DIR}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 2 | return (); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 2 | 1; | |||||||||