| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
## no critic (ValuesAndExpressions::ProhibitConstantPragma) |
|
2
|
|
|
|
|
|
|
package Env::Dot::Functions; |
|
3
|
3
|
|
|
3
|
|
456094
|
use strict; |
|
|
3
|
|
|
|
|
13
|
|
|
|
3
|
|
|
|
|
115
|
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
86
|
|
|
5
|
3
|
|
|
3
|
|
1888
|
use Data::Dumper; |
|
|
3
|
|
|
|
|
20732
|
|
|
|
3
|
|
|
|
|
273
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
28
|
use Exporter 'import'; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
234
|
|
|
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
|
|
1111
|
use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier |
|
|
3
|
|
|
|
|
7376
|
|
|
|
3
|
|
|
|
|
19
|
|
|
15
|
3
|
|
|
3
|
|
1145
|
use Carp; |
|
|
3
|
|
|
|
|
10
|
|
|
|
3
|
|
|
|
|
312
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# ABSTRACT: Read environment variables from .env file |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.007'; # VERSION: generated by DZP::OurPkgVersion |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use constant { |
|
22
|
3
|
|
|
|
|
3872
|
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
|
|
23
|
}; |
|
|
3
|
|
|
|
|
31
|
|
|
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
|
7
|
my @dotenv_filepaths = @_; |
|
35
|
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
2
|
my @vars; |
|
37
|
1
|
|
|
|
|
3
|
foreach my $filepath (@dotenv_filepaths) { |
|
38
|
1
|
50
|
|
|
|
33
|
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
|
10089
|
return split qr{:}msx, $_[0]; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Private subroutines |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _interpret_dotenv { |
|
57
|
4
|
|
|
4
|
|
12993
|
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
|
|
|
|
|
10
|
foreach (@rows) { |
|
65
|
|
|
|
|
|
|
## no critic (ControlStructures::ProhibitCascadingIfElse) |
|
66
|
|
|
|
|
|
|
## no critic (RegularExpressions::ProhibitComplexRegexes) |
|
67
|
33
|
100
|
|
|
|
225
|
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
|
|
|
|
|
29
|
my $opts = _interpret_opts( $LAST_PAREN_MATCH{opts} ); |
|
79
|
6
|
|
|
|
|
24
|
_validate_opts($opts); |
|
80
|
6
|
|
|
|
|
13
|
$options{'var:allow_interpolate'} = 0; |
|
81
|
6
|
|
|
|
|
10
|
foreach ( keys %{$opts} ) { |
|
|
6
|
|
|
|
|
16
|
|
|
82
|
6
|
|
|
|
|
59
|
$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
|
|
|
|
|
8
|
1; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
elsif ( |
|
104
|
|
|
|
|
|
|
# This is env var description |
|
105
|
|
|
|
|
|
|
m{ |
|
106
|
|
|
|
|
|
|
^ (? [^=]{1,}) = (? .*) $ |
|
107
|
|
|
|
|
|
|
}msx |
|
108
|
|
|
|
|
|
|
) |
|
109
|
|
|
|
|
|
|
{ |
|
110
|
18
|
|
|
|
|
156
|
my ( $name, $value ) = ( $LAST_PAREN_MATCH{name}, $LAST_PAREN_MATCH{value} ); |
|
111
|
18
|
100
|
|
|
|
85
|
if ( $options{'file:type'} eq OPTION_FILE_TYPE_SHELL ) { |
|
|
|
50
|
|
|
|
|
|
|
112
|
11
|
100
|
|
|
|
500
|
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
|
|
|
|
|
40
|
($value) = $LAST_PAREN_MATCH{value}; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# "export" can also be at the start. Only for TYPE_SHELL |
|
126
|
11
|
100
|
|
|
|
50
|
if ( $name =~ m{^ [[:space:]]{0,} export [[:space:]]{1,} }msx ) { |
|
127
|
1
|
|
|
|
|
9
|
$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
|
|
|
|
|
11
|
1; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
18
|
|
|
|
|
50
|
my %opts = ( allow_interpolate => $options{'var:allow_interpolate'}, ); |
|
139
|
18
|
|
|
|
|
58
|
push @vars, { name => $name, value => $value, opts => \%opts, }; |
|
140
|
18
|
|
|
|
|
44
|
$options{'var:allow_interpolate'} = 0; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
else { |
|
143
|
0
|
|
|
|
|
0
|
carp "Uninterpretable row: $_"; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
} |
|
146
|
4
|
|
|
|
|
21
|
return @vars; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _validate_opts { |
|
150
|
6
|
|
|
6
|
|
22
|
my ($opts) = @_; |
|
151
|
6
|
|
|
|
|
11
|
foreach my $key ( keys %{$opts} ) { |
|
|
6
|
|
|
|
|
34
|
|
|
152
|
6
|
50
|
|
|
|
23
|
if ( !exists $DOTENV_OPTIONS{$key} ) { |
|
153
|
0
|
|
|
|
|
0
|
croak "Unknown envdot option: $key"; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
} |
|
156
|
6
|
|
|
|
|
27
|
return; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub _interpret_opts { |
|
160
|
12
|
|
|
12
|
|
9648
|
my ($opts_str) = @_; |
|
161
|
12
|
|
|
|
|
112
|
my @opts = split qr{ |
|
162
|
|
|
|
|
|
|
[[:space:]]{0,} [,] [[:space:]]{0,} |
|
163
|
|
|
|
|
|
|
}msx, $opts_str; |
|
164
|
12
|
|
|
|
|
33
|
my %opts; |
|
165
|
12
|
|
|
|
|
24
|
foreach (@opts) { |
|
166
|
|
|
|
|
|
|
## no critic (ControlStructures::ProhibitPostfixControls) |
|
167
|
17
|
|
|
|
|
77
|
my ( $key, $val ) = split qr/=/msx; |
|
168
|
17
|
|
100
|
|
|
63
|
$val = $val // 1; |
|
169
|
17
|
50
|
33
|
|
|
72
|
$val = 1 if ( $val eq 'true' || $val eq 'True' ); |
|
170
|
17
|
50
|
33
|
|
|
59
|
$val = 0 if ( $val eq 'false' || $val eq 'False' ); |
|
171
|
17
|
|
|
|
|
48
|
$opts{$key} = $val; |
|
172
|
|
|
|
|
|
|
} |
|
173
|
12
|
|
|
|
|
36
|
return \%opts; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub _read_dotenv_file { |
|
177
|
1
|
|
|
1
|
|
6
|
my ($filepath) = @_; |
|
178
|
1
|
50
|
|
|
|
59
|
open my $fh, q{<}, $filepath or croak "Cannot open file '$filepath'"; |
|
179
|
1
|
|
|
|
|
4
|
my @dotenv_rows; |
|
180
|
1
|
|
|
|
|
53
|
while (<$fh>) { chomp; push @dotenv_rows, $_; } |
|
|
10
|
|
|
|
|
20
|
|
|
|
10
|
|
|
|
|
44
|
|
|
181
|
1
|
50
|
|
|
|
16
|
close $fh or croak "Cannot close file '$filepath'"; |
|
182
|
1
|
|
|
|
|
10
|
return @dotenv_rows; |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
__END__ |