line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## no critic (ValuesAndExpressions::ProhibitConstantPragma) |
2
|
|
|
|
|
|
|
package Env::Dot::ScriptFunctions; |
3
|
1
|
|
|
1
|
|
230097
|
use strict; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
28
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
5
|
1
|
|
|
1
|
|
598
|
use Data::Dumper; |
|
1
|
|
|
|
|
6567
|
|
|
1
|
|
|
|
|
62
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
9
|
|
|
|
|
|
|
convert_variables_into_commands |
10
|
|
|
|
|
|
|
); |
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [qw( convert_variables_into_commands )], ); |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
550
|
use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier |
|
1
|
|
|
|
|
3822
|
|
|
1
|
|
|
|
|
5
|
|
14
|
1
|
|
|
1
|
|
372
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
75
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# ABSTRACT: Read environment variables from .env file |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION: generated by DZP::OurPkgVersion |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use constant { |
21
|
1
|
|
|
|
|
630
|
OPTION_FILE_TYPE => q{file:type}, |
22
|
|
|
|
|
|
|
OPTION_FILE_TYPE_PLAIN => q{plain}, |
23
|
|
|
|
|
|
|
OPTION_FILE_TYPE_SHELL => q{shell}, |
24
|
|
|
|
|
|
|
DEFAULT_OPTION_FILE_TYPE => q{shell}, |
25
|
1
|
|
|
1
|
|
8
|
}; |
|
1
|
|
|
|
|
2
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my %DOTENV_OPTIONS = ( |
28
|
|
|
|
|
|
|
'file:type' => 1, |
29
|
|
|
|
|
|
|
'var:allow_interpolate' => 1, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my %VAR_OUTPUT = ( |
33
|
|
|
|
|
|
|
q{sh} => \&_convert_var_to_sh, |
34
|
|
|
|
|
|
|
q{csh} => \&_convert_var_to_csh, |
35
|
|
|
|
|
|
|
q{fish} => \&_convert_var_to_fish, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub convert_variables_into_commands { |
39
|
2
|
|
|
2
|
1
|
2367
|
my ( $shell, @vars ) = @_; |
40
|
2
|
|
|
|
|
4
|
my $out = q{}; |
41
|
2
|
|
|
|
|
5
|
foreach my $var (@vars) { |
42
|
4
|
|
|
|
|
21
|
$out .= _convert_variable( $shell, $var ); |
43
|
4
|
|
|
|
|
11
|
$out .= "\n"; |
44
|
|
|
|
|
|
|
} |
45
|
2
|
|
|
|
|
13
|
return $out; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Private subroutines |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _convert_variable { |
51
|
4
|
|
|
4
|
|
12
|
my ( $shell, $var ) = @_; |
52
|
4
|
50
|
|
|
|
13
|
if ( exists $VAR_OUTPUT{$shell} ) { |
53
|
4
|
|
|
|
|
6
|
return &{ $VAR_OUTPUT{$shell} }($var); |
|
4
|
|
|
|
|
8
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
0
|
|
|
|
|
0
|
croak "Unknown shell: $shell"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _convert_var_to_sh { |
61
|
7
|
|
|
7
|
|
7665
|
my ($var) = @_; |
62
|
|
|
|
|
|
|
my ( $name, $value, $want_export, $allow_interpolate ) = |
63
|
7
|
|
|
|
|
22
|
( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, ); |
64
|
7
|
100
|
|
|
|
16
|
my $quote = $allow_interpolate ? q{"} : q{'}; |
65
|
7
|
100
|
|
|
|
14
|
if ($want_export) { |
66
|
4
|
|
|
|
|
22
|
return sprintf "%s=$quote%s$quote; export %s", $name, $value, $name; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
3
|
|
|
|
|
20
|
return sprintf "%s=$quote%s$quote", $name, $value; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _convert_var_to_csh { |
74
|
3
|
|
|
3
|
|
3396
|
my ($var) = @_; |
75
|
|
|
|
|
|
|
my ( $name, $value, $want_export, $allow_interpolate ) = |
76
|
3
|
|
|
|
|
11
|
( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, ); |
77
|
3
|
100
|
|
|
|
21
|
my $quote = $allow_interpolate ? q{"} : q{'}; |
78
|
3
|
100
|
|
|
|
6
|
if ($want_export) { |
79
|
2
|
|
|
|
|
31
|
return sprintf "setenv %s $quote%s$quote", $name, $value; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
else { |
82
|
1
|
|
|
|
|
6
|
return sprintf "set %s $quote%s$quote", $name, $value; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _convert_var_to_fish { |
87
|
0
|
|
|
0
|
|
|
my ($var) = @_; |
88
|
|
|
|
|
|
|
my ( $name, $value, $want_export, $allow_interpolate ) = |
89
|
0
|
|
|
|
|
|
( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, ); |
90
|
0
|
0
|
|
|
|
|
my $quote = $allow_interpolate ? q{"} : q{'}; |
91
|
0
|
|
|
|
|
|
return sprintf "set -e %s; set -x -U %s $quote%s$quote", $name, $name, $value; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |