| 1 | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | ||||||||||||||||||||||||||||
| 2 | package Env::Dot::Functions; | ||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 9 | get_dotenv_vars | ||||||||||||||||||||||||||||
| 10 | interpret_dotenv_filepath_var | ||||||||||||||||||||||||||||
| 11 | get_envdot_filepaths_var_name | ||||||||||||||||||||||||||||
| 12 | extract_error_msg | ||||||||||||||||||||||||||||
| 13 | create_error_msg | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | 15 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 16 | 'all' => [ | ||||||||||||||||||||||||||||
| 17 | qw( | ||||||||||||||||||||||||||||
| 18 | get_dotenv_vars | ||||||||||||||||||||||||||||
| 19 | interpret_dotenv_filepath_var | ||||||||||||||||||||||||||||
| 20 | get_envdot_filepaths_var_name | ||||||||||||||||||||||||||||
| 21 | extract_error_msg | ||||||||||||||||||||||||||||
| 22 | create_error_msg | ||||||||||||||||||||||||||||
| 23 | ) | ||||||||||||||||||||||||||||
| 24 | ], | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 15 | ▶ | use Cwd qw( abs_path ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 15 | ▶ | use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 15 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 15 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 15 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # ABSTRACT: Read environment variables from a .env file | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 15 | our $VERSION = '0.022'; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | use constant { | ||||||||||||||||||||||||||||
| 38 | 15 | OPTION_FILE_TYPE => q{file:type}, | |||||||||||||||||||||||||||
| 39 | OPTION_FILE_TYPE_PLAIN => q{plain}, | ||||||||||||||||||||||||||||
| 40 | OPTION_FILE_TYPE_SHELL => q{shell}, | ||||||||||||||||||||||||||||
| 41 | DEFAULT_OPTION_FILE_TYPE => q{shell}, | ||||||||||||||||||||||||||||
| 42 | OPTION_READ_FROM_PARENT => q{read:from_parent}, | ||||||||||||||||||||||||||||
| 43 | DEFAULT_OPTION_READ_FROM_PARENT => 0, | ||||||||||||||||||||||||||||
| 44 | OPTION_READ_ALLOW_MISSING_PARENT => q{read:allow_missing_parent}, | ||||||||||||||||||||||||||||
| 45 | DEFAULT_OPTION_READ_ALLOW_MISSING_PARENT => 0, | ||||||||||||||||||||||||||||
| 46 | 15 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 15 | my %DOTENV_OPTIONS = ( | |||||||||||||||||||||||||||
| 49 | OPTION_READ_FROM_PARENT() => 1, | ||||||||||||||||||||||||||||
| 50 | OPTION_READ_ALLOW_MISSING_PARENT() => 1, | ||||||||||||||||||||||||||||
| 51 | 'file:type' => 1, | ||||||||||||||||||||||||||||
| 52 | 'var:allow_interpolate' => 1, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 15 | my %DOS_PLATFORMS = ( | |||||||||||||||||||||||||||
| 55 | 'dos' => 'MS-DOS/PC-DOS', | ||||||||||||||||||||||||||||
| 56 | 'os2' => 'OS/2', | ||||||||||||||||||||||||||||
| 57 | 'MSWin32' => 'Windows', | ||||||||||||||||||||||||||||
| 58 | 'cygwin' => 'Cygwin', | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub get_dotenv_vars { | ||||||||||||||||||||||||||||
| 62 | 16 | ▶ | my (@dotenv_filepaths) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_dotenv_vars: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 16 | my @vars; | |||||||||||||||||||||||||||
| 65 | 16 | foreach my $filepath ( reverse @dotenv_filepaths ) { | |||||||||||||||||||||||||||
| 66 | 24 | ▶ | if ( -f $filepath ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 24 | push @vars, _read_dotenv_file_recursively($filepath); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 0 | my ($err) = "File not found: '$filepath'"; | |||||||||||||||||||||||||||
| 71 | 0 | croak create_error_msg($err); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 13 | return @vars; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub interpret_dotenv_filepath_var { | ||||||||||||||||||||||||||||
| 78 | 21 | ▶ | my ($var_content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
interpret_dotenv_filepath_var: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 21 | ▶ | if ( exists $DOS_PLATFORMS{$OSNAME} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | return split qr{;}msx, $var_content; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | 21 | return split qr{:}msx, $var_content; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub get_envdot_filepaths_var_name { | ||||||||||||||||||||||||||||
| 88 | 21 | ▶ | return q{ENVDOT_FILEPATHS}; | ||||||||||||||||||||||||||
|
Subroutine
get_envdot_filepaths_var_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Private subroutines | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub _read_dotenv_file_recursively { | ||||||||||||||||||||||||||||
| 94 | 29 | ▶ | my ($filepath) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_read_dotenv_file_recursively: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 95 | 29 | $filepath = abs_path($filepath); | |||||||||||||||||||||||||||
| 96 | 29 | my @rows = _read_dotenv_file($filepath); | |||||||||||||||||||||||||||
| 97 | 29 | my %r = _interpret_dotenv( $filepath, @rows ); | |||||||||||||||||||||||||||
| 98 | 27 | my @these_vars = @{ $r{'vars'} }; | |||||||||||||||||||||||||||
| 99 | 27 | ▶ | if ( $r{'opts'}->{ OPTION_READ_FROM_PARENT() } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 7 | my $parent_filepath = _get_parent_dotenv_filepath($filepath); | |||||||||||||||||||||||||||
| 101 | 7 | ▶ | if ($parent_filepath) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 102 | 4 | unshift @these_vars, _read_dotenv_file_recursively($parent_filepath); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | elsif ( !$r{'opts'}->{ OPTION_READ_ALLOW_MISSING_PARENT() } ) { | ||||||||||||||||||||||||||||
| 105 | 2 | my ($err) = "No parent .env file found for child file '$filepath'"; | |||||||||||||||||||||||||||
| 106 | 2 | croak create_error_msg($err); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 23 | return @these_vars; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Follow directory hierarchy upwards until you find a .env file. | ||||||||||||||||||||||||||||
| 113 | # If you don't, return undef. | ||||||||||||||||||||||||||||
| 114 | # Otherwise return the path. | ||||||||||||||||||||||||||||
| 115 | sub _get_parent_dotenv_filepath { | ||||||||||||||||||||||||||||
| 116 | 10 | ▶ | my ($current_filepath) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_parent_dotenv_filepath: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 10 | my ( $volume, $directories ) = File::Spec->splitpath($current_filepath); | |||||||||||||||||||||||||||
| 119 | 10 | my $parent_path = File::Spec->catpath( $volume, $directories ); | |||||||||||||||||||||||||||
| 120 | 10 | my $parent_filepath; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 10 | ▶ | while ( defined $parent_path && $parent_path ne File::Spec->rootdir() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $parent_path and $parent_path ne "File::Spec"->rootdir
MC/DC50%1/2
defined $parent_path and $parent_path ne "File::Spec"->rootdir
defined $parent_path
$parent_path ne "File::Spec"->rootdir
Truth table66%2/3
A and B
A= defined $parent_pathB= $parent_path ne "File::Spec"->rootdir
| |||||||||||||||||||||||||||||
| 123 | 22 | $parent_path = abs_path( File::Spec->catdir( $parent_path, File::Spec->updir ) ); | |||||||||||||||||||||||||||
| 124 | 22 | $parent_filepath = File::Spec->catfile( $parent_path, '.env' ); | |||||||||||||||||||||||||||
| 125 | 22 | ▶ | return $parent_filepath if ( defined $parent_path && -f $parent_filepath ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $parent_path and -f $parent_filepath
MC/DC50%1/2
defined $parent_path and -f $parent_filepath
defined $parent_path
-f $parent_filepath
Truth table66%2/3
A and B
A= defined $parent_pathB= -f $parent_filepath
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Explicitly return undef as sub doc decrees. | ||||||||||||||||||||||||||||
| 129 | 3 | return undef; ## no critic (Subroutines::ProhibitExplicitReturnUndef) | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub _interpret_dotenv { | ||||||||||||||||||||||||||||
| 133 | 36 | ▶ | my ( $fp, @rows ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_interpret_dotenv: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 134 | 36 | my %options = ( | |||||||||||||||||||||||||||
| 135 | OPTION_READ_FROM_PARENT() => DEFAULT_OPTION_READ_FROM_PARENT, | ||||||||||||||||||||||||||||
| 136 | OPTION_READ_ALLOW_MISSING_PARENT() => DEFAULT_OPTION_READ_ALLOW_MISSING_PARENT, | ||||||||||||||||||||||||||||
| 137 | 'file:type' => DEFAULT_OPTION_FILE_TYPE, | ||||||||||||||||||||||||||||
| 138 | 'var:allow_interpolate' => 0, | ||||||||||||||||||||||||||||
| 139 | ); # Options related to reading the file. Applied as they are read. | ||||||||||||||||||||||||||||
| 140 | 36 | my @vars; | |||||||||||||||||||||||||||
| 141 | 36 | my $row_num = 1; | |||||||||||||||||||||||||||
| 142 | 36 | foreach (@rows) { | |||||||||||||||||||||||||||
| 143 | ## no critic (ControlStructures::ProhibitCascadingIfElse) | ||||||||||||||||||||||||||||
| 144 | ## no critic (RegularExpressions::ProhibitComplexRegexes) | ||||||||||||||||||||||||||||
| 145 | 171 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 146 | # This is envdot meta command | ||||||||||||||||||||||||||||
| 147 | # The var:<value> options can only apply to one subsequent var row. | ||||||||||||||||||||||||||||
| 148 | m{ | ||||||||||||||||||||||||||||
| 149 | ^ [[:space:]]{0,} [#]{1} | ||||||||||||||||||||||||||||
| 150 | [[:space:]]{1,} envdot [[:space:]]{1,} | ||||||||||||||||||||||||||||
| 151 | [(] (?<opts> [^)]{0,}) [)] | ||||||||||||||||||||||||||||
| 152 | [[:space:]]{0,} $ | ||||||||||||||||||||||||||||
| 153 | }msx | ||||||||||||||||||||||||||||
| 154 | ) | ||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | 18 | my $opts = _interpret_opts( $LAST_PAREN_MATCH{opts} ); | |||||||||||||||||||||||||||
| 157 | 18 | foreach my $key ( keys %{$opts} ) { | |||||||||||||||||||||||||||
| 158 | 26 | ▶ | if ( !exists $DOTENV_OPTIONS{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 5 | my $err = "Unknown envdot option: '$key'"; | |||||||||||||||||||||||||||
| 160 | 5 | croak create_error_msg( $err, $row_num, $fp ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 13 | $options{'var:allow_interpolate'} = 0; | |||||||||||||||||||||||||||
| 164 | 13 | foreach ( keys %{$opts} ) { | |||||||||||||||||||||||||||
| 165 | 21 | $options{$_} = $opts->{$_}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | elsif ( | ||||||||||||||||||||||||||||
| 169 | # This is comment row | ||||||||||||||||||||||||||||
| 170 | m{ | ||||||||||||||||||||||||||||
| 171 | ^ [[:space:]]{0,} [#]{1} .* $ | ||||||||||||||||||||||||||||
| 172 | }msx | ||||||||||||||||||||||||||||
| 173 | ) | ||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 25 | 1; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | elsif ( | ||||||||||||||||||||||||||||
| 178 | # This is empty row | ||||||||||||||||||||||||||||
| 179 | m{ | ||||||||||||||||||||||||||||
| 180 | ^ [[:space:]]{0,} $ | ||||||||||||||||||||||||||||
| 181 | }msx | ||||||||||||||||||||||||||||
| 182 | ) | ||||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||
| 184 | 3 | 1; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | elsif ( | ||||||||||||||||||||||||||||
| 187 | # This is env var description | ||||||||||||||||||||||||||||
| 188 | m{ | ||||||||||||||||||||||||||||
| 189 | ^ (?<name> [^=]{1,}) = (?<value> .*) $ | ||||||||||||||||||||||||||||
| 190 | }msx | ||||||||||||||||||||||||||||
| 191 | ) | ||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 124 | my ( $name, $value ) = ( $LAST_PAREN_MATCH{name}, $LAST_PAREN_MATCH{value} ); | |||||||||||||||||||||||||||
| 194 | 124 | ▶ | if ( $options{'file:type'} eq OPTION_FILE_TYPE_SHELL ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 195 | 119 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | $value =~ m{ | ||||||||||||||||||||||||||||
| 197 | ^ | ||||||||||||||||||||||||||||
| 198 | ['"]{1} (?<value> .*) ["']{1} # Get value from between quotes | ||||||||||||||||||||||||||||
| 199 | (?: [;] [[:space:]]{0,} export [[:space:]]{1,} $name)? # optional | ||||||||||||||||||||||||||||
| 200 | [[:space:]]{0,} # optional whitespace at the end | ||||||||||||||||||||||||||||
| 201 | $ | ||||||||||||||||||||||||||||
| 202 | }msx | ||||||||||||||||||||||||||||
| 203 | ) | ||||||||||||||||||||||||||||
| 204 | { | ||||||||||||||||||||||||||||
| 205 | 95 | ($value) = $LAST_PAREN_MATCH{value}; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # "export" can also be at the start. Only for TYPE_SHELL | ||||||||||||||||||||||||||||
| 209 | 119 | ▶ | if ( $name =~ m{^ [[:space:]]{0,} export [[:space:]]{1,} }msx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 1 | $name =~ m{ | |||||||||||||||||||||||||||
| 211 | ^ | ||||||||||||||||||||||||||||
| 212 | [[:space:]]{0,} export [[:space:]]{1,} (?<name> .*) | ||||||||||||||||||||||||||||
| 213 | $ | ||||||||||||||||||||||||||||
| 214 | }msx; | ||||||||||||||||||||||||||||
| 215 | 1 | $name = $LAST_PAREN_MATCH{name}; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | elsif ( $options{'file:type'} eq OPTION_FILE_TYPE_PLAIN ) { | ||||||||||||||||||||||||||||
| 219 | 5 | 1; # document no-operation | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 124 | my %opts = ( allow_interpolate => $options{'var:allow_interpolate'}, ); | |||||||||||||||||||||||||||
| 222 | 124 | push @vars, { name => $name, value => $value, opts => \%opts, }; | |||||||||||||||||||||||||||
| 223 | 124 | $options{'var:allow_interpolate'} = 0; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||
| 226 | 1 | my $err = "Invalid line: '$_'"; | |||||||||||||||||||||||||||
| 227 | 1 | croak create_error_msg( $err, $row_num, $fp ); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 165 | $row_num++; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 30 | return opts => \%options, vars => \@vars; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | sub _interpret_opts { | ||||||||||||||||||||||||||||
| 235 | 24 | ▶ | my ($opts_str) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_interpret_opts: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 236 | 24 | my @opts = split qr{ [[:space:]]{0,} [,] [[:space:]]{0,} }msx, $opts_str; | |||||||||||||||||||||||||||
| 237 | 24 | my %opts; | |||||||||||||||||||||||||||
| 238 | 24 | foreach (@opts) { | |||||||||||||||||||||||||||
| 239 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||||||||||||||||
| 240 | 37 | my ( $key, $val ) = split qr/=/msx; | |||||||||||||||||||||||||||
| 241 | 37 | ▶ | $val = $val // 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$val // 1
MC/DC100%1/1
$val // 1
$val
Truth table100%2/2
A
A= $val
| |||||||||||||||||||||||||||||
| 242 | 37 | ▶ | $val = 1 if ( $val eq 'true' || $val eq 'True' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$val eq "true" or $val eq "True"
MC/DC50%1/2
$val eq "true" or $val eq "True"
$val eq "true"
$val eq "True"
Truth table66%2/3
A or B
A= $val eq "true"B= $val eq "True"
| |||||||||||||||||||||||||||||
| 243 | 37 | ▶ | $val = 0 if ( $val eq 'false' || $val eq 'False' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$val eq "false" or $val eq "False"
MC/DC50%1/2
$val eq "false" or $val eq "False"
$val eq "false"
$val eq "False"
Truth table66%2/3
A or B
A= $val eq "false"B= $val eq "False"
| |||||||||||||||||||||||||||||
| 244 | 37 | $opts{$key} = $val; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | 24 | return \%opts; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | sub _read_dotenv_file { | ||||||||||||||||||||||||||||
| 250 | 29 | ▶ | my ($filepath) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_read_dotenv_file: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 251 | 29 | my $fh = IO::File->new(); | |||||||||||||||||||||||||||
| 252 | 29 | ▶ | $fh->open(qq{< $filepath}) or croak "Error: Cannot open file '$filepath'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 29 | $fh->binmode(':encoding(UTF-8)'); | |||||||||||||||||||||||||||
| 254 | 29 | my @dotenv_rows = <$fh>; | |||||||||||||||||||||||||||
| 255 | 29 | chomp @dotenv_rows; | |||||||||||||||||||||||||||
| 256 | 29 | ▶ | $fh->close or croak "Error: Cannot close file '$filepath'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 29 | return @dotenv_rows; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Error messages: | ||||||||||||||||||||||||||||
| 261 | # Message structure: | ||||||||||||||||||||||||||||
| 262 | # <msg>! [line <num>] [file <filepath>] | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | sub extract_error_msg { | ||||||||||||||||||||||||||||
| 265 | 9 | ▶ | my ($msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
extract_error_msg: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 266 | 9 | ▶ | if ( !$msg ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 1 | croak 'Parameter error: missing parameter \'msg\''; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | ## no critic (RegularExpressions::ProhibitComplexRegexes) | ||||||||||||||||||||||||||||
| 270 | 8 | my ( $err, $line, $filepath ) = | |||||||||||||||||||||||||||
| 271 | $msg =~ m/^ ([^!]{1,}) \! (?: \s line \s ([[:digit:]]{1,}) (?: \s file \s \'([^']{1,})\' )? )? .* $/msx; | ||||||||||||||||||||||||||||
| 272 | 8 | return $err, $line, $filepath; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | sub create_error_msg { | ||||||||||||||||||||||||||||
| 276 | 15 | ▶ | my ( $err, $line, $filepath ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
create_error_msg: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 277 | 15 | ▶ | if ( !$err ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 1 | croak 'Parameter error: missing parameter \'err\''; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 14 | ▶ | if ( !$line && $filepath ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $line and $filepath
MC/DC100%2/2
not $line and $filepath
not $line
$filepath
Truth table100%3/3
not(A) and B
A= not $lineB= $filepath
| |||||||||||||||||||||||||||||
| 281 | 1 | croak 'Parameter error: missing parameter \'line\''; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | 13 | ▶ | return "${err}!" . ( defined $line ? " line ${line}" : q{} ) . ( defined $filepath ? " file '${filepath}'" : q{} ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 15 | 1; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | __END__ | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =pod | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head1 NAME | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | Env::Dot::Functions - Read environment variables from a .env file | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head1 VERSION | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | version 0.022 | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | use Env::Dot::Functions qw( get_dotenv_vars interpret_dotenv_filepath_var ); | ||||||||||||||||||||||||||||
| 305 | # or | ||||||||||||||||||||||||||||
| 306 | use Env::Dot::Functions ':all'; | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =for :stopwords env dotenv filepath filepaths | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head1 STATUS | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | This module is currently being developed so changes in the API are possible, | ||||||||||||||||||||||||||||
| 315 | though not likely. | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =for stopwords envdot | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | This package just contains functions for use | ||||||||||||||||||||||||||||
| 320 | in the main package L<Env::Dot> and in | ||||||||||||||||||||||||||||
| 321 | the command line tool B<envdot>. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | No functions are automatically exported to the calling namespace. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head2 get_dotenv_vars(@) | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =for stopwords env | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | Return all variables from the F<.env> file | ||||||||||||||||||||||||||||
| 332 | as a list of hashes (name/value pairs). | ||||||||||||||||||||||||||||
| 333 | This list is created in the same order the variables | ||||||||||||||||||||||||||||
| 334 | are read from the files and may therefore contain | ||||||||||||||||||||||||||||
| 335 | the same variable several times. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | The files, however, are read in reversed order, just like | ||||||||||||||||||||||||||||
| 338 | paths in variable B<PATH> are used. | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | Arguments: | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =over 8 | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =item * filepaths, list of dotenv filepaths. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =back | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | If a file does not exist, we break the execution. | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =head2 interpret_dotenv_filepath_var( $filepaths ) | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | Return a list of file paths. | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head2 get_envdot_filepaths_var_name | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =for stopwords env | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Return the name of the environment variable | ||||||||||||||||||||||||||||
| 359 | which user can use to specify the paths of .env files. | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head2 extract_error_msg | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Extract the elements of error message (exception): err, line and filepath. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head2 create_error_msg | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | create an error message (exception) from the three elements: err, line and filepath. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Mikko Koivunalho <mikkoi@cpan.org> | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | This software is copyright (c) 2023 by Mikko Koivunalho. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 378 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =cut | ||||||||||||||||||||||||||||