File Coverage

blib/lib/Env/Dot/ScriptFunctions.pm
Criterion Covered Total %
statement 40 45 88.8
branch 9 12 75.0
condition n/a
subroutine 10 11 90.9
pod 1 1 100.0
total 60 69 86.9


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