| 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'; | |||||||||||||||||
| 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 | 8 | ▶ | use constant { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.7
| |||||||||||||||||||
| 28 | 8 | ▶ | DEFAULT_OPTION_DOTENV_FILENAME => '.env', | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 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 | }; | |||||||||||||||||
| 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"
MC/DC0%0/1
$SHELL_ALTERNATIVES{$ENV{"SHELL"}} // "sh"
$SHELL_ALTERNATIVES{$ENV{"SHELL"}}
Truth table50%1/2
A
A= $SHELL_ALTERNATIVES{$ENV{"SHELL"}}
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 6 | ▶ | sub main { | ||||||||||||||||
|
Subroutine
main: called CC=12 SCAR=25.3
| |||||||||||||||||||
| 61 | 6 | my $var_name = get_envdot_filepaths_var_name(); | |||||||||||||||||
| 62 | 6 | my @dotenv_filepaths; | |||||||||||||||||
| 63 | 6 | ▶ | if( exists $ENV{ $var_name } ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 64 | 2 | @dotenv_filepaths = interpret_dotenv_filepath_var( $ENV{ $var_name } ); | |||||||||||||||||
| 65 | } elsif( $read_from_stdin ) { | ||||||||||||||||||
| 66 | 0 | croak 'Error: Option not implemented'; | |||||||||||||||||
| 67 | } else { | ||||||||||||||||||
| 68 | 4 | ▶ | if( ! -f $dotenv_filepath ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 1 | ▶ | print {*STDERR} "Error: File not found: '$dotenv_filepath'\n" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 70 | or croak 'Cannot print error message'; | ||||||||||||||||||
| 71 | 1 | return EXIT_ERROR_NO_FILE; | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | 3 | @dotenv_filepaths = ( $dotenv_filepath ); # The CLI parameter | |||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 5 | my @vars; | |||||||||||||||||
| 77 | 5 | foreach my $dotenv_filepath (reverse @dotenv_filepaths) { | |||||||||||||||||
| 78 | 5 | local $EVAL_ERROR = undef; | |||||||||||||||||
| 79 | 5 | my @these_vars; | |||||||||||||||||
| 80 | 5 | ▶ | eval { @these_vars = get_dotenv_vars( $dotenv_filepath ); 1; } or do { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | 1 | my $e = $EVAL_ERROR; | |||||||||||||||||
| 82 | 1 | my ($err, $l, $fp) = extract_error_msg($e); | |||||||||||||||||
| 83 | 1 | ▶ | print {*STDERR} 'Error: ' . $err . ($l ? qq{ line $l} : q{}) . ($fp ? qq{ file '$fp'} : q{}) . "\n" | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 84 | or croak 'Cannot print error message'; | ||||||||||||||||||
| 85 | 1 | return EXIT_ERROR_OTHER_ERROR; | |||||||||||||||||
| 86 | }; | ||||||||||||||||||
| 87 | 4 | push @vars, @these_vars; | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | 4 | $_->{'opts'}->{'export'} = $export foreach (@vars); | |||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 4 | ▶ | print {*STDOUT} convert_variables_into_commands( $shell, @vars ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 92 | or croak 'Cannot print variables to STDOUT'; | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 4 | return EXIT_SUCCESS; | |||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 6 | exit main(@ARGV); | |||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||