bin/envdot

filter: Statementstmt 98.7% 76 / 77 Branchbran 61.5% 16 / 26 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 89.1% 107 / 120 CC 14 SCAR 18.5
CC
14
Cov
89%
main
12.5
BEGIN
3.2
BEGIN
1.0
CRAP 6.4
« blib/lib/Env/Dot.pm ↑ Summary blib/lib/Env/Dot/Functions.pm »
1 #!/usr/bin/env perl
2 ## no critic (ControlStructures::ProhibitPostfixControls)
3 ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use PerlIO::encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use open ':std', IO => ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Read .env file and turn its content into environment variables for different shells.
11  
12 # PODNAME: envdot
13  
14 8 our $VERSION = '0.023_08'; # TRIAL
15 8 $VERSION =~ tr/_//d;
16  
17 8 ▶use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Getopt::Long qw( :config auto_version auto_help );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶use Env::Dot::Functions qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Env::Dot::ScriptFunctions qw( convert_variables_into_commands );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 8 local $OUTPUT_AUTOFLUSH = 1;
27  
28 8 ▶use constant {
Subroutine BEGIN: called CC=3 SCAR=11.7
29 8 ▶ DEFAULT_OPTION_DOTENV_FILENAME => '.env',
Branch: 2 branches
BranchTrueFalseTotal
exists &Errno::ENOENT ? : 8 0 8
exists &Errno::EINVAL ? : 8 0 8
30 DEFAULT_OPTION_SHELL => q{sh},
31 DEFAULT_OPTION_READ_FROM_STDIN => 0,
32 EXIT_SUCCESS => 0,
33 EXIT_ERROR_NO_FILE => (exists &Errno::ENOENT ? Errno::ENOENT : 255),
34 EXIT_ERROR_OTHER_ERROR => (exists &Errno::EINVAL ? Errno::EINVAL : 255),
35 8 };
36  
37 8 my %SHELL_ALTERNATIVES = (
38 sh => 'sh',
39 bash => 'sh',
40 dash => 'sh',
41 ksh => 'sh',
42 csh => 'csh',
43 tcsh => 'csh',
44 fish => 'fish',
45 );
46  
47 8 my $man = 0;
48 8 my $export = 1;
49 8 ▶my $shell = $SHELL_ALTERNATIVES{ $ENV{SHELL} } // DEFAULT_OPTION_SHELL;
Condition50%1/2
$SHELL_ALTERNATIVES{$ENV{"SHELL"}} // "sh"
l!l
08
MC/DC0%0/1
$SHELL_ALTERNATIVES{$ENV{"SHELL"}} // "sh"
$SHELL_ALTERNATIVES{$ENV{"SHELL"}}
Truth table50%1/2
A
A= $SHELL_ALTERNATIVES{$ENV{"SHELL"}}
Aresult
11
00
50 8 my $dotenv_filepath = DEFAULT_OPTION_DOTENV_FILENAME;
51 8 my $read_from_stdin = DEFAULT_OPTION_READ_FROM_STDIN;
52 8 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("man", \$man, "export!", \$export, "shell|s=s", \$shell, "dotenv|e=s", \$dotenv_filepath, "", \$read_from_stdin) or pod2usage(2) 6 0 6
53 'man' => \$man,
54 'export!' => \$export,
55 'shell|s=s' => \$shell,
56 'dotenv|e=s' => \$dotenv_filepath,
57 '' => \$read_from_stdin, ## no critic (ValuesAndExpressions::ProhibitEmptyQuotes)
58 ) or pod2usage(2);
59 6 ▶pod2usage(-exitval => 0, -verbose => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 6 6
60  
61 6 ▶sub main {
Subroutine main: called CC=12 SCAR=25.3
62 6 my $var_name = get_envdot_filepaths_var_name();
63 6 my @dotenv_filepaths;
64 6 ▶ if( exists $ENV{ $var_name } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{$var_name}) { } 2 4 6
elsif ($read_from_stdin) { } 0 4 4
65 2 @dotenv_filepaths = interpret_dotenv_filepath_var( $ENV{ $var_name } );
66 } elsif( $read_from_stdin ) {
67 0 croak 'Error: Option not implemented';
68 } else {
69 4 ▶ if( ! -f $dotenv_filepath ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $dotenv_filepath) 1 3 4
70 1 ▶ print {*STDERR} "Error: File not found: '$dotenv_filepath'\n"
Branch: 1 branch
BranchTrueFalseTotal
print {*STDERR;} "Error: File not found: '${dotenv_filepath}'\n" or croak("Cannot print error message") 1 0 1
71 or croak 'Cannot print error message';
72 1 return EXIT_ERROR_NO_FILE;
73 }
74 3 @dotenv_filepaths = ( $dotenv_filepath ); # The CLI parameter
75 }
76  
77 5 my @vars;
78 5 foreach my $dotenv_filepath (reverse @dotenv_filepaths) {
79 5 local $EVAL_ERROR = undef;
80 5 my @these_vars;
81 5 ▶ eval { @these_vars = get_dotenv_vars( $dotenv_filepath ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @these_vars = get_dotenv_vars($dotenv_filepath); 1 } } or do { my $e = $main::EVAL_ERROR; () = extract_error_msg($e); print {*STDERR;} 'Error: ' . $err . ($l ? " line $l" : "") . ($fp ? " file '${fp}'" : "") . "\n" or croak("Cannot print error message"); return 22 } 4 1 5
82 1 my $e = $EVAL_ERROR;
83 1 my ($err, $l, $fp) = extract_error_msg($e);
84 1 ▶ print {*STDERR} 'Error: ' . $err . ($l ? qq{ line $l} : q{}) . ($fp ? qq{ file '$fp'} : q{}) . "\n"
Branch: 3 branches
BranchTrueFalseTotal
print {*STDERR;} 'Error: ' . $err . ($l ? " line $l" : "") . ($fp ? " file '${fp}'" : "") . "\n" or croak("Cannot print error message") 1 0 1
$l ? : 1 0 1
$fp ? : 1 0 1
85 or croak 'Cannot print error message';
86 1 return EXIT_ERROR_OTHER_ERROR;
87 };
88 4 push @vars, @these_vars;
89 }
90 4 $_->{'opts'}->{'export'} = $export foreach (@vars);
91  
92 4 ▶ print {*STDOUT} convert_variables_into_commands( $shell, @vars )
Branch: 1 branch
BranchTrueFalseTotal
print {*STDOUT;} convert_variables_into_commands($shell, @vars) or croak("Cannot print variables to STDOUT") 4 0 4
93 or croak 'Cannot print variables to STDOUT';
94  
95 4 return EXIT_SUCCESS;
96 }
97  
98 6 exit main(@ARGV);
99  
100 __END__
101  
102 =pod
103  
104 =for stopwords dotenv env envdot shdotenv subshells
105  
106 =head1 NAME
107  
108 envdot - Read .env file and turn its content into environment variables for different shells.
109  
110  
111 =head1 SYNOPSIS
112  
113 envdot [options]
114  
115 eval `envdot`
116  
117 Options:
118 --help
119 --man
120 --version
121 --export --no-export
122 --shell -s
123 --dotenv -e
124  
125 =head2 CLI interface without dependencies
126  
127 The F<envdot> command is also available
128 as a self contained executable.
129 You can download it and run it as it is without
130 additional installation of CPAN packages.
131 Of course, you still need Perl, but Perl comes with any
132 normal Linux installation.
133  
134 This can be convenient if you want to, for instance,
135 include F<envdot> in a docker container build.
136  
137 curl -LSs -o envdot https://raw.githubusercontent.com/mikkoi/env-dot/main/envdot.self-contained
138 chmod +x ./envdot
139  
140  
141 =head1 OPTIONS
142  
143 =over 8
144  
145 =item B<--help>
146  
147 Print a brief help message and exits.
148  
149 =item B<--man>
150  
151 Prints the manual page and exits.
152  
153 =item B<--version>
154  
155 Prints the version and exits.
156  
157 =item B<--export>, B<--no-export>
158  
159 Write commands to set variables for local shell or for exporting them.
160 You usually want to export the variables
161 to all subsequent programs and subshells, i.e.
162 make them into I<environment variables>.
163  
164 Default: export
165  
166 =item B<-s>, B<--shell>
167  
168 Which shell (family) are you using? Supported: sh, csh, fish.
169  
170 Default: sh
171  
172 =item B<-e>, B<--dotenv>
173  
174 Path to F<.env> file.
175  
176 Default: current directory F<.env>
177  
178 =back
179  
180  
181 =head1 DESCRIPTION
182  
183 B<envdot> reads your F<.env> file and converts it
184 into environment variable commands suitable for
185 different shells (shell families): B<sh>, B<csh> and B<fish>.
186  
187 F<.env> files can be written in different flavors.
188 B<envdot> supports the often used B<sh> compatible flavor and
189 the B<docker> flavor which are not compatible with each other.
190  
191 If you have several F<.env> files, you can read them in at one go
192 with the help of the environment variable B<ENVDOT_FILEPATHS>.
193 Separate the full paths with 'B<:>' character.
194  
195 Env::Dot will load the files in the B<reverse order>,
196 starting from the last. This is the same ordering as used in B<PATH> variable:
197 the first overrules the following ones, that is, when reading from the last path
198 to the first path, if same variable is present in more than one file, the later
199 one replaces the one already read.
200  
201 If you have set the variable ENVDOT_FILEPATHS, then B<envdot> will use that.
202 Otherwise, it uses the command line parameter.
203 If no parameter, then default value is used. Default is the file
204 F<.env> in the current directory.
205  
206  
207 =head1 EXAMPLES
208  
209 eval `envdot --no-export --shell csh`
210  
211 eval `envdot --dotenv subdir/.env`
212  
213 ENVDOT_FILEPATHS='../.env:subdir/.env:.env' eval `envdot`
214  
215  
216 =head1 DEPENDENCIES
217  
218 No external dependencies outside Perl's standard distribution.
219  
220  
221 =head1 SEE ALSO
222  
223 L<Env::Assert> will verify that you certainly have those environmental
224 variables you need. It also has an executable which can, for example,
225 perform the check in the beginning of a B<docker> container run.
226  
227 L<Dotenv> and L<ENV::Util|https://metacpan.org/pod/ENV::Util>
228 are packages which also implement functionality to use
229 F<.env> files in Perl.
230  
231 L<Config::ENV> and L<Config::Layered::Source::ENV> provide other means
232 to configure application with the help of environment variables.
233  
234 L<shdotenv|https://github.com/ko1nksm/shdotenv> is a project to provide dotenv
235 for shells with support for POSIX-compliant and multiple .env file syntax.
236  
237 =cut
« blib/lib/Env/Dot.pm ↑ Summary blib/lib/Env/Dot/Functions.pm »