File Coverage

blib/lib/Env/Dot/ScriptFunctions.pm
Criterion Covered Total %
statement 42 47 89.3
branch 9 12 75.0
condition n/a
subroutine 11 12 91.6
pod 1 1 100.0
total 63 72 87.5


line stmt bran cond sub pod time code
1             ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
2             package Env::Dot::ScriptFunctions;
3 9     9   203336 use strict;
  9         20  
  9         354  
4 9     9   42 use warnings;
  9         21  
  9         551  
5 9     9   161 use 5.010;
  9         33  
6              
7 9     9   46 use Exporter 'import';
  9         31  
  9         725  
8             our @EXPORT_OK = qw(
9             convert_variables_into_commands
10             );
11             our %EXPORT_TAGS = ( 'all' => [qw( convert_variables_into_commands )], );
12              
13 9     9   691 use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier
  9         4601  
  9         89  
14 9     9   6403 use Carp;
  9         17  
  9         840  
15              
16             # ABSTRACT: Read environment variables from a .env file
17              
18             our $VERSION = '0.020';
19              
20             use constant {
21 9         6728 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 9     9   174 };
  9         72  
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 6     6 1 3103 my ( $shell, @vars ) = @_;
40 6         15 my $out = q{};
41 6         18 foreach my $var (@vars) {
42 16         51 $out .= _convert_variable( $shell, $var );
43 16         38 $out .= "\n";
44             }
45 6         280 return $out;
46             }
47              
48             # Private subroutines
49              
50             sub _convert_variable {
51 16     16   50 my ( $shell, $var ) = @_;
52 16 50       61 if ( exists $VAR_OUTPUT{$shell} ) {
53 16         37 return &{ $VAR_OUTPUT{$shell} }($var);
  16         77  
54             }
55             else {
56 0         0 croak "Unknown shell: $shell";
57             }
58             }
59              
60             sub _convert_var_to_sh {
61 19     19   303243 my ($var) = @_;
62             my ( $name, $value, $want_export, $allow_interpolate ) =
63 19         67 ( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, );
64 19 100       50 my $quote = $allow_interpolate ? q{"} : q{'};
65 19 100       39 if ($want_export) {
66 16         121 return sprintf "%s=$quote%s$quote; export %s", $name, $value, $name;
67             }
68             else {
69 3         18 return sprintf "%s=$quote%s$quote", $name, $value;
70             }
71             }
72              
73             sub _convert_var_to_csh {
74 3     3   3939 my ($var) = @_;
75             my ( $name, $value, $want_export, $allow_interpolate ) =
76 3         14 ( $var->{'name'}, $var->{'value'}, $var->{'opts'}->{'export'}, $var->{'opts'}->{'allow_interpolate'}, );
77 3 100       10 my $quote = $allow_interpolate ? q{"} : q{'};
78 3 100       8 if ($want_export) {
79 2         13 return sprintf "setenv %s $quote%s$quote", $name, $value;
80             }
81             else {
82 1         9 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__
97              
98             =pod
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             Env::Dot::ScriptFunctions - Read environment variables from a .env file
105              
106             =head1 VERSION
107              
108             version 0.020
109              
110             =head1 SYNOPSIS
111              
112             use Env::Dot::ScriptFunctions qw( convert_variables_into_commands );
113              
114             =head1 DESCRIPTION
115              
116             =for stopwords envdot env
117              
118             =head1 STATUS
119              
120             This module is currently being developed so changes in the API are possible,
121             though not likely.
122              
123             =for stopwords envdot
124              
125             This package just contains functions for use
126             in the main package L<Env::Dot> and in
127             the command line tool B<envdot>.
128              
129             =head1 FUNCTIONS
130              
131             No functions are automatically exported to the calling namespace.
132              
133             =head2 convert_variables_into_commands()
134              
135             # Return all variables from the F<.env> file
136             # as a list of hashes (name/value pairs).
137             # This list is created in the same order the variables
138             # are read from the files and may therefore contain
139             # the same variable several times.
140              
141             =head1 AUTHOR
142              
143             Mikko Koivunalho <mikkoi@cpan.org>
144              
145             =head1 COPYRIGHT AND LICENSE
146              
147             This software is copyright (c) 2023 by Mikko Koivunalho.
148              
149             This is free software; you can redistribute it and/or modify it under
150             the same terms as the Perl 5 programming language system itself.
151              
152             =cut