line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## no critic (ValuesAndExpressions::ProhibitConstantPragma) |
2
|
|
|
|
|
|
|
package Env::Dot::Functions; |
3
|
3
|
|
|
3
|
|
474205
|
use strict; |
|
3
|
|
|
|
|
17
|
|
|
3
|
|
|
|
|
114
|
|
4
|
3
|
|
|
3
|
|
32
|
use warnings; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
90
|
|
5
|
3
|
|
|
3
|
|
1984
|
use Data::Dumper; |
|
3
|
|
|
|
|
19660
|
|
|
3
|
|
|
|
|
207
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
25
|
use Exporter 'import'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
174
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
9
|
|
|
|
|
|
|
get_dotenv_vars |
10
|
|
|
|
|
|
|
interpret_dotenv_filepath_var |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [qw( get_dotenv_vars interpret_dotenv_filepath_var )], ); |
13
|
|
|
|
|
|
|
|
14
|
3
|
|
|
3
|
|
1078
|
use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier |
|
3
|
|
|
|
|
7744
|
|
|
3
|
|
|
|
|
16
|
|
15
|
3
|
|
|
3
|
|
1029
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
244
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# ABSTRACT: Read environment variables from .env file |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION: generated by DZP::OurPkgVersion |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use constant { |
22
|
3
|
|
|
|
|
3486
|
OPTION_FILE_TYPE => q{file:type}, |
23
|
|
|
|
|
|
|
OPTION_FILE_TYPE_PLAIN => q{plain}, |
24
|
|
|
|
|
|
|
OPTION_FILE_TYPE_SHELL => q{shell}, |
25
|
|
|
|
|
|
|
DEFAULT_OPTION_FILE_TYPE => q{shell}, |
26
|
3
|
|
|
3
|
|
20
|
}; |
|
3
|
|
|
|
|
23
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my %DOTENV_OPTIONS = ( |
29
|
|
|
|
|
|
|
'file:type' => 1, |
30
|
|
|
|
|
|
|
'var:allow_interpolate' => 1, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub get_dotenv_vars { |
34
|
1
|
|
|
1
|
1
|
3
|
my @dotenv_filepaths = @_; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
1
|
my @vars; |
37
|
1
|
|
|
|
|
2
|
foreach my $filepath (@dotenv_filepaths) { |
38
|
1
|
50
|
|
|
|
24
|
if ( -f $filepath ) { |
39
|
1
|
|
|
|
|
4
|
my @rows = _read_dotenv_file($filepath); |
40
|
1
|
|
|
|
|
4
|
my @tmp = _interpret_dotenv(@rows); |
41
|
1
|
|
|
|
|
4
|
push @vars, @tmp; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
0
|
|
|
|
|
0
|
carp "No file found: '$filepath'"; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
1
|
|
|
|
|
4
|
return @vars; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub interpret_dotenv_filepath_var { ## no critic (Subroutines::RequireArgUnpacking) |
51
|
7
|
|
|
7
|
1
|
10066
|
return split qr{:}msx, $_[0]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Private subroutines |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _interpret_dotenv { |
57
|
4
|
|
|
4
|
|
13521
|
my (@rows) = @_; |
58
|
4
|
|
|
|
|
15
|
my %options = ( |
59
|
|
|
|
|
|
|
'file:type' => DEFAULT_OPTION_FILE_TYPE, |
60
|
|
|
|
|
|
|
'var:allow_interpolate' => 0, |
61
|
|
|
|
|
|
|
); # Options related to reading the file. Applied as they are read. |
62
|
|
|
|
|
|
|
# my %vars; |
63
|
4
|
|
|
|
|
7
|
my @vars; |
64
|
4
|
|
|
|
|
8
|
foreach (@rows) { |
65
|
|
|
|
|
|
|
## no critic (ControlStructures::ProhibitCascadingIfElse) |
66
|
|
|
|
|
|
|
## no critic (RegularExpressions::ProhibitComplexRegexes) |
67
|
33
|
100
|
|
|
|
200
|
if ( |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# This is envdot meta command |
69
|
|
|
|
|
|
|
# The var: options can only apply to one subsequent var row. |
70
|
|
|
|
|
|
|
m{ |
71
|
|
|
|
|
|
|
^ [[:space:]]{0,} [#]{1} |
72
|
|
|
|
|
|
|
[[:space:]]{1,} envdot [[:space:]]{1,} |
73
|
|
|
|
|
|
|
[(] (? [^)]{0,}) [)] |
74
|
|
|
|
|
|
|
[[:space:]]{0,} $ |
75
|
|
|
|
|
|
|
}msx |
76
|
|
|
|
|
|
|
) |
77
|
|
|
|
|
|
|
{ |
78
|
6
|
|
|
|
|
27
|
my $opts = _interpret_opts( $LAST_PAREN_MATCH{opts} ); |
79
|
6
|
|
|
|
|
20
|
_validate_opts($opts); |
80
|
6
|
|
|
|
|
10
|
$options{'var:allow_interpolate'} = 0; |
81
|
6
|
|
|
|
|
9
|
foreach ( keys %{$opts} ) { |
|
6
|
|
|
|
|
15
|
|
82
|
6
|
|
|
|
|
23
|
$options{$_} = $opts->{$_}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
elsif ( |
86
|
|
|
|
|
|
|
# This is comment row |
87
|
|
|
|
|
|
|
m{ |
88
|
|
|
|
|
|
|
^ [[:space:]]{0,} [#]{1} .* $ |
89
|
|
|
|
|
|
|
}msx |
90
|
|
|
|
|
|
|
) |
91
|
|
|
|
|
|
|
{ |
92
|
5
|
|
|
|
|
11
|
1; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
elsif ( |
95
|
|
|
|
|
|
|
# This is empty row |
96
|
|
|
|
|
|
|
m{ |
97
|
|
|
|
|
|
|
^ [[:space:]]{0,} $ |
98
|
|
|
|
|
|
|
}msx |
99
|
|
|
|
|
|
|
) |
100
|
|
|
|
|
|
|
{ |
101
|
4
|
|
|
|
|
7
|
1; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
elsif ( |
104
|
|
|
|
|
|
|
# This is env var description |
105
|
|
|
|
|
|
|
m{ |
106
|
|
|
|
|
|
|
^ (? [^=]{1,}) = (? .*) $ |
107
|
|
|
|
|
|
|
}msx |
108
|
|
|
|
|
|
|
) |
109
|
|
|
|
|
|
|
{ |
110
|
18
|
|
|
|
|
130
|
my ( $name, $value ) = ( $LAST_PAREN_MATCH{name}, $LAST_PAREN_MATCH{value} ); |
111
|
18
|
100
|
|
|
|
67
|
if ( $options{'file:type'} eq OPTION_FILE_TYPE_SHELL ) { |
|
|
50
|
|
|
|
|
|
112
|
11
|
100
|
|
|
|
502
|
if ( |
113
|
|
|
|
|
|
|
$value =~ m{ |
114
|
|
|
|
|
|
|
^ |
115
|
|
|
|
|
|
|
['"]{1} (? .*) ["']{1} # Get value from between quotes |
116
|
|
|
|
|
|
|
(?: [;] [[:space:]]{0,} export [[:space:]]{1,} $name)? # optional |
117
|
|
|
|
|
|
|
[[:space:]]{0,} # optional whitespace at the end |
118
|
|
|
|
|
|
|
$ |
119
|
|
|
|
|
|
|
}msx |
120
|
|
|
|
|
|
|
) |
121
|
|
|
|
|
|
|
{ |
122
|
6
|
|
|
|
|
38
|
($value) = $LAST_PAREN_MATCH{value}; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# "export" can also be at the start. Only for TYPE_SHELL |
126
|
11
|
100
|
|
|
|
51
|
if ( $name =~ m{^ [[:space:]]{0,} export [[:space:]]{1,} }msx ) { |
127
|
1
|
|
|
|
|
8
|
$name =~ m{ |
128
|
|
|
|
|
|
|
^ |
129
|
|
|
|
|
|
|
[[:space:]]{0,} export [[:space:]]{1,} (? .*) |
130
|
|
|
|
|
|
|
$ |
131
|
|
|
|
|
|
|
}msx; |
132
|
1
|
|
|
|
|
5
|
$name = $LAST_PAREN_MATCH{name}; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
elsif ( $options{'file:type'} eq OPTION_FILE_TYPE_PLAIN ) { |
136
|
7
|
|
|
|
|
10
|
1; |
137
|
|
|
|
|
|
|
} |
138
|
18
|
|
|
|
|
49
|
my %opts = ( allow_interpolate => $options{'var:allow_interpolate'}, ); |
139
|
18
|
|
|
|
|
67
|
push @vars, { name => $name, value => $value, opts => \%opts, }; |
140
|
18
|
|
|
|
|
59
|
$options{'var:allow_interpolate'} = 0; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else { |
143
|
0
|
|
|
|
|
0
|
carp "Uninterpretable row: $_"; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
4
|
|
|
|
|
33
|
return @vars; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _validate_opts { |
150
|
6
|
|
|
6
|
|
21
|
my ($opts) = @_; |
151
|
6
|
|
|
|
|
16
|
foreach my $key ( keys %{$opts} ) { |
|
6
|
|
|
|
|
35
|
|
152
|
6
|
50
|
|
|
|
23
|
if ( !exists $DOTENV_OPTIONS{$key} ) { |
153
|
0
|
|
|
|
|
0
|
croak "Unknown envdot option: $key"; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
6
|
|
|
|
|
13
|
return; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub _interpret_opts { |
160
|
12
|
|
|
12
|
|
10040
|
my ($opts_str) = @_; |
161
|
12
|
|
|
|
|
90
|
my @opts = split qr{ |
162
|
|
|
|
|
|
|
[[:space:]]{0,} [,] [[:space:]]{0,} |
163
|
|
|
|
|
|
|
}msx, $opts_str; |
164
|
12
|
|
|
|
|
32
|
my %opts; |
165
|
12
|
|
|
|
|
23
|
foreach (@opts) { |
166
|
|
|
|
|
|
|
## no critic (ControlStructures::ProhibitPostfixControls) |
167
|
17
|
|
|
|
|
73
|
my ( $key, $val ) = split qr/=/msx; |
168
|
17
|
|
100
|
|
|
53
|
$val = $val // 1; |
169
|
17
|
50
|
33
|
|
|
71
|
$val = 1 if ( $val eq 'true' || $val eq 'True' ); |
170
|
17
|
50
|
33
|
|
|
53
|
$val = 0 if ( $val eq 'false' || $val eq 'False' ); |
171
|
17
|
|
|
|
|
46
|
$opts{$key} = $val; |
172
|
|
|
|
|
|
|
} |
173
|
12
|
|
|
|
|
36
|
return \%opts; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub _read_dotenv_file { |
177
|
1
|
|
|
1
|
|
3
|
my ($filepath) = @_; |
178
|
1
|
50
|
|
|
|
76
|
open my $fh, q{<}, $filepath or croak "Cannot open file '$filepath'"; |
179
|
1
|
|
|
|
|
4
|
my @dotenv_rows; |
180
|
1
|
|
|
|
|
42
|
while (<$fh>) { chomp; push @dotenv_rows, $_; } |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
33
|
|
181
|
1
|
50
|
|
|
|
19
|
close $fh or croak "Cannot close file '$filepath'"; |
182
|
1
|
|
|
|
|
8
|
return @dotenv_rows; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
__END__ |