bin/envdot

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