line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- perl -*- |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Author: Slaven Rezic |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright (C) 2023 Slaven Rezic. All rights reserved. |
7
|
|
|
|
|
|
|
# This package is free software; you can redistribute it and/or |
8
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
package YAML::PP::Schema::Env; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
4157
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
14
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
5
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
511
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub register { |
21
|
5
|
|
|
5
|
0
|
8707
|
my ($self, %args) = @_; |
22
|
5
|
|
|
|
|
11
|
my $schema = $args{schema}; |
23
|
|
|
|
|
|
|
|
24
|
5
|
|
|
|
|
8
|
my $options = $args{options}; |
25
|
5
|
|
|
|
|
7
|
my $default_value; |
26
|
5
|
|
|
|
|
9
|
my $default_separator = ':'; |
27
|
5
|
|
|
|
|
9
|
for my $opt (@$options) { |
28
|
6
|
100
|
|
|
|
43
|
if ($opt =~ m{^defval=(.*)$}) { |
|
|
50
|
|
|
|
|
|
29
|
4
|
|
|
|
|
12
|
$default_value = $1; |
30
|
|
|
|
|
|
|
} elsif ($opt =~ m{defsep=(.*)$}) { |
31
|
2
|
|
|
|
|
16
|
$default_separator = $1; |
32
|
|
|
|
|
|
|
} else { |
33
|
0
|
|
|
|
|
0
|
croak "Invalid option for ENV Schema: '$opt'"; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$schema->add_resolver( |
38
|
|
|
|
|
|
|
tag => '!ENV', |
39
|
|
|
|
|
|
|
match => [ all => sub { |
40
|
28
|
|
|
28
|
|
73215
|
my ($constructor, $event) = @_; |
41
|
28
|
|
|
|
|
194
|
(my $val = $event->{value}) =~ s{\$(\{.*?\})}{ |
42
|
37
|
|
|
|
|
107
|
my $capture = $1; |
43
|
37
|
50
|
|
|
|
341
|
if ($capture =~ m{\{(.*?)(?:\Q$default_separator\E(.*))?\}$}) { |
44
|
37
|
|
|
|
|
118
|
my($this_env, $this_default_value) = ($1, $2); |
45
|
37
|
|
|
|
|
55
|
my $this_value; |
46
|
37
|
100
|
|
|
|
89
|
if (!exists $ENV{$this_env}) { |
47
|
23
|
100
|
|
|
|
48
|
if (defined $this_default_value) { |
|
|
100
|
|
|
|
|
|
48
|
9
|
|
|
|
|
16
|
$this_value = $this_default_value; |
49
|
|
|
|
|
|
|
} elsif (defined $default_value) { |
50
|
12
|
|
|
|
|
20
|
$this_value = $default_value; |
51
|
|
|
|
|
|
|
} else { |
52
|
2
|
|
|
|
|
370
|
croak "There's no environment variable '$this_env', and no global or local default value was configured"; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} else { |
55
|
14
|
|
|
|
|
27
|
$this_value = $ENV{$this_env}; |
56
|
|
|
|
|
|
|
} |
57
|
35
|
|
|
|
|
126
|
$this_value; |
58
|
|
|
|
|
|
|
} else { |
59
|
0
|
|
|
|
|
0
|
croak "Unexpected error: cannot parse '$capture'"; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
}ge; |
62
|
26
|
|
|
|
|
99
|
return $val; |
63
|
5
|
|
|
|
|
86
|
}], |
64
|
|
|
|
|
|
|
implicit => 0, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |