| 1 | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | ||||||||||
| 2 | package Env::Dot::ScriptFunctions; | ||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 9 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 9 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 9 | our @EXPORT_OK = qw( | |||||||||
| 9 | convert_variables_into_commands | ||||||||||
| 10 | ); | ||||||||||
| 11 | 9 | our %EXPORT_TAGS = ( 'all' => [qw( convert_variables_into_commands )], ); | |||||||||
| 12 | |||||||||||
| 13 | 9 | ▶ | use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | # ABSTRACT: Read environment variables from a .env file | ||||||||||
| 17 | |||||||||||
| 18 | 9 | our $VERSION = '0.022'; | |||||||||
| 19 | |||||||||||
| 20 | use constant { | ||||||||||
| 21 | 9 | 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 | ▶ | }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 9 | my %DOTENV_OPTIONS = ( | |||||||||
| 28 | 'file:type' => 1, | ||||||||||
| 29 | 'var:allow_interpolate' => 1, | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 9 | 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 | ▶ | my ( $shell, @vars ) = @_; | ||||||||
|
Subroutine
convert_variables_into_commands: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | 6 | my $out = q{}; | |||||||||
| 41 | 6 | foreach my $variable (@vars) { | |||||||||
| 42 | 16 | $out .= _convert_variable( $shell, $variable ); | |||||||||
| 43 | 16 | $out .= "\n"; | |||||||||
| 44 | } | ||||||||||
| 45 | 6 | return $out; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | # Private subroutines | ||||||||||
| 49 | |||||||||||
| 50 | sub _convert_variable { | ||||||||||
| 51 | 16 | ▶ | my ( $shell, $variable ) = @_; | ||||||||
|
Subroutine
_convert_variable: called CC=2 SCAR=7.4
| |||||||||||
| 52 | 16 | ▶ | if ( exists $VAR_OUTPUT{$shell} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 16 | return &{ $VAR_OUTPUT{$shell} }($variable); | |||||||||
| 54 | } | ||||||||||
| 55 | else { | ||||||||||
| 56 | 0 | croak "Unknown shell: $shell"; | |||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | sub _convert_var_to_sh { | ||||||||||
| 61 | 19 | ▶ | my ($variable) = @_; | ||||||||
|
Subroutine
_convert_var_to_sh: called CC=3 SCAR=11.0
| |||||||||||
| 62 | my ( $name, $value, $want_export, $allow_interpolate ) = | ||||||||||
| 63 | 19 | ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, ); | |||||||||
| 64 | 19 | ▶ | my $quote = $allow_interpolate ? q{"} : q{'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 19 | ▶ | if ($want_export) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 16 | return sprintf "%s=$quote%s$quote; export %s", $name, $value, $name; | |||||||||
| 67 | } | ||||||||||
| 68 | else { | ||||||||||
| 69 | 3 | return sprintf "%s=$quote%s$quote", $name, $value; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub _convert_var_to_csh { | ||||||||||
| 74 | 3 | ▶ | my ($variable) = @_; | ||||||||
|
Subroutine
_convert_var_to_csh: called CC=3 SCAR=11.0
| |||||||||||
| 75 | my ( $name, $value, $want_export, $allow_interpolate ) = | ||||||||||
| 76 | 3 | ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, ); | |||||||||
| 77 | 3 | ▶ | my $quote = $allow_interpolate ? q{"} : q{'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 3 | ▶ | if ($want_export) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 2 | return sprintf "setenv %s $quote%s$quote", $name, $value; | |||||||||
| 80 | } | ||||||||||
| 81 | else { | ||||||||||
| 82 | 1 | return sprintf "set %s $quote%s$quote", $name, $value; | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | sub _convert_var_to_fish { | ||||||||||
| 87 | 0 | ▶ | my ($variable) = @_; | ||||||||
|
Subroutine
_convert_var_to_fish: not called CC=2 SCAR=17.9
| |||||||||||
| 88 | my ( $name, $value, $want_export, $allow_interpolate ) = | ||||||||||
| 89 | 0 | ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, ); | |||||||||
| 90 | 0 | ▶ | my $quote = $allow_interpolate ? q{"} : q{'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | return sprintf "set -e %s; set -x -U %s $quote%s$quote", $name, $name, $value; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 9 | 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.022 | ||||||||||
| 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 | ||||||||||