| 1 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||||||||||||||||
| 2 | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | ||||||||||||||||||||||||||||
| 3 | package Env::Assert; | ||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Ensure that the environment variables match what you need, or abort. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '0.018'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # We define our own import routine because | ||||||||||||||||||||||||||||
| 13 | # this is the point (when `use Env::Assert` is called) | ||||||||||||||||||||||||||||
| 14 | # when we do our magic. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use open ':std', IO => ':encoding(UTF-8)'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use Env::Assert::Functions qw( :all ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant { ENV_DESC_FILENAME => '.envdesc', }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Handle exports | ||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | 3 | ▶ | no warnings 'redefine'; ## no critic [TestingAndDebugging::ProhibitNoWarnings] | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 30 | 5 | my ( $class, $cmd, $args ) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # We also allow only: 'use Env::Assert;' | ||||||||||||||||||||||||||||
| 33 | 5 | ▶ | croak "Unknown argument '$cmd'" if ( $cmd && $cmd ne 'assert' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cmd and $cmd ne "assert"
MC/DC100%2/2
$cmd and $cmd ne "assert"
$cmd
$cmd ne "assert"
Truth table100%3/3
A and B
A= $cmdB= $cmd ne "assert"
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | if ( !assert_env( %{$args} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | croak 'Errors in environment detected.'; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 3 | return; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =pod | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =encoding utf8 | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =for :stopwords env filepath filepaths | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 NAME | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Env::Assert - Ensure that the environment variables match what you need, or abort | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 VERSION | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | version 0.018 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =for test_synopsis BEGIN { die 'SKIP: no .envdesc file here' } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | use Env::Assert 'assert'; | ||||||||||||||||||||||||||||
| 61 | # or: | ||||||||||||||||||||||||||||
| 62 | use Env::Assert assert => { | ||||||||||||||||||||||||||||
| 63 | envdesc_file => 'another-envdesc', | ||||||||||||||||||||||||||||
| 64 | break_at_first_error => 1, | ||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # .envdesc file: | ||||||||||||||||||||||||||||
| 68 | # MY_VAR=.+ | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # use any verified environment variable | ||||||||||||||||||||||||||||
| 71 | say $ENV{MY_VAR}; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # You can inline the envdesc file: | ||||||||||||||||||||||||||||
| 74 | use Env::Assert assert => { | ||||||||||||||||||||||||||||
| 75 | exact => 1, | ||||||||||||||||||||||||||||
| 76 | envdesc => <<'EOF' | ||||||||||||||||||||||||||||
| 77 | NUMERIC_VAR=^[[:digit:]]+$ | ||||||||||||||||||||||||||||
| 78 | TIME_VAR=^\d{2}:\d{2}:\d{2}$ | ||||||||||||||||||||||||||||
| 79 | EOF | ||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 STATUS | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Package Env::Assert is currently being developed so changes in the API are possible, | ||||||||||||||||||||||||||||
| 85 | though not likely. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 NOTES | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Functionality of L<Env::Assert> has been moved to module L<Env::Assert::Functions> since version 0.013. | ||||||||||||||||||||||||||||
| 90 | L<Env::Assert> has a different API now. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 METHODS | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 assert_env | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Read environment description, F<.envdesc> by default, | ||||||||||||||||||||||||||||
| 97 | and compare current environment. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | ▶ | sub assert_env { | ||||||||||||||||||||||||||
|
Subroutine
assert_env: called CC=13 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 4 | my (%args) = @_; | |||||||||||||||||||||||||||
| 103 | 4 | local $OUTPUT_AUTOFLUSH = 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | my $break_at_first_error = $args{'break_at_first_error'} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"break_at_first_error"} // 0
MC/DC100%1/1
$args{"break_at_first_error"} // 0
$args{"break_at_first_error"}
Truth table100%2/2
A
A= $args{"break_at_first_error"}
| |||||||||||||||||||||||||||||
| 106 | 4 | ▶ | my $exact = $args{'exact'} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"exact"} // 0
MC/DC0%0/1
$args{"exact"} // 0
$args{"exact"}
Truth table50%1/2
A
A= $args{"exact"}
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | my @env_desc_rows; | |||||||||||||||||||||||||||
| 109 | 4 | ▶ | if ( $args{'envdesc'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | my $content = $args{'envdesc'}; | |||||||||||||||||||||||||||
| 111 | 1 | ▶ | open my $fh, q{<}, \$content | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | or croak 'Cannot open scalar envdesc content'; | ||||||||||||||||||||||||||||
| 113 | 1 | @env_desc_rows = <$fh>; | |||||||||||||||||||||||||||
| 114 | 1 | ▶ | close $fh or croak 'Cannot close scalar envdesc content'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||
| 116 | 3 | ▶ | my $env_desc_filename = $args{'envdesc_file'} // ENV_DESC_FILENAME; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"envdesc_file"} // ".envdesc"
MC/DC100%1/1
$args{"envdesc_file"} // ".envdesc"
$args{"envdesc_file"}
Truth table100%2/2
A
A= $args{"envdesc_file"}
| |||||||||||||||||||||||||||||
| 117 | 3 | ▶ | open my $fh, q{<}, $env_desc_filename or croak "Cannot open file '$env_desc_filename'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 3 | @env_desc_rows = <$fh>; | |||||||||||||||||||||||||||
| 119 | 3 | ▶ | close $fh or croak "Cannot close file '$env_desc_filename'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | my %desc = file_to_desc(@env_desc_rows); | |||||||||||||||||||||||||||
| 123 | 4 | my %parameters; | |||||||||||||||||||||||||||
| 124 | 4 | ▶ | $parameters{'break_at_first_error'} = $break_at_first_error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | if defined $break_at_first_error; | ||||||||||||||||||||||||||||
| 126 | 4 | ▶ | $desc{'options'}->{OPTION_ENV_EXACT} = $exact | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | if defined $exact; | ||||||||||||||||||||||||||||
| 128 | 4 | my $r = assert( \%ENV, \%desc, \%parameters ); | |||||||||||||||||||||||||||
| 129 | 4 | ▶ | if ( !$r->{'success'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | print {*STDERR} report_errors( $r->{'errors'} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | or croak 'Cannot print errors to STDERR'; | ||||||||||||||||||||||||||||
| 132 | 1 | return 0; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 3 | return 1; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | No external dependencies outside Perl's standard distribution. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | L<Env::Dot> is a "sister" to Env::Assert. | ||||||||||||||||||||||||||||
| 144 | Read environment variables from a F<.env> file directly into you program. | ||||||||||||||||||||||||||||
| 145 | There is also script F<envdot> which can turn F<.env> file's content | ||||||||||||||||||||||||||||
| 146 | into environment variables for different shells. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Mikko Koivunalho <mikkoi@cpan.org> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | This software is copyright (c) 2023 by Mikko Koivunalho. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 157 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | 1; | |||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||