blib/lib/YAML/yq/Helper.pm

filter: Statementstmt 4.6% 16 / 343 Branchbran 0.0% 0 / 222 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.8% 5 / 24 Podpod 94.7% 18 / 19 totaltotal 6.2% 39 / 620 SCAR 94.5
CC
117
Cov
3%
set_hash
210.0
set_in_array
210.0
dedup_array
110.0
CRAP 12691.0
↑ Summary
1 package YAML::yq::Helper;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Slurp qw (read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 YAML::yq::Helper - Wrapper for yq for various common tasks so YAML files can be manipulated in a manner to preserve comments and version header.
12  
13 =head1 VERSION
14  
15 Version 0.1.1
16  
17 =cut
18  
19 1 our $VERSION = '0.1.1';
20  
21 =head1 SYNOPSIS
22  
23 use YAML::yq::Helper;
24  
25 my $yq = YAML::yq::Helper->new(file='/etc/suricata/suricata-ids.yaml');
26  
27 $yq->set_array(var=>'rule-files', vals=>['suricata.rules','custom.rules'])
28  
29 =head1 METHODS
30  
31 =head2 new
32  
33 Inits the object and check if a version header is present for use with the
34 ensure method.
35  
36 Will make sure the file specified exists, is a file, is readable, and is
37 writable. Otherwise it will die.
38  
39 Will also die if yq is not in the path.
40  
41 - file :: The YAML file to operate on.
42  
43 =cut
44  
45 sub new {
46 0 ▶ my ( $blank, %opts ) = @_;
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
47  
48 0 my $exists = `/bin/sh -c "which yq"`;
49 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
50 0 die("yq not found in the path");
51 }
52  
53 0 ▶ if ( !defined( $opts{file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"file"}) 0 0 0
54 0 die('No file specified');
55 }
56  
57 0 ▶ if ( !-e $opts{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $opts{"file"}) 0 0 0
58 0 die( '"' . $opts{file} . '" does not exist' );
59 }
60  
61 0 ▶ if ( !-f $opts{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $opts{"file"}) 0 0 0
62 0 die( '"' . $opts{file} . '" is not a file' );
63 }
64  
65 0 ▶ if ( !-r $opts{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $opts{"file"}) 0 0 0
66 0 die( '"' . $opts{file} . '" is not readable' );
67 }
68  
69 my $self = {
70 file => $opts{file},
71 0 qfile => quotemeta( $opts{file} ),
72 ensure => 0,
73 ver => undef,
74 };
75 0 bless $self;
76  
77 0 my $raw = read_file( $self->{file} );
78 0 ▶ if ( $raw =~ /^\%YAML\ 1\.1/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($raw =~ /^\%YAML\ 1\.1/) { } 0 0 0
elsif ($raw =~ /^\%YAML\ 1\.1/) { } 0 0 0
79 0 $self->{ensure} = 1;
80 0 $self->{ver} = '1.1';
81 }
82 elsif ( $raw =~ /^\%YAML\ 1\.1/ ) {
83 0 $self->{ensure} = 1;
84 0 $self->{ver} = '1.2';
85 }
86  
87 0 return $self;
88 }
89  
90 =head2 clear_array
91  
92 Clears the entries in a array, but does not delete the array.
93  
94 Will die if called on a item that is not a array.
95  
96 - var :: Variable to check. If not matching /^\./,
97 a period will be prepended.
98  
99 $yq->clear_array(var=>'rule-files');
100  
101 =cut
102  
103 sub clear_array {
104 0 ▶ my ( $self, %opts ) = @_;
Subroutine clear_array: not called CC=5 SCAR=34.0
Pod documented
105  
106 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
107 0 die('Nothing specified for var to check');
108 }
109 elsif ( $opts{var} !~ /^\./ ) {
110 0 $opts{var} = '.' . $opts{var};
111 }
112  
113 0 ▶ if ( $self->is_array_clear( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_array_clear("var", $opts{"var"})) 0 0 0
114 0 return;
115 }
116  
117 0 ▶ if ( $opts{var} !~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\[\]$/) 0 0 0
118 0 $opts{var} = $opts{var} . '[]';
119 }
120  
121 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
122  
123 0 $self->ensure;
124 }
125  
126 =head2 clear_hash
127  
128 Clears the entries in a hash, but does not delete the hash.
129  
130 Will die if called on a item that is not a hash.
131  
132 - var :: Variable to check. If not matching /^\./,
133 a period will be prepended.
134  
135 $yq->clear_hash(var=>'rule-files');
136  
137 =cut
138  
139 sub clear_hash {
140 0 ▶ my ( $self, %opts ) = @_;
Subroutine clear_hash: not called CC=5 SCAR=34.0
Pod documented
141  
142 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
143 0 die('Nothing specified for var to check');
144 }
145 elsif ( $opts{var} !~ /^\./ ) {
146 0 $opts{var} = '.' . $opts{var};
147 }
148  
149 0 ▶ if ( $self->is_hash_clear( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_hash_clear("var", $opts{"var"})) 0 0 0
150 0 return;
151 }
152  
153 0 ▶ if ( $opts{var} !~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\[\]$/) 0 0 0
154 0 $opts{var} = $opts{var} . '[]';
155 }
156  
157 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
158  
159 0 $self->ensure;
160 }
161  
162 =head2 create_array
163  
164 Creates a empty array. Unlike set_array, vals is optional.
165  
166 Will die if it already exists.
167  
168 - var :: Variable to operate on. If not matching /^\./,
169 a period will be prepended.
170  
171 - vals :: Array of values to set the array to.
172  
173 $yq->clear_array(var=>'rule-files');
174  
175 =cut
176  
177 sub create_array {
178 0 ▶ my ( $self, %opts ) = @_;
Subroutine create_array: not called CC=6 SCAR=37.4
Pod documented
179  
180 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
181 0 die('Nothing specified for var to check');
182 }
183 elsif ( $opts{var} !~ /^\./ ) {
184 0 $opts{var} = '.' . $opts{var};
185 }
186  
187 0 my $string;
188 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_defined('var', $opts{'var'})) { } 0 0 0
189 0 $string = `yq -i '$opts{var}=[]' $self->{qfile}`;
190 }
191 else {
192 0 die( '"' . $opts{var} . '" already exists' );
193 }
194  
195 0 ▶ if ( $opts{var} !~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\[\]$/) 0 0 0
196 0 $opts{var} =~ s/\[\]$//;
197 }
198  
199 0 my $int = 0;
200 0 ▶ while ( defined( $opts{vals}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"vals"}[$int]) 0 0 0
201 0 my $insert = $opts{var} . '[' . $int . ']="' . $opts{vals}[$int] . '"';
202 0 $string = `yq -i '$insert' $self->{qfile}`;
203 0 $int++;
204 }
205  
206 0 $self->ensure;
207 }
208  
209 =head2 create_array
210  
211 Creates a empty array.
212  
213 Will die if it already exists.
214  
215 - var :: Variable to operate on. If not matching /^\./,
216 a period will be prepended.
217  
218 $yq->clear_array(var=>'rule-files');
219  
220 =cut
221  
222 sub create_hash {
223 0 ▶ my ( $self, %opts ) = @_;
Subroutine create_hash: not called CC=4 SCAR=30.0
Pod undocumented
224  
225 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
226 0 die('Nothing specified for var to check');
227 }
228 elsif ( $opts{var} !~ /^\./ ) {
229 0 $opts{var} = '.' . $opts{var};
230 }
231  
232 0 my $string;
233 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_defined('var', $opts{'var'})) { } 0 0 0
234 0 $string = `yq -i '$opts{var}={}' $self->{qfile}`;
235 }
236 else {
237 0 die( '"' . $opts{var} . '" already exists' );
238 }
239  
240 0 $self->ensure;
241 }
242  
243 =head2 dedup_array
244  
245 Dedup the specified array.
246  
247 Will die if called on a item that is not a array or the array
248 does not exist.
249  
250 - var :: Variable to check. If not matching /^\./,
251 a period will be prepended.
252  
253 $yq->set_array(var=>'rule-files');
254  
255 =cut
256  
257 sub dedup_array {
258 0 ▶ my ( $self, %opts ) = @_;
Subroutine dedup_array: not called CC=10 SCAR=47.0
Pod documented
259  
260 0 ▶ if ( !defined( $opts{dedup} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"dedup"}) 0 0 0
261 0 $opts{dedup} = 1;
262 }
263  
264 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
265 0 die('Nothing specified for vals');
266 }
267 elsif ( $opts{var} !~ /^\./ ) {
268 0 $opts{var} = '.' . $opts{var};
269 }
270  
271 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
272 0 $opts{var} =~ s/\[\]$//;
273 }
274  
275 0 my $string;
276 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
277 0 die( '"' . $opts{var} . '" is not a array or is undef' );
278 }
279  
280 0 $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
281 0 my $yaml;
282 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
283 0 print "blank\n";
284 0 $yaml = [];
285 }
286 else {
287 0 eval { $yaml = Load($string); }
288 0 ▶ || eval { $string = 'foo: ' . $string; $yaml = Load($string); $yaml = $yaml->{foo} };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $yaml = Load($string) } } or eval { do { $string = 'foo: ' . $string; $yaml = Load($string); $yaml = $yaml->{"foo"} } } 0 0 0
289 }
290  
291 0 my $int = 0;
292 0 my $existing = {};
293 0 my @new_array;
294 0 ▶ while ( defined( $yaml->[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $yaml->[$int]) 0 0 0
295 0 ▶ if ( !defined( $existing->{ $yaml->[$int] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$existing{$yaml->[$int]}) 0 0 0
296 0 $existing->{ $yaml->[$int] } = 1;
297 0 push( @new_array, $yaml->[$int] );
298 }
299  
300 0 $int++;
301 }
302  
303 0 $self->set_array( var => $opts{var}, vals => \@new_array );
304 }
305  
306 =head2 delete
307  
308 Deletes an variable. If it is already undef, it will just return.
309  
310 - var :: Variable to check. If not matching /^\./,
311 a period will be prepended.
312  
313 $yq->delete_array(var=>'rule-files');
314  
315 =cut
316  
317 sub delete {
318 0 ▶ my ( $self, %opts ) = @_;
Subroutine delete: not called CC=4 SCAR=30.0
Pod documented
319  
320 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
321 0 die('Nothing specified for var to check');
322 }
323 elsif ( $opts{var} !~ /^\./ ) {
324 0 $opts{var} = '.' . $opts{var};
325 }
326  
327 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_defined("var", $opts{"var"})) 0 0 0
328 0 return;
329 }
330  
331 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
332  
333 0 $self->ensure;
334 }
335  
336 =head2 delete_array
337  
338 Deletes an array. If it is already undef, it will just return.
339  
340 Will die if called on a item that is not a array.
341  
342 - var :: Variable to check. If not matching /^\./,
343 a period will be prepended.
344  
345 $yq->delete_array(var=>'rule-files');
346  
347 =cut
348  
349 sub delete_array {
350 0 ▶ my ( $self, %opts ) = @_;
Subroutine delete_array: not called CC=6 SCAR=37.4
Pod documented
351  
352 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
353 0 die('Nothing specified for var to check');
354 }
355 elsif ( $opts{var} !~ /^\./ ) {
356 0 $opts{var} = '.' . $opts{var};
357 }
358  
359 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_defined("var", $opts{"var"})) 0 0 0
360 0 return;
361 }
362  
363 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
364 0 die( '"' . $opts{var} . '" is not a array' );
365 }
366  
367 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
368 0 $opts{var} =~ s/\[\]$//;
369 }
370  
371 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
372  
373 0 $self->ensure;
374 }
375  
376 =head2 delete_hash
377  
378 Deletes an hash. If it is already undef, it will just return.
379  
380 Will die if called on a item that is not a hash.
381  
382 - var :: Variable to check. If not matching /^\./,
383 a period will be prepended.
384  
385 $yq->delete_hash(var=>'vars');
386  
387 =cut
388  
389 sub delete_hash {
390 0 ▶ my ( $self, %opts ) = @_;
Subroutine delete_hash: not called CC=6 SCAR=37.4
Pod documented
391  
392 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
393 0 die('Nothing specified for var to check');
394 }
395 elsif ( $opts{var} !~ /^\./ ) {
396 0 $opts{var} = '.' . $opts{var};
397 }
398  
399 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_defined("var", $opts{"var"})) 0 0 0
400 0 return;
401 }
402  
403 0 ▶ if ( !$self->is_hash( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_hash("var", $opts{"var"})) 0 0 0
404 0 die( '"' . $opts{var} . '" is not a hash or is undef' );
405 }
406  
407 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
408 0 $opts{var} =~ s/\[\]$//;
409 }
410  
411 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
412  
413 0 $self->ensure;
414 }
415  
416 =head2 ensure
417  
418 Makes sure that the YAML file has the
419 version at the top.
420  
421 $yq->ensure;
422  
423 =cut
424  
425 sub ensure {
426 0 ▶ my ($self) = @_;
Subroutine ensure: not called CC=5 SCAR=34.0
Pod documented
427  
428 0 ▶ if ( !$self->{ensure} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ensure"}) 0 0 0
429 0 return;
430 }
431  
432 0 my $raw = read_file( $self->{file} );
433  
434 # starts
435 0 ▶ if ( $raw =~ /^\%YANL/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /^\%YANL/) 0 0 0
436 0 return;
437 }
438  
439 # add dashes to the start of the raw if it is missing
440 0 ▶ if ( $raw !~ /^\-\-\-\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw =~ /^\-\-\-\n/) 0 0 0
441 0 $raw = "---\n\n" . $raw;
442 }
443  
444 # adds the yaml version
445 0 $raw = '%YAML ' . $self->{ver} . "\n" . $raw;
446  
447 0 ▶ write_file( $self->{file}, $raw ) or die($@);
Branch: 1 branch
BranchTrueFalseTotal
write_file($self->{"file"}, $raw) or die $@ 0 0 0
448  
449 0 return;
450 }
451  
452 =head2 is_array
453  
454 Checks if the specified variable in a array.
455  
456 - var :: Variable to check. If not matching /^\./,
457 a period will be prepended.
458  
459 if ( $yq->is_array(var=>'rule-files') ){
460 print "array...\n:";
461 }
462  
463 =cut
464  
465 sub is_array {
466 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_array: not called CC=10 SCAR=47.0
Pod documented
467  
468 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
469 0 die('Nothing specified for var to check');
470 }
471 elsif ( $opts{var} !~ /^\./ ) {
472 0 $opts{var} = '.' . $opts{var};
473 }
474  
475 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
476 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
elsif ($string =~ /\{\}/) { } 0 0 0
elsif ($string eq "null\n") { } 0 0 0
477 0 return 1;
478 }
479 elsif ( $string =~ /\{\}/ ) {
480 0 return 0;
481 }
482 elsif ( $string eq "null\n" ) {
483 0 return 0;
484 }
485  
486 0 my $yaml;
487 0 eval { $yaml = Load($string); };
488 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
489 0 $string = 'foo: ' . $string;
490 0 ▶ $yaml = Load($string) || die( $string . "\n\n" . $@ );
Condition0%0/2
Load($string) || die($string . "\n\n" . $@)
l!l
00
MC/DC0%0/1
Load($string) || die($string . "\n\n" . $@)
Load($string)
Truth table0%0/2
A
A= Load($string)
Aresult
11
00
491 0 ▶ if ( ref( $yaml->{foo} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml->{"foo"} eq "ARRAY") 0 0 0
492 0 return 1;
493 }
494 0 return 0;
495 }
496  
497 0 ▶ if ( ref($yaml) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml eq "ARRAY") 0 0 0
498 0 return 1;
499 }
500  
501 0 return 0;
502 }
503  
504 =head2 is_array_clear
505  
506 Checks if a array is clear or not.
507  
508 - var :: Variable to check. If not matching /^\./,
509 a period will be prepended.
510  
511 if ( $yq->is_array_clear(var=>'rule-files') ){
512 print "clear...\n:";
513 }
514  
515 =cut
516  
517 sub is_array_clear {
518 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_array_clear: not called CC=5 SCAR=34.0
Pod documented
519  
520 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
521 0 die('Nothing specified for var to check');
522 }
523 elsif ( $opts{var} !~ /^\./ ) {
524 0 $opts{var} = '.' . $opts{var};
525 }
526  
527 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
528 0 die( '"' . $opts{var} . '" is not a array or is undef' );
529 }
530  
531 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
532 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) 0 0 0
533 0 return 1;
534 }
535  
536 0 return 0;
537 }
538  
539 =head2 is_defined
540  
541 Checks if the specified variable is defined or not.
542  
543 Will die if called on a item that is not a array.
544  
545 - var :: Variable to check. If not matching /^\./,
546 a period will be prepended.
547  
548 if ( $yq->is_defined('vars.address-groups') ){
549 print "defined...\n:";
550 }
551  
552 =cut
553  
554 sub is_defined {
555 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_defined: not called CC=4 SCAR=30.0
Pod documented
556  
557 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
558 0 die('Nothing specified for var to check');
559 }
560 elsif ( $opts{var} !~ /^\./ ) {
561 0 $opts{var} = '.' . $opts{var};
562 }
563  
564 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
565  
566 0 ▶ if ( $string eq "null\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "null\n") 0 0 0
567 0 return 0;
568 }
569  
570 0 return 1;
571 }
572  
573 =head2 is_hash
574  
575 Checks if the specified variable in a hash.
576  
577 Will die if called on a item that is not a array.
578  
579 - var :: Variable to check. If not matching /^\./,
580 a period will be prepended.
581  
582 if ( $yq->is_hash('vars.address-groups') ){
583 print "hash...\n:";
584 }
585  
586 =cut
587  
588 sub is_hash {
589 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_hash: not called CC=7 SCAR=40.3
Pod documented
590  
591 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
592 0 die('Nothing specified for var to check');
593 }
594 elsif ( $opts{var} !~ /^\./ ) {
595 0 $opts{var} = '.' . $opts{var};
596 }
597  
598 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
599  
600 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
elsif ($string =~ /\{\}/) { } 0 0 0
elsif ($string eq "null\n") { } 0 0 0
601 0 return 0;
602 }
603 elsif ( $string =~ /\{\}/ ) {
604 0 return 1;
605 }
606 elsif ( $string eq "null\n" ) {
607 0 return 0;
608 }
609  
610 0 my $yaml = Load($string);
611  
612 0 ▶ if ( ref($yaml) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml eq "HASH") 0 0 0
613 0 return 1;
614 }
615  
616 0 return 0;
617 }
618  
619 =head2 is_hash_clear
620  
621 Checks if a hash is clear or not.
622  
623 Will die if called on a item that is not a hash.
624  
625 - var :: Variable to check. If not matching /^\./,
626 a period will be prepended.
627  
628 if ( ! $yq->is_hash_clear(var=>'vars') ){
629 print "not clear...\n:";
630 }
631  
632 =cut
633  
634 sub is_hash_clear {
635 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_hash_clear: not called CC=5 SCAR=34.0
Pod documented
636  
637 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
638 0 die('Nothing specified for var to check');
639 }
640 elsif ( $opts{var} !~ /^\./ ) {
641 0 $opts{var} = '.' . $opts{var};
642 }
643  
644 0 ▶ if ( !$self->is_hash( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_hash("var", $opts{"var"})) 0 0 0
645 0 die( '"' . $opts{var} . '" is not a hash or is undef' );
646 }
647  
648 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
649 0 ▶ if ( $string =~ /\{\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\{\}/) 0 0 0
650 0 return 1;
651 }
652  
653 0 return 0;
654 }
655  
656 =head2 push_array
657  
658 Pushes the passed array onto the specified array.
659  
660 Will die if called on a item that is not a array or the array
661 does not exist.
662  
663 - var :: Variable to check. If not matching /^\./,
664 a period will be prepended.
665  
666 - vals :: Array of values to set the array to.
667  
668 $yq->set_array(var=>'rule-files',vals=>\@new_rules_files);
669  
670 =cut
671  
672 sub push_array {
673 0 ▶ my ( $self, %opts ) = @_;
Subroutine push_array: not called CC=10 SCAR=47.0
Pod documented
674  
675 0 ▶ if ( !defined( $opts{vals} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'vals'}) { } 0 0 0
676 0 die('Nothing specified for vars');
677 }
678 else {
679 0 ▶ if ( !defined $opts{vals}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vals"}[0]) 0 0 0
680 0 return;
681 }
682 }
683  
684 0 ▶ if ( !defined( $opts{dedup} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"dedup"}) 0 0 0
685 0 $opts{dedup} = 1;
686 }
687  
688 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
689 0 die('Nothing specified for vals');
690 }
691 elsif ( $opts{var} !~ /^\./ ) {
692 0 $opts{var} = '.' . $opts{var};
693 }
694  
695 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
696 0 $opts{var} =~ s/\[\]$//;
697 }
698  
699 0 my $string;
700 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
701 0 die( '"' . $opts{var} . '" is not a array or is undef' );
702 }
703  
704 0 $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
705 0 my $yaml;
706 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
707 0 print "blank\n";
708 0 $yaml = [];
709 }
710 else {
711 0 eval { $yaml = Load($string); }
712 0 ▶ || eval { $string = 'foo: ' . $string; $yaml = Load($string); $yaml = $yaml->{foo} };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $yaml = Load($string) } } or eval { do { $string = 'foo: ' . $string; $yaml = Load($string); $yaml = $yaml->{"foo"} } } 0 0 0
713 }
714  
715 0 my @new_array;
716 0 push( @new_array, @{$yaml} );
717 0 push( @new_array, @{ $opts{vals} } );
718  
719 0 $self->set_array( var => $opts{var}, vals => \@new_array );
720 }
721  
722 =head2 set_array
723  
724 Creates an array and sets it to the values.
725  
726 If the array is already defined, it will clear it and set
727 the values to those specified.
728  
729 Will die if called on a item that is not a array.
730  
731 - var :: Variable to check. If not matching /^\./,
732 a period will be prepended.
733  
734 - vals :: Array of values to set the array to.
735  
736 $yq->set_array(var=>'rule-files',vals=>\@vals);
737  
738 =cut
739  
740 sub set_array {
741 0 ▶ my ( $self, %opts ) = @_;
Subroutine set_array: not called CC=7 SCAR=40.3
Pod documented
742  
743 0 ▶ if ( !defined( $opts{vals} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vals"}) 0 0 0
744 0 die('Nothing specified for vars');
745 }
746  
747 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
748 0 die('Nothing specified for vals');
749 }
750 elsif ( $opts{var} !~ /^\./ ) {
751 0 $opts{var} = '.' . $opts{var};
752 }
753  
754 0 my $string;
755 0 ▶ if ( $self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_defined('var', $opts{'var'})) { } 0 0 0
756 0 $string = `yq -i '$opts{var}=[]' $self->{qfile}`;
757 }
758 else {
759 0 $self->clear_array( var => $opts{var} );
760 }
761  
762 0 ▶ if ( $opts{var} !~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\[\]$/) 0 0 0
763 0 $opts{var} =~ s/\[\]$//;
764 }
765  
766 0 my $int = 0;
767 0 ▶ while ( defined( $opts{vals}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"vals"}[$int]) 0 0 0
768 0 my $insert = $opts{var} . '[' . $int . ']="' . $opts{vals}[$int] . '"';
769 0 $string = `yq -i '$insert' $self->{qfile}`;
770 0 $int++;
771 }
772  
773 0 $self->ensure;
774 }
775  
776 =head2 set_hash
777  
778 Creates an hash and sets it to the values.
779  
780 If the hash is already defined, it will clear it and set
781 the values to those specified.
782  
783 Will die if called on a item that is not a array.
784  
785 - var :: Variable to check. If not matching /^\./,
786 a period will be prepended.
787  
788 - hash :: A hash to use for generating the hash to be
789 added. Any undef value will be set to null.
790  
791 $yq->set_array(var=>'vars',hash=>{a=>33,bar=>undef});
792  
793 =cut
794  
795 sub set_hash {
796 0 ▶ my ( $self, %opts ) = @_;
Subroutine set_hash: not called CC=14 SCAR=53.5
Pod documented
797  
798 0 my @keys;
799 0 ▶ if ( !defined( $opts{hash} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'hash'}) { } 0 0 0
800 0 die('Nothing specified for hash');
801 }
802 else {
803 0 ▶ if ( ref( $opts{hash} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"hash"} ne "HASH") 0 0 0
804 0 die( 'The passed value for hash is a ' . ref( $opts{hash} ) . ' and not HASH' );
805 }
806  
807 0 @keys = keys( %{ $opts{hash} } );
808  
809 0 foreach my $key (@keys) {
810 0 ▶ if ( defined( $opts{hash}{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"hash"}{$key} and ref $opts{"hash"}{$key} ne "SCALAR" and ref $opts{"hash"}{$key} ne "") 0 0 0
Condition0%0/6
defined $opts{"hash"}{$key} and ref $opts{"hash"}{$key} ne "SCALAR" and ref $opts{"hash"}{$key} ne ""
!ll&&!rl&&r
000
defined $opts{"hash"}{$key} and ref $opts{"hash"}{$key} ne "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $opts{"hash"}{$key} and ref $opts{"hash"}{$key} ne "SCALAR" and ref $opts{"hash"}{$key} ne ""
defined $opts{"hash"}{$key} ref $opts{"hash"}{$key} ne "SCALAR" ref $opts{"hash"}{$key} ne ""
Truth table0%0/4
A and B and C
A= defined $opts{"hash"}{$key}B= ref $opts{"hash"}{$key} ne "SCALAR"C= ref $opts{"hash"}{$key} ne ""
ABCresult
0XX0
10X0
1100
1111
811 && ref( $opts{hash}{$key} ) ne 'SCALAR'
812 && ref( $opts{hash}{$key} ) ne '' )
813 {
814 die( 'The passed value for the key "'
815 . $key
816 . '" for the hash is a '
817 0 . ref( $opts{hash}{$key} )
818 . ' and not SCALAR or undef' );
819 }
820 }
821 }
822  
823 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
824 0 die('Nothing specified for vals');
825 }
826 elsif ( $opts{var} !~ /^\./ ) {
827 0 $opts{var} = '.' . $opts{var};
828 }
829  
830 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
831 0 die( 'vars, "' . $opts{var} . '", may not contains []' );
832 }
833  
834 0 ▶ if ( $opts{var} !~ /\.$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\.$/) 0 0 0
835 0 $opts{var} =~ s/\.$//;
836 }
837  
838 0 my $string;
839 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_defined('var', $opts{'var'})) { } 0 0 0
840 0 $string = `yq -i '$opts{var}={}' $self->{qfile}`;
841 }
842 else {
843 0 $self->clear_hash( var => $opts{var} );
844 }
845  
846 0 foreach my $key (@keys) {
847 0 my $insert;
848 0 ▶ if ( defined( $opts{hash}{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'hash'}{$key}) { } 0 0 0
849 0 $insert = $opts{var} . '.' . $key . '="' . $opts{hash}{$key} . '"';
850 }
851 else {
852 0 $insert = $opts{var} . '.' . $key . '=null';
853 }
854 0 $string = `yq -i '$insert' $self->{qfile}`;
855 }
856  
857 0 $self->ensure;
858 }
859  
860 =head2 set_in_array
861  
862 Ensures the values specified exist at any point in the array.
863  
864 Will create the array if it does not already exist.
865  
866 Will die if called on a item that is not a array.
867  
868 - var :: Variable to check. If not matching /^\./,
869 a period will be prepended.
870  
871 - vals :: Array of values to set the array to.
872  
873 - dedup :: If it should deduplicate the existing items
874 in the array or not.
875 Default :: 1
876  
877 $yq->set_array(var=>'rule-files',vals=>\@vals);
878  
879 =cut
880  
881 sub set_in_array {
882 0 ▶ my ( $self, %opts ) = @_;
Subroutine set_in_array: not called CC=14 SCAR=53.5
Pod documented
883  
884 0 my $to_exist = {};
885 0 ▶ if ( !defined( $opts{vals} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'vals'}) { } 0 0 0
886 0 die('Nothing specified for vars');
887 }
888 else {
889 0 ▶ if ( !defined $opts{vals}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vals"}[0]) 0 0 0
890 0 return;
891 }
892  
893 0 my $int = 0;
894 0 ▶ while ( defined( $opts{vals}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"vals"}[$int]) 0 0 0
895 0 $to_exist->{ $opts{vals}[$int] } = 1;
896 0 $int++;
897 }
898  
899 }
900  
901 0 ▶ if ( !defined( $opts{dedup} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"dedup"}) 0 0 0
902 0 $opts{dedup} = 1;
903 }
904  
905 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
906 0 die('Nothing specified for vals');
907 }
908 elsif ( $opts{var} !~ /^\./ ) {
909 0 $opts{var} = '.' . $opts{var};
910 }
911  
912 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
913 0 $opts{var} =~ s/\[\]$//;
914 }
915  
916 0 my $string;
917 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_defined('var', $opts{'var'})) { } 0 0 0
918 0 $string = `yq -i '$opts{var}=[]' $self->{qfile}`;
919 }
920 else {
921 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
922 0 die( '"' . $opts{var} . '" is not a array or is undef' );
923 }
924  
925 }
926  
927 0 $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
928 0 my $yaml;
929 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
930 0 print "blank\n";
931 0 $yaml = [];
932 }
933 else {
934 0 eval { $yaml = Load($string); };
935 }
936  
937 0 my $int = 0;
938 0 my @exiting_a;
939 0 my $existing_h = {};
940 0 ▶ while ( defined( $yaml->[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $yaml->[$int]) 0 0 0
941 0 ▶ if ( defined( $to_exist->{ $yaml->[$int] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$to_exist{$yaml->[$int]}) 0 0 0
942 0 delete( $to_exist->{ $yaml->[$int] } );
943 }
944  
945 0 push( @exiting_a, $yaml->[$int] );
946  
947 0 $existing_h->{ $yaml->[$int] } = 1;
948  
949 0 $int++;
950 }
951  
952 0 my @new_array;
953 0 ▶ if ( $opts{dedup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'dedup'}) { } 0 0 0
954 0 push( @new_array, keys( %{$existing_h} ) );
955 0 push( @new_array, keys( %{$to_exist} ) );
956 }
957 else {
958 0 push( @new_array, @exiting_a );
959 0 push( @new_array, keys( %{$to_exist} ) );
960 }
961  
962 0 $self->set_array( var => $opts{var}, vals => \@new_array );
963 }
964  
965 =head1 AUTHOR
966  
967 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
968  
969 =head1 BUGS
970  
971 Please report any bugs or feature requests to C<bug-yaml-yq-helper at rt.cpan.org>, or through
972 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=YAML-yq-Helper>. I will be notified, and then you'll
973 automatically be notified of progress on your bug as I make changes.
974  
975  
976  
977  
978 =head1 SUPPORT
979  
980 You can find documentation for this module with the perldoc command.
981  
982 perldoc YAML::yq::Helper
983  
984  
985 You can also look for information at:
986  
987 =over 4
988  
989 =item * RT: CPAN's request tracker (report bugs here)
990  
991 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=YAML-yq-Helper>
992  
993 =item * CPAN Ratings
994  
995 L<https://cpanratings.perl.org/d/YAML-yq-Helper>
996  
997 =item * Search CPAN
998  
999 L<https://metacpan.org/release/YAML-yq-Helper>
1000  
1001 =back
1002  
1003  
1004 =head1 ACKNOWLEDGEMENTS
1005  
1006  
1007 =head1 LICENSE AND COPYRIGHT
1008  
1009 This software is Copyright (c) 2022 by Zane C. Bowers-Hadley.
1010  
1011 This is free software, licensed under:
1012  
1013 The Artistic License 2.0 (GPL Compatible)
1014  
1015  
1016 =cut
1017  
1018 1 1; # End of YAML::yq::Helper
↑ Summary