| 1 | package App::optex::Config; | ||||||||||||||||||||||||||||||||
| 2 | 48 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 48 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 48 | ▶ | use TOML (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 48 | ▶ | use File::Glob qw(bsd_glob); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 48 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 48 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 48 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 48 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | # load_config($path, ...) | ||||||||||||||||||||||||||||||||
| 12 | 48 | ▶ | sub load_config { | ||||||||||||||||||||||||||||||
|
Subroutine
load_config: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 13 | 48 | my (@files) = @_; | |||||||||||||||||||||||||||||||
| 14 | 48 | ▶ | die "load_config requires file\n" unless @files; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 48 | my $merged = {}; | |||||||||||||||||||||||||||||||
| 17 | 48 | for my $file (@files) { | |||||||||||||||||||||||||||||||
| 18 | 49 | ▶ | die "load_config requires file\n" unless defined $file && length $file; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and length $file
MC/DC0%0/2
defined $file and length $file
defined $file
length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 49 | my $path = _maybe_config_path($file); | |||||||||||||||||||||||||||||||
| 21 | 49 | ▶ | next unless $path; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 19 | my %seen; | |||||||||||||||||||||||||||||||
| 24 | 19 | my $config = _load_with_include($path, \%seen, 0); | |||||||||||||||||||||||||||||||
| 25 | 18 | $merged = _merge($merged, $config); | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | 47 | return $merged; | |||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 29 | ▶ | sub _load_with_include { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_with_include: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||||||
| 31 | 29 | my ($file, $seen, $depth) = @_; | |||||||||||||||||||||||||||||||
| 32 | 29 | ▶ | die "include depth too deep ($depth)\n" if $depth > 20; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 29 | my $path = _normalize_path($file); | |||||||||||||||||||||||||||||||
| 35 | 29 | ▶ | die "config not found: $file\n" unless defined $path && -f $path; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $path and -f $path
MC/DC0%0/2
defined $path and -f $path
defined $path
-f $path
Truth table33%1/3
A and B
A= defined $pathB= -f $path
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 29 | ▶ | if ($seen->{$path}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 1 | die "include loop detected: $path\n"; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | 28 | $seen->{$path} = 1; | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 28 | ▶ | my $base = _toml_load($path) || {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
_toml_load($path) || {}
MC/DC0%0/1
_toml_load($path) || {}
_toml_load($path)
Truth table50%1/2
A
A= _toml_load($path)
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 28 | my $inc = delete $base->{include}; | |||||||||||||||||||||||||||||||
| 45 | 28 | my $merged = {}; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 28 | ▶ | if ($inc) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 10 | my @files = _expand_include($inc, dirname($path)); | |||||||||||||||||||||||||||||||
| 49 | 10 | for my $f (@files) { | |||||||||||||||||||||||||||||||
| 50 | 13 | my $child_path = _normalize_path($f); | |||||||||||||||||||||||||||||||
| 51 | 13 | ▶ | next if !defined $child_path || $child_path eq $path; # skip self include | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $child_path or $child_path eq $path
MC/DC50%1/2
not defined $child_path or $child_path eq $path
not defined $child_path
$child_path eq $path
Truth table66%2/3
not(A) or B
A= not defined $child_pathB= $child_path eq $path
| |||||||||||||||||||||||||||||||||
| 52 | 10 | my $child = _load_with_include($child_path, $seen, $depth + 1); | |||||||||||||||||||||||||||||||
| 53 | 7 | $merged = _merge($merged, $child); | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | 25 | $merged = _merge($merged, $base); | |||||||||||||||||||||||||||||||
| 57 | 25 | delete $seen->{$path}; | |||||||||||||||||||||||||||||||
| 58 | 25 | return $merged; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 28 | ▶ | sub _toml_load { | ||||||||||||||||||||||||||||||
|
Subroutine
_toml_load: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 62 | 28 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 63 | 28 | my $txt = _slurp($path); | |||||||||||||||||||||||||||||||
| 64 | 28 | my ($data, $err) = TOML::from_toml($txt); | |||||||||||||||||||||||||||||||
| 65 | 28 | ▶ | die "$path: $err\n" unless $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 28 | return $data; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 28 | ▶ | sub _slurp { | ||||||||||||||||||||||||||||||
|
Subroutine
_slurp: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 70 | 28 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 71 | 28 | ▶ | open my $fh, '<:encoding(UTF-8)', $path or die "open $path: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 28 | local $/; | |||||||||||||||||||||||||||||||
| 73 | 28 | my $txt = <$fh>; | |||||||||||||||||||||||||||||||
| 74 | 28 | close $fh; | |||||||||||||||||||||||||||||||
| 75 | 28 | return $txt; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 10 | ▶ | sub _expand_include { | ||||||||||||||||||||||||||||||
|
Subroutine
_expand_include: called CC=10 SCAR=26.2
| |||||||||||||||||||||||||||||||||
| 79 | 10 | my ($inc, $base_dir) = @_; | |||||||||||||||||||||||||||||||
| 80 | 10 | my @items; | |||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | my $t = reftype($inc) || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
builtin::reftype($inc) || ''
MC/DC0%0/1
builtin::reftype($inc) || ''
builtin::reftype($inc)
Truth table50%1/2
A
A= builtin::reftype($inc)
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 10 | ▶ | if (!$t) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 84 | 10 | @items = ($inc); | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | elsif ($t eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
| 87 | 0 | @items = @$inc; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||||||
| 90 | 0 | die "include must be string or array\n"; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 10 | my @files; | |||||||||||||||||||||||||||||||
| 94 | 10 | for my $item (@items) { | |||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | next unless defined $item && length $item; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $item and length $item
MC/DC0%0/2
defined $item and length $item
defined $item
length $item
Truth table33%1/3
A and B
A= defined $itemB= length $item
| |||||||||||||||||||||||||||||||||
| 96 | 10 | $item = _expand_tilde($item); | |||||||||||||||||||||||||||||||
| 97 | 10 | ▶ | if ($base_dir && !File::Spec->file_name_is_absolute($item)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$base_dir and not "File::Spec"->file_name_is_absolute($item)
MC/DC50%1/2
$base_dir and not "File::Spec"->file_name_is_absolute($item)
$base_dir
not "File::Spec"->file_name_is_absolute($item)
Truth table66%2/3
A and not(B)
A= $base_dirB= not "File::Spec"->file_name_is_absolute($item)
| |||||||||||||||||||||||||||||||||
| 98 | 3 | $item = File::Spec->rel2abs($item, $base_dir); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 10 | my @g = bsd_glob($item, File::Glob::GLOB_TILDE | File::Glob::GLOB_NOSORT); | |||||||||||||||||||||||||||||||
| 102 | 10 | ▶ | if (@g) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 10 | push @files, sort @g; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||||||
| 106 | 0 | push @files, $item; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 10 | return @files; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 101 | ▶ | sub _expand_tilde { | ||||||||||||||||||||||||||||||
|
Subroutine
_expand_tilde: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||
| 113 | 101 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 114 | 101 | ▶ | return $path unless $path =~ /^~/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 4 | ▶ | my $home = $ENV{HOME} || (getpwuid($<))[7] || ''; | ||||||||||||||||||||||||||||||
|
Condition40%2/5
$ENV{'HOME'} || (getpwuid $<)[7] || ''
$ENV{"HOME"} or (getpwuid $<)[7]
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7] || ''
$ENV{"HOME"}
(getpwuid $<)[7]
Truth table33%1/3
A or B
A= $ENV{"HOME"}B= (getpwuid $<)[7]
| |||||||||||||||||||||||||||||||||
| 116 | 4 | $path =~ s/^~/$home/; | |||||||||||||||||||||||||||||||
| 117 | 4 | return $path; | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 91 | ▶ | sub _normalize_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_normalize_path: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||
| 121 | 91 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 122 | 91 | $path = _expand_tilde($path); | |||||||||||||||||||||||||||||||
| 123 | 91 | ▶ | my $abs = File::Spec->file_name_is_absolute($path) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | ? $path | ||||||||||||||||||||||||||||||||
| 125 | : File::Spec->rel2abs($path); | ||||||||||||||||||||||||||||||||
| 126 | 91 | ▶ | return abs_path($abs) || $abs; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
abs_path($abs) || $abs
MC/DC0%0/2
abs_path($abs) || $abs
abs_path($abs)
$abs
Truth table66%2/3
A || B
A= abs_path($abs)B= $abs
| |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 49 | ▶ | sub _maybe_config_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_maybe_config_path: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 130 | 49 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 131 | 49 | my $norm = _normalize_path($path); | |||||||||||||||||||||||||||||||
| 132 | 49 | ▶ | return unless defined $norm && -f $norm; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $norm and -f $norm
MC/DC50%1/2
defined $norm and -f $norm
defined $norm
-f $norm
Truth table66%2/3
A and B
A= defined $normB= -f $norm
| |||||||||||||||||||||||||||||||||
| 133 | 19 | return $norm; | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 55 | ▶ | sub _merge { | ||||||||||||||||||||||||||||||
|
Subroutine
_merge: called CC=10 SCAR=24.0
| |||||||||||||||||||||||||||||||||
| 137 | 55 | my ($a, $b) = @_; | |||||||||||||||||||||||||||||||
| 138 | 55 | ▶ | $a ||= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$a ||= {}
MC/DC0%0/1
$a ||= {}
$a
Truth table50%1/2
A
A= $a
| |||||||||||||||||||||||||||||||||
| 139 | 55 | ▶ | $b ||= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$b ||= {}
MC/DC0%0/1
$b ||= {}
$b
Truth table50%1/2
A
A= $b
| |||||||||||||||||||||||||||||||||
| 140 | 55 | my %out = %$a; | |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 55 | for my $k (keys %$b) { | |||||||||||||||||||||||||||||||
| 143 | 71 | my $va = $out{$k}; | |||||||||||||||||||||||||||||||
| 144 | 71 | my $vb = $b->{$k}; | |||||||||||||||||||||||||||||||
| 145 | 71 | ▶ | my $ta = reftype($va) || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
builtin::reftype($va) || ''
MC/DC100%1/1
builtin::reftype($va) || ''
builtin::reftype($va)
Truth table100%2/2
A
A= builtin::reftype($va)
| |||||||||||||||||||||||||||||||||
| 146 | 71 | ▶ | my $tb = reftype($vb) || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
builtin::reftype($vb) || ''
MC/DC100%1/1
builtin::reftype($vb) || ''
builtin::reftype($vb)
Truth table100%2/2
A
A= builtin::reftype($vb)
| |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 71 | ▶ | if ($ta eq 'HASH' && $tb eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$ta eq "HASH" and $tb eq "HASH"
MC/DC50%1/2
$ta eq "HASH" and $tb eq "HASH"
$ta eq "HASH"
$tb eq "HASH"
Truth table66%2/3
A and B
A= $ta eq "HASH"B= $tb eq "HASH"
| |||||||||||||||||||||||||||||||||
| 149 | 5 | $out{$k} = _merge($va, $vb); | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | ▶ | elsif ($ta eq 'ARRAY' && $tb eq 'ARRAY') { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$ta eq "ARRAY" and $tb eq "ARRAY"
MC/DC0%0/2
$ta eq "ARRAY" and $tb eq "ARRAY"
$ta eq "ARRAY"
$tb eq "ARRAY"
Truth table33%1/3
A and B
A= $ta eq "ARRAY"B= $tb eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 152 | 0 | $out{$k} = [ @$va, @$vb ]; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||||||
| 155 | 66 | $out{$k} = $vb; | |||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | 55 | return \%out; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 48 | 1; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =pod | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | =encoding utf8 | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | App::optex::Config - load optex config with recursive include support | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | use App::optex::Config; | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | my $config = App::optex::Config::load_config("$ENV{HOME}/.optex.d/config.toml"); | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | =head2 load_config | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | my $config = App::optex::Config::load_config($path, ...); | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | Loads a TOML config file and expands any top-level C<include> directives. | ||||||||||||||||||||||||||||||||
| 184 | Returns a hashref of the merged configuration. | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||