| 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.023_08'; # TRIAL | |||||||||
| 19 | 9 | $VERSION =~ tr/_//d; | |||||||||
| 20 | |||||||||||
| 21 | 9 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 9 | OPTION_FILE_TYPE => q{file:type}, | |||||||||
| 23 | OPTION_FILE_TYPE_PLAIN => q{plain}, | ||||||||||
| 24 | OPTION_FILE_TYPE_SHELL => q{shell}, | ||||||||||
| 25 | DEFAULT_OPTION_FILE_TYPE => q{shell}, | ||||||||||
| 26 | 9 | }; | |||||||||
| 27 | |||||||||||
| 28 | 9 | my %DOTENV_OPTIONS = ( | |||||||||
| 29 | 'file:type' => 1, | ||||||||||
| 30 | 'var:allow_interpolate' => 1, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 9 | my %VAR_OUTPUT = ( | |||||||||
| 34 | q{sh} => \&_convert_var_to_sh, | ||||||||||
| 35 | q{csh} => \&_convert_var_to_csh, | ||||||||||
| 36 | q{fish} => \&_convert_var_to_fish, | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | =pod | ||||||||||
| 40 | |||||||||||
| 41 | =encoding utf8 | ||||||||||
| 42 | |||||||||||
| 43 | =for stopwords envdot env | ||||||||||
| 44 | |||||||||||
| 45 | =head1 NAME | ||||||||||
| 46 | |||||||||||
| 47 | Env::Dot::ScriptFunctions - Read environment variables from a .env file, functions for the binary script | ||||||||||
| 48 | |||||||||||
| 49 | =head1 STATUS | ||||||||||
| 50 | |||||||||||
| 51 | This module is currently being developed so changes in the API are possible, | ||||||||||
| 52 | though not likely. | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||
| 56 | |||||||||||
| 57 | use Env::Dot::ScriptFunctions qw( convert_variables_into_commands ); | ||||||||||
| 58 | |||||||||||
| 59 | =head1 DESCRIPTION | ||||||||||
| 60 | |||||||||||
| 61 | =for stopwords envdot | ||||||||||
| 62 | |||||||||||
| 63 | This package just contains functions for use | ||||||||||
| 64 | in the main package L<Env::Dot> and in | ||||||||||
| 65 | the command line tool B<envdot>. | ||||||||||
| 66 | |||||||||||
| 67 | =head1 FUNCTIONS | ||||||||||
| 68 | |||||||||||
| 69 | No functions are automatically exported to the calling namespace. | ||||||||||
| 70 | |||||||||||
| 71 | =head2 convert_variables_into_commands() | ||||||||||
| 72 | |||||||||||
| 73 | # Return all variables from the F<.env> file | ||||||||||
| 74 | # as a list of hashes (name/value pairs). | ||||||||||
| 75 | # This list is created in the same order the variables | ||||||||||
| 76 | # are read from the files and may therefore contain | ||||||||||
| 77 | # the same variable several times. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 7 | ▶ | sub convert_variables_into_commands { | ||||||||
|
Subroutine
convert_variables_into_commands: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 7 | my ( $shell, @vars ) = @_; | |||||||||
| 83 | 7 | my $out = q{}; | |||||||||
| 84 | 7 | foreach my $variable (@vars) { | |||||||||
| 85 | 17 | $out .= _convert_variable( $shell, $variable ); | |||||||||
| 86 | 16 | $out .= "\n"; | |||||||||
| 87 | } | ||||||||||
| 88 | 6 | return $out; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | # Private subroutines | ||||||||||
| 92 | |||||||||||
| 93 | 17 | ▶ | sub _convert_variable { | ||||||||
|
Subroutine
_convert_variable: called CC=2 SCAR=6.9
| |||||||||||
| 94 | 17 | my ( $shell, $variable ) = @_; | |||||||||
| 95 | 17 | ▶ | if ( exists $VAR_OUTPUT{$shell} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 16 | return &{ $VAR_OUTPUT{$shell} }($variable); | |||||||||
| 97 | } | ||||||||||
| 98 | else { | ||||||||||
| 99 | 1 | croak "Unknown shell: $shell"; | |||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 19 | ▶ | sub _convert_var_to_sh { | ||||||||
|
Subroutine
_convert_var_to_sh: called CC=3 SCAR=11.0
| |||||||||||
| 104 | 19 | my ($variable) = @_; | |||||||||
| 105 | my ( $name, $value, $want_export, $allow_interpolate ) = | ||||||||||
| 106 | 19 | ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, ); | |||||||||
| 107 | 19 | ▶ | my $quote = $allow_interpolate ? q{"} : q{'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 19 | ▶ | if ($want_export) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 16 | return sprintf "%s=$quote%s$quote; export %s", $name, $value, $name; | |||||||||
| 110 | } | ||||||||||
| 111 | else { | ||||||||||
| 112 | 3 | return sprintf "%s=$quote%s$quote", $name, $value; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 3 | ▶ | sub _convert_var_to_csh { | ||||||||
|
Subroutine
_convert_var_to_csh: called CC=3 SCAR=11.0
| |||||||||||
| 117 | 3 | my ($variable) = @_; | |||||||||
| 118 | my ( $name, $value, $want_export, $allow_interpolate ) = | ||||||||||
| 119 | 3 | ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, ); | |||||||||
| 120 | 3 | ▶ | my $quote = $allow_interpolate ? q{"} : q{'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 3 | ▶ | if ($want_export) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 2 | return sprintf "setenv %s $quote%s$quote", $name, $value; | |||||||||
| 123 | } | ||||||||||
| 124 | else { | ||||||||||
| 125 | 1 | return sprintf "set %s $quote%s$quote", $name, $value; | |||||||||
| 126 | } | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 3 | ▶ | sub _convert_var_to_fish { | ||||||||
|
Subroutine
_convert_var_to_fish: called CC=3 SCAR=11.0
| |||||||||||
| 130 | 3 | my ($variable) = @_; | |||||||||
| 131 | my ( $name, $value, $want_export, $allow_interpolate ) = | ||||||||||
| 132 | 3 | ( $variable->{'name'}, $variable->{'value'}, $variable->{'opts'}->{'export'}, $variable->{'opts'}->{'allow_interpolate'}, ); | |||||||||
| 133 | 3 | ▶ | my $quote = $allow_interpolate ? q{"} : q{'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 3 | ▶ | my $export_flag = $want_export ? q{--export} : q{--unexport}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 3 | return sprintf "set --erase $export_flag --universal %s $quote%s$quote", $name, $value; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | =pod | ||||||||||
| 139 | |||||||||||
| 140 | =head1 AUTHOR | ||||||||||
| 141 | |||||||||||
| 142 | Mikko Koivunalho <mikkoi@cpan.org> | ||||||||||
| 143 | |||||||||||
| 144 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 145 | |||||||||||
| 146 | This software is copyright (c) 2023 by Mikko Koivunalho. | ||||||||||
| 147 | |||||||||||
| 148 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 149 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 150 | |||||||||||
| 151 | =cut | ||||||||||
| 152 | |||||||||||
| 153 | 9 | 1; | |||||||||
| 154 | __END__ | ||||||||||