| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Config::Station; | 
| 2 |  |  |  |  |  |  | $Config::Station::VERSION = '0.001000'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Load configs from files and the environment | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 45641 | use Moo; | 
|  | 1 |  |  |  |  | 15271 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 6 | 1 |  |  | 1 |  | 1542 | use warnings NONFATAL => 'all'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 651 | use JSON::MaybeXS; | 
|  | 1 |  |  |  |  | 3358245 |  | 
|  | 1 |  |  |  |  | 75 |  | 
| 9 | 1 |  |  | 1 |  | 903 | use IO::All; | 
|  | 1 |  |  |  |  | 13820 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 10 | 1 |  |  | 1 |  | 1049 | use Try::Tiny; | 
|  | 1 |  |  |  |  | 1627 |  | 
|  | 1 |  |  |  |  | 855 |  | 
| 11 | 1 |  |  | 1 |  | 36 | use Module::Runtime 'use_module'; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | has _debug => ( | 
| 14 |  |  |  |  |  |  | is => 'ro', | 
| 15 |  |  |  |  |  |  | init_arg => undef, | 
| 16 |  |  |  |  |  |  | lazy => 1, | 
| 17 |  |  |  |  |  |  | default => sub { | 
| 18 |  |  |  |  |  |  | my $self = shift; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | exists $ENV{'DEBUG_' . $self->_env_key} | 
| 21 |  |  |  |  |  |  | ? $ENV{'DEBUG_' . $self->_env_key} | 
| 22 |  |  |  |  |  |  | : $self->__debug | 
| 23 |  |  |  |  |  |  | }, | 
| 24 |  |  |  |  |  |  | ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | has __debug => ( | 
| 27 |  |  |  |  |  |  | is => 'ro', | 
| 28 |  |  |  |  |  |  | init_arg => 'debug', | 
| 29 |  |  |  |  |  |  | ); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | has _env_key => ( | 
| 32 |  |  |  |  |  |  | is => 'ro', | 
| 33 |  |  |  |  |  |  | init_arg => 'env_key', | 
| 34 |  |  |  |  |  |  | required => 1, | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | has _location => ( | 
| 38 |  |  |  |  |  |  | is => 'ro', | 
| 39 |  |  |  |  |  |  | init_arg => undef, | 
| 40 |  |  |  |  |  |  | lazy => 1, | 
| 41 |  |  |  |  |  |  | default => sub { | 
| 42 |  |  |  |  |  |  | my $self = shift; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | my $path = $ENV{'FILE_' . $self->_env_key} || | 
| 45 |  |  |  |  |  |  | $self->__location; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | warn "No path specified to load config from\n" | 
| 48 |  |  |  |  |  |  | if !$path && $self->_debug; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | return $path | 
| 51 |  |  |  |  |  |  | }, | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | has __location => ( | 
| 55 |  |  |  |  |  |  | is => 'ro', | 
| 56 |  |  |  |  |  |  | init_arg => 'location', | 
| 57 |  |  |  |  |  |  | ); | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | has _config_class => ( | 
| 60 |  |  |  |  |  |  | is => 'ro', | 
| 61 |  |  |  |  |  |  | init_arg => 'config_class', | 
| 62 |  |  |  |  |  |  | required => 1, | 
| 63 |  |  |  |  |  |  | ); | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 7 |  |  | 7 |  | 26 | sub _io { io->file(shift->_location) } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub _debug_log { | 
| 68 | 9 |  |  | 9 |  | 1333 | my ($self, $line, $ret) = @_; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 9 | 50 |  |  |  | 170 | if ($self->_debug) { | 
| 71 | 0 | 0 |  |  |  | 0 | if (my @keys = keys %$ret) { | 
| 72 | 0 |  |  |  |  | 0 | warn "CONFIGSTATION FROM $line:\n"; | 
| 73 | 0 |  |  |  |  | 0 | warn "  $_: $ret->{$_}\n" for @keys; | 
| 74 |  |  |  |  |  |  | } else { | 
| 75 | 0 |  |  |  |  | 0 | warn "CONFIGSTATION FROM $line: EMPTY\n"; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | $ret | 
| 80 | 9 |  |  |  |  | 161 | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub _read_config_from_file { | 
| 83 | 6 |  |  | 6 |  | 9 | my $self = shift; | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | my $ret = try { | 
| 86 | 6 |  |  | 6 |  | 187 | $self->_debug_log(FILE => decode_json($self->_io->all)); | 
| 87 |  |  |  |  |  |  | } catch { | 
| 88 | 3 | 50 |  | 3 |  | 11816 | if ($self->_debug) { | 
| 89 | 0 |  |  |  |  | 0 | warn "CONFIGSTATION FROM FILE: $_\n" | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | {} | 
| 92 | 6 |  |  |  |  | 48 | }; | 
|  | 3 |  |  |  |  | 28 |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub _read_config_from_env { | 
| 97 | 6 |  |  | 6 |  | 8 | my $self = shift; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 6 |  |  |  |  | 16 | my $k_re = '^' . quotemeta($self->_env_key) . '_(.+)'; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | my $ret = +{ | 
| 102 | 6 |  |  |  |  | 86 | map {; m/$k_re/; lc $1 => $ENV{$self->_env_key . "_$1"} } | 
|  | 7 |  |  |  |  | 27 |  | 
|  | 7 |  |  |  |  | 38 |  | 
| 103 |  |  |  |  |  |  | grep m/$k_re/, | 
| 104 |  |  |  |  |  |  | keys %ENV | 
| 105 |  |  |  |  |  |  | }; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 6 |  |  |  |  | 23 | $self->_debug_log(ENV => $ret); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub _read_config { | 
| 111 | 6 |  |  | 6 |  | 169 | my $self = shift; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | { | 
| 114 | 6 |  |  |  |  | 6 | %{$self->_read_config_from_file}, | 
|  | 6 |  |  |  |  | 17 |  | 
| 115 | 6 |  |  |  |  | 7 | %{$self->_read_config_from_env}, | 
|  | 6 |  |  |  |  | 86 |  | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub load { | 
| 120 | 6 |  |  | 6 | 0 | 8669 | my $self = shift; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 6 |  |  |  |  | 29 | use_module($self->_config_class)->new($self->_read_config) | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # eat my data | 
| 126 |  |  |  |  |  |  | sub store { | 
| 127 | 1 |  |  | 1 | 0 | 1040 | my ($self, $obj) = @_; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 1 |  |  |  |  | 3 | $self->_io->print(encode_json($obj->serialize)) | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | 1; | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | __END__ |