blib/lib/Env/Dot/ScriptFunctions.pm

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