Structured/Command/config_dump.pm

filter: Statementstmt 90.7% 88 / 97 Branchbran 56.5% 26 / 46 Conditioncond 52.6% 10 / 19 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 90.0% 18 / 20 Podpod 100% 1 / 1 totaltotal 74.7% 145 / 194 CC 39 SCAR 29.6
CC
39
Cov
77%
dump_node_verbose
41.7
dump_node
13.5
stringify_value
4.4
CRAP 19.3
↑ Summary Structured.pm »
1 package Mojolicious::Plugin::Config::Structured::Command::config_dump 3.01;
2 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding UTF-8
6  
7 =head1 NAME
8  
9 Mojolicious::Plugin::Config::Structured::Command::config_dump - dump a Config::Structured configuration to text
10  
11 =head1 SYNOPSIS
12  
13 Dump a Config::Structured configuration to text
14 Usage: <APP> config-dump [--verbose] [--depth n] [--path str] [--reveal-sensitive]
15  
16 Options:
17 --verbose display all node metadata instead of just the value
18 --depth n truncate/ignore nodes below depth n (relative to real root, not --path)
19 --path str display only config nodes from str down
20 --reveal-sensitive do not obscure sensitive values in output
21  
22 =head1 DESCRIPTION
23  
24 C<config_dump> is a utility for displaying a L<Config::Structured> config that
25 has been loaded into Mojolicious as formatted text
26  
27 In its default mode, it displays each node name and the associated value. With
28 the L<--verbose|/verbose> flag, though, all node metadata is output, including
29 description, examples, notes, and where the configured value came from.
30  
31 =head1 OPTIONS
32  
33 =head2 verbose
34  
35 Display complete node metadata instead of just node names and values. All
36 supported L<Config::Structured> node metadata keys and values are included.
37  
38 =head2 path
39
40 Begin the output at the specified configuration path, ignoring configuration
41 nodes above/outside that point
42  
43 =head2 depth
44  
45 End the output at the specified configuration depth (relative to the original
46 root node -- does not regard L<--path|/path>), ignoring configuration nodes
47 below that point
48  
49 =head2 reveal-sensitive
50  
51 By default, configured values for any nodes marked C<sensitive> are obscured and
52 replaced by a string of asterisks. Use this option to show the actual values for
53 these nodes
54  
55 =cut
56  
57 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 ▶use JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 has description => 'Display Config::Structured configuration';
69 0 ▶has usage => sub ($self) {$self->extract_usage};
Subroutine __ANON__: not called CC=1 SCAR=1.2
70  
71 7 ▶my sub resolve_path($conf, $path) {
Subroutine resolve_path: called CC=4 SCAR=14.0
72 7 my @path = split(q{/}, $path);
73 7 shift(@path);
74 7 my $node_name = pop(@path);
75 7 ▶ return $conf unless ($node_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $node_name 4 3 7
76 try {
77 3 $conf = $conf->$_ foreach (@path);
78 3 ▶ return ($conf, $node_name) if ($conf->get_node($node_name));
Branch: 1 branch
BranchTrueFalseTotal
if $conf->get_node($node_name) 3 0 3
79 3 } catch ($e) {
80 }
81 0 die("'$path' is not a valid configuration path\n");
82 }
83  
84 11 ▶my sub stringify_value($value) {
Subroutine stringify_value: called CC=4 SCAR=14.9
85 11 ▶ return 'undef' unless (defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 11 11
86 11 ▶ return encode_json($value) if (ref($value));
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 11 11
87 11 ▶ return $value if (looks_like_number($value));
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($value) 0 11 11
88 11 return qq{"$value"};
89 }
90  
91 7 ▶my sub is_branch($node) {
Subroutine is_branch: called CC=1 SCAR=0.0
92 7 return eval {$node->isa('Config::Structured::Node')} # pre-5.32 safe "isa" check
93 }
94  
95 7 ▶my sub dump_node($conf, %params) {
Subroutine dump_node: called CC=11 SCAR=26.0
96 7 my ($name, $allow_sensitive, $depth, $max_depth) = @params{qw(node sensitive depth max_depth)};
97 7 ▶ $depth //= 0;
Condition100%2/2
$depth //= 0
l!l
25
MC/DC100%1/1
$depth //= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
98 7 ▶ my $at_limit = defined($max_depth) && $depth >= $max_depth;
Condition33%1/3
defined $max_depth && $depth >= $max_depth
!ll&&!rl&&r
700
MC/DC0%0/2
defined $max_depth && $depth >= $max_depth
defined $max_depth $depth >= $max_depth
Truth table33%1/3
A && B
A= defined $max_depthB= $depth >= $max_depth
ABresult
0X0
100
111
99 7 my $indent = ' ' x $depth;
100  
101 7 ▶ if (defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 3 4 7
102 3 ▶ say stringify_value($conf->$name($allow_sensitive)) and return unless (is_branch($conf->$name));
Branch: 1 branch
BranchTrueFalseTotal
unless is_branch $conf->$name 3 0 3
Condition50%2/4
is_branch($conf->$name) || say(stringify_value($conf->$name($allow_sensitive))) && (return)
l!l
03
say stringify_value($conf->$name($allow_sensitive)) and return
!ll
03
MC/DC0%0/2
is_branch($conf->$name) || say(stringify_value($conf->$name($allow_sensitive))) && (return)
is_branch($conf->$name) say stringify_value($conf->$name($allow_sensitive))
Truth table33%1/3
A || B
A= is_branch($conf->$name)B= say stringify_value($conf->$name($allow_sensitive))
ABresult
1X1
011
000
103 0 $conf = $conf->$name;
104 }
105  
106 4 ▶ my $m = (max map {length} ($conf->get_node->{leaves}->@*, $conf->get_node->{branches}->@*)) // 0;
Condition50%1/2
&max(map({length $_;} @{$conf->get_node->{"leaves"}}, @{$conf->get_node->{"branches"}})) // 0
l!l
40
MC/DC0%0/1
&max(map({length $_;} @{$conf->get_node->{"leaves"}}, @{$conf->get_node->{"branches"}})) // 0
&max(map({length $_;} @{$conf->get_node->{"leaves"}}, @{$conf->get_node->{"branches"}}))
Truth table50%1/2
A
A= &max(map({length $_;} @{$conf->get_node->{"leaves"}}, @{$conf->get_node->{"branches"}}))
Aresult
11
00
107 4 foreach (sort($conf->get_node->{leaves}->@*)) {
108 4 printf("%s%-${m}s%s%s\n", $indent, $_, ' => ', stringify_value($conf->$_($allow_sensitive)));
109 }
110 4 foreach (sort($conf->get_node->{branches}->@*)) {
111 2 ▶ printf("%s%-${m}s%s%s\n", $indent, $_, ' =>', $at_limit ? ' {...}' : '');
Branch: 1 branch
BranchTrueFalseTotal
$at_limit ? : 0 2 2
112 2 ▶ __SUB__->($conf->$_, max_depth => $max_depth, depth => $depth + 1, sensitive => $allow_sensitive) unless ($at_limit);
Branch: 1 branch
BranchTrueFalseTotal
unless $at_limit 2 0 2
113 }
114 }
115  
116 10 ▶my sub fv($label, $value = '') {
Subroutine fv: called CC=2 SCAR=6.9
117 10 printf(" %-12s%s\n", $label . ':', $value);
118 }
119  
120 8 ▶my sub dump_node_verbose($conf, %params) {
Subroutine dump_node_verbose: called CC=22 SCAR=37.3
121 8 my ($name, $allow_sensitive, $depth, $max_depth) = @params{qw(node sensitive depth max_depth)};
122 8 ▶ $depth //= 0;
Condition100%2/2
$depth //= 0
l!l
62
MC/DC100%1/1
$depth //= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
123 8 ▶ my $at_limit = defined($max_depth) && $depth >= $max_depth;
Condition33%1/3
defined $max_depth && $depth >= $max_depth
!ll&&!rl&&r
800
MC/DC0%0/2
defined $max_depth && $depth >= $max_depth
defined $max_depth $depth >= $max_depth
Truth table33%1/3
A && B
A= defined $max_depthB= $depth >= $max_depth
ABresult
0X0
100
111
124  
125 8 ▶ if (!defined($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $name) { } 4 4 8
elsif (is_branch $conf->$name) { } 0 4 4
126 4 ▶ return if (defined($max_depth) && $max_depth < $depth);
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_depth and $max_depth < $depth 0 4 4
Condition33%1/3
defined $max_depth and $max_depth < $depth
!ll&&!rl&&r
400
MC/DC0%0/2
defined $max_depth and $max_depth < $depth
defined $max_depth $max_depth < $depth
Truth table33%1/3
A and B
A= defined $max_depthB= $max_depth < $depth
ABresult
0X0
100
111
127 __SUB__->($conf, sensitive => $allow_sensitive, depth => $depth + 1, max_depth => $max_depth, node => $_)
128 4 foreach (sort $conf->get_node->{leaves}->@*);
129 __SUB__->($conf->$_, sensitive => $allow_sensitive, depth => $depth + 1, max_depth => $max_depth)
130 4 foreach (sort $conf->get_node->{branches}->@*);
131 } elsif (is_branch($conf->$name)) {
132 0 __SUB__->($conf->$name, sensitive => $allow_sensitive, depth => $depth + 1, max_depth => $max_depth);
133 } else {
134 4 my $node = $conf->get_node($name, $allow_sensitive);
135 4 my @fmt;
136 4 ▶ push(@fmt, qw(red)) if (!defined($node->{value}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{"value"} 0 4 4
137 4 ▶ push(@fmt, qw(italic)) unless ($node->{overridden});
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"overridden"} 0 4 4
138 4 ▶ @fmt = qw(reset) unless (@fmt);
Branch: 1 branch
BranchTrueFalseTotal
unless @fmt 4 0 4
139  
140 4 say colored([qw(clear)], $node->{path});
141 4 ▶ say " " . colored([qw(faint)], $node->{description}) if ($node->{description});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"description"} 0 4 4
142 4 ▶ if ($node->{notes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"notes"}) 0 4 4
143 0 my @lines = split("\n", $node->{notes});
144 0 say colored([qw(faint)], ' | ') . colored([qw(faint italic)], $_) foreach (@lines);
145 }
146 4 fv('Type', $node->{isa});
147 4 ▶ fv('Sensitive', colored([qw(green)], 'Y')) if ($node->{sensitive});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"sensitive"} 2 2 4
148 4 ▶ fv('URL', colored([qw(underline)], $node->{url})) if ($node->{url});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"url"} 0 4 4
149 4 ▶ fv('Default', colored([qw(italic)], stringify_value($node->{default}))) if ($node->{default});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"default"} 0 4 4
150 4 ▶ fv('Ref<' . ucfirst($node->{reference}->{source}) . '>', $node->{reference}->{ref}) if ($node->{reference});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"reference"} 0 4 4
151 4 fv('Value', colored([@fmt], stringify_value($node->{value})));
152 4 ▶ if ($node->{examples}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"examples"}) 0 4 4
153 0 fv('Examples');
154 0 ▶ say " " . stringify_value($_) foreach (ref($node->{examples}) eq 'ARRAY') ? $node->{examples}->@* : ($node->{examples});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'examples'} eq 'ARRAY' ? : 0 0 0
155 }
156 4 say "";
157 }
158 }
159  
160 7 ▶sub run ($self, @args) {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
161 7 my ($dump_node, $depth, $path, $sensitive) = (\&dump_node, undef, '/', 0);
162 GetOptionsFromArray(
163 \@args,
164 2 ▶ 'verbose' => sub($n, $v) {$dump_node = \&dump_node_verbose},
Subroutine __ANON__: called CC=1 SCAR=0.0
165 0 ▶ 'depth=i' => sub($n, $v) {$depth = $v - 1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 7 'path=s' => \$path,
167 'reveal-sensitive' => \$sensitive,
168 );
169 7 my ($conf, $node_name) = resolve_path($self->app->conf, $path);
170 7 $dump_node->($conf, node => $node_name, max_depth => $depth, sensitive => $sensitive);
171 }
172  
173 =pod
174  
175 =head1 EXAMPLES
176  
177 Standard:
178  
179 /app$ script/myapp config-dump
180 secrets => "************"
181 db =>
182 dsn => "dbi:mysql:host=mydb;port=3306;database=myapp_dev"
183 pass => "************"
184 user => "myapp_user"
185 migration =>
186 directory => "/schema"
187 pass => "************"
188 registry => "sqitch_myapp_dev"
189 user => "sqitch"
190  
191 At path:
192  
193 /app$ script/myapp config-dump --path /db/migration
194 directory => "/schema"
195 pass => "************"
196 registry => "sqitch_myapp_dev"
197 user => "sqitch"
198  
199 With depth limit:
200  
201 /app$ script/myapp config-dump --path /db --depth 1
202 dsn => "dbi:mysql:host=mydb;port=3306;database=myapp_dev"
203 pass => "************"
204 user => "myapp_user"
205 migration => {...}
206  
207 Without sensitive obscurement:
208  
209 /app$ script/myapp config-dump --path /db --depth 1 --reveal-sensitive
210 dsn => "dbi:mysql:host=mydb;port=3306;database=myapp_dev"
211 pass => "&xyus7#^kP**6Eeo9Yht6fU"
212 user => "devapps"
213 migration => {...}
214  
215 Verbose:
216  
217 /app$ script/myapp config-dump --verbose
218 /secrets
219 private key to encrypt session data with
220 Type: ArrayRef[Str]
221 Sensitive: Y
222 Default: ["not-very-secret"]
223 Value: "************"
224  
225 /db/dsn
226 Data Source Name for the database connection
227 Type: Str
228 URL: https://en.wikipedia.org/wiki/Data_source_name
229 Default: "dbi:mysql:host=localhost;port=3306;database=myapp"
230 Value: "dbi:mysql:host=mydb;port=3306;database=myapp_dev"
231  
232 /db/pass
233 database connection password
234 | Often passed as a file or ENV for security
235 Type: Str
236 Sensitive: Y
237 Ref<File>: /run/secrets/app_db_password
238 Value: "************"
239  
240 /db/user
241 database connection username
242 Type: Str
243 Default: "app_user"
244 Value: "myapp_user"
245  
246 /db/migration/directory
247 location of schema migration files
248 Type: Str
249 Default: "/schema"
250 Value: "/schema"
251  
252 /db/migration/pass
253 database connection password for migrations
254 | Often passed as a file or ENV for security
255 Type: Str
256 Sensitive: Y
257 Ref<File>: /run/secrets/sqitch_password
258 Value: "************"
259 ...
260  
261 =head1 AUTHOR
262  
263 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
264  
265 =head1 LICENSE
266  
267 Copyright (c) 2024 Mark Tyrrell
268  
269 Permission is hereby granted, free of charge, to any person obtaining a copy
270 of this software and associated documentation files (the "Software"), to deal
271 in the Software without restriction, including without limitation the rights
272 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
273 copies of the Software, and to permit persons to whom the Software is
274 furnished to do so, subject to the following conditions:
275  
276 The above copyright notice and this permission notice shall be included in all
277 copies or substantial portions of the Software.
278  
279 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
280 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
281 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
282 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
283 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
284 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
285 SOFTWARE.
286  
287 =cut
288  
289 1 1;
290  
291 __END__
↑ Summary Structured.pm »