File Coverage

blib/lib/Env/Dot/ScriptFunctions.pm
Criterion Covered Total %
statement 43 48 89.5
branch 9 12 75.0
condition n/a
subroutine 11 12 91.6
pod 1 1 100.0
total 64 73 87.6


line stmt bran cond sub pod time code
1             ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
2             package Env::Dot::ScriptFunctions;
3 1     1   227794 use strict;
  1         10  
  1         27  
4 1     1   9 use warnings;
  1         1  
  1         22  
5 1     1   660 use Data::Dumper;
  1         6626  
  1         61  
6              
7 1     1   7 use Exporter 'import';
  1         2  
  1         61  
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   486 use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier
  1         3640  
  1         6  
14 1     1   352 use Carp;
  1         3  
  1         72  
15              
16             # ABSTRACT: Read environment variables from .env file
17              
18             our $VERSION = '0.005'; # VERSION: generated by DZP::OurPkgVersion
19              
20             use constant {
21 1         595 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   7 };
  1         1  
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 2348 my ( $shell, @vars ) = @_;
40 2         4 my $out = q{};
41 2         7 foreach my $var (@vars) {
42 4         11 $out .= _convert_variable( $shell, $var );
43 4         15 $out .= "\n";
44             }
45 2         11 return $out;
46             }
47              
48             # Private subroutines
49              
50             sub _convert_variable {
51 4     4   12 my ( $shell, $var ) = @_;
52 4 50       11 if ( exists $VAR_OUTPUT{$shell} ) {
53 4         5 return &{ $VAR_OUTPUT{$shell} }($var);
  4         9  
54             }
55             else {
56 0         0 croak "Unknown shell: $shell";
57             }
58             }
59              
60             sub _convert_var_to_sh {
61 7     7   7498 my ($var) = @_;
62             my ( $name, $value, $want_export, $allow_interpolate ) =
63 7         20 ( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, );
64 7 100       15 my $quote = $allow_interpolate ? q{"} : q{'};
65 7 100       12 if ($want_export) {
66 4         23 return sprintf "%s=$quote%s$quote; export %s", $name, $value, $name;
67             }
68             else {
69 3         19 return sprintf "%s=$quote%s$quote", $name, $value;
70             }
71             }
72              
73             sub _convert_var_to_csh {
74 3     3   3192 my ($var) = @_;
75             my ( $name, $value, $want_export, $allow_interpolate ) =
76 3         9 ( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, );
77 3 100       19 my $quote = $allow_interpolate ? q{"} : q{'};
78 3 100       20 if ($want_export) {
79 2         32 return sprintf "setenv %s $quote%s$quote", $name, $value;
80             }
81             else {
82 1         11 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__