blib/lib/YAML/yq/Helper.pm

filter: Statementstmt 4.8% 16 / 333 Branchbran 0.0% 0 / 216 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 20.8% 5 / 24 Podpod 94.7% 18 / 19 totaltotal 6.4% 39 / 601 CC 113 SCAR 44.1
CC
113
Cov
3%
set_hash
210.0
set_in_array
210.0
dedup_array
90.0
CRAP 82.3
↑ 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.0
16  
17 =cut
18  
19 1 our $VERSION = '0.1.0';
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=9 SCAR=45.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 }
289  
290 0 my $int = 0;
291 0 my $existing = {};
292 0 my @new_array;
293 0 ▶ while ( defined( $yaml->[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $yaml->[$int]) 0 0 0
294 0 ▶ if ( !defined( $existing->{ $yaml->[$int] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$existing{$yaml->[$int]}) 0 0 0
295 0 $existing->{ $yaml->[$int] } = 1;
296 0 push( @new_array, $yaml->[$int] );
297 }
298  
299 0 $int++;
300 }
301  
302 0 $self->set_array( var => $opts{var}, vals => \@new_array );
303 }
304  
305 =head2 delete
306  
307 Deletes an variable. If it is already undef, it will just return.
308  
309 - var :: Variable to check. If not matching /^\./,
310 a period will be prepended.
311  
312 $yq->delete_array(var=>'rule-files');
313  
314 =cut
315  
316 sub delete {
317 0 ▶ my ( $self, %opts ) = @_;
Subroutine delete: not called CC=4 SCAR=30.0
Pod documented
318  
319 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
320 0 die('Nothing specified for var to check');
321 }
322 elsif ( $opts{var} !~ /^\./ ) {
323 0 $opts{var} = '.' . $opts{var};
324 }
325  
326 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_defined("var", $opts{"var"})) 0 0 0
327 0 return;
328 }
329  
330 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
331  
332 0 $self->ensure;
333 }
334  
335 =head2 delete_array
336  
337 Deletes an array. If it is already undef, it will just return.
338  
339 Will die if called on a item that is not a array.
340  
341 - var :: Variable to check. If not matching /^\./,
342 a period will be prepended.
343  
344 $yq->delete_array(var=>'rule-files');
345  
346 =cut
347  
348 sub delete_array {
349 0 ▶ my ( $self, %opts ) = @_;
Subroutine delete_array: not called CC=6 SCAR=37.4
Pod documented
350  
351 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
352 0 die('Nothing specified for var to check');
353 }
354 elsif ( $opts{var} !~ /^\./ ) {
355 0 $opts{var} = '.' . $opts{var};
356 }
357  
358 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_defined("var", $opts{"var"})) 0 0 0
359 0 return;
360 }
361  
362 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
363 0 die( '"' . $opts{var} . '" is not a array' );
364 }
365  
366 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
367 0 $opts{var} =~ s/\[\]$//;
368 }
369  
370 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
371  
372 0 $self->ensure;
373 }
374  
375 =head2 delete_hash
376  
377 Deletes an hash. If it is already undef, it will just return.
378  
379 Will die if called on a item that is not a hash.
380  
381 - var :: Variable to check. If not matching /^\./,
382 a period will be prepended.
383  
384 $yq->delete_hash(var=>'vars');
385  
386 =cut
387  
388 sub delete_hash {
389 0 ▶ my ( $self, %opts ) = @_;
Subroutine delete_hash: not called CC=6 SCAR=37.4
Pod documented
390  
391 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
392 0 die('Nothing specified for var to check');
393 }
394 elsif ( $opts{var} !~ /^\./ ) {
395 0 $opts{var} = '.' . $opts{var};
396 }
397  
398 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_defined("var", $opts{"var"})) 0 0 0
399 0 return;
400 }
401  
402 0 ▶ if ( !$self->is_hash( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_hash("var", $opts{"var"})) 0 0 0
403 0 die( '"' . $opts{var} . '" is not a hash or is undef' );
404 }
405  
406 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
407 0 $opts{var} =~ s/\[\]$//;
408 }
409  
410 0 my $string = `yq -i "del $opts{var}" $self->{qfile}`;
411  
412 0 $self->ensure;
413 }
414  
415 =head2 ensure
416  
417 Makes sure that the YAML file has the
418 version at the top.
419  
420 $yq->ensure;
421  
422 =cut
423  
424 sub ensure {
425 0 ▶ my ($self) = @_;
Subroutine ensure: not called CC=5 SCAR=34.0
Pod documented
426  
427 0 ▶ if ( !$self->{ensure} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ensure"}) 0 0 0
428 0 return;
429 }
430  
431 0 my $raw = read_file( $self->{file} );
432  
433 # starts
434 0 ▶ if ( $raw =~ /^\%YANL/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /^\%YANL/) 0 0 0
435 0 return;
436 }
437  
438 # add dashes to the start of the raw if it is missing
439 0 ▶ if ( $raw !~ /^\-\-\-\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw =~ /^\-\-\-\n/) 0 0 0
440 0 $raw = "---\n\n" . $raw;
441 }
442  
443 # adds the yaml version
444 0 $raw = '%YAML ' . $self->{ver} . "\n" . $raw;
445  
446 0 ▶ write_file( $self->{file}, $raw ) or die($@);
Branch: 1 branch
BranchTrueFalseTotal
write_file($self->{"file"}, $raw) or die $@ 0 0 0
447  
448 0 return;
449 }
450  
451 =head2 is_array
452  
453 Checks if the specified variable in a array.
454  
455 - var :: Variable to check. If not matching /^\./,
456 a period will be prepended.
457  
458 if ( $yq->is_array(var=>'rule-files') ){
459 print "array...\n:";
460 }
461  
462 =cut
463  
464 sub is_array {
465 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_array: not called CC=8 SCAR=42.8
Pod documented
466  
467 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
468 0 die('Nothing specified for var to check');
469 }
470 elsif ( $opts{var} !~ /^\./ ) {
471 0 $opts{var} = '.' . $opts{var};
472 }
473  
474 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
475 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
476 0 return 1;
477 }
478 elsif ( $string =~ /\{\}/ ) {
479 0 return 0;
480 }
481 elsif ( $string eq "null\n" ) {
482 0 return 0;
483 }
484  
485 0 my $yaml;
486 0 eval { $yaml = Load($string); };
487 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
488 0 die($@);
489 }
490  
491 0 ▶ if ( ref($yaml) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml eq "ARRAY") 0 0 0
492 0 return 1;
493 }
494  
495 0 return 0;
496 }
497  
498 =head2 is_array_clear
499  
500 Checks if a array is clear or not.
501  
502 - var :: Variable to check. If not matching /^\./,
503 a period will be prepended.
504  
505 if ( $yq->is_array_clear(var=>'rule-files') ){
506 print "clear...\n:";
507 }
508  
509 =cut
510  
511 sub is_array_clear {
512 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_array_clear: not called CC=5 SCAR=34.0
Pod documented
513  
514 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
515 0 die('Nothing specified for var to check');
516 }
517 elsif ( $opts{var} !~ /^\./ ) {
518 0 $opts{var} = '.' . $opts{var};
519 }
520  
521 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
522 0 die( '"' . $opts{var} . '" is not a array or is undef' );
523 }
524  
525 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
526 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) 0 0 0
527 0 return 1;
528 }
529  
530 0 return 0;
531 }
532  
533 =head2 is_defined
534  
535 Checks if the specified variable is defined or not.
536  
537 Will die if called on a item that is not a array.
538  
539 - var :: Variable to check. If not matching /^\./,
540 a period will be prepended.
541  
542 if ( $yq->is_defined('vars.address-groups') ){
543 print "defined...\n:";
544 }
545  
546 =cut
547  
548 sub is_defined {
549 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_defined: not called CC=4 SCAR=30.0
Pod documented
550  
551 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
552 0 die('Nothing specified for var to check');
553 }
554 elsif ( $opts{var} !~ /^\./ ) {
555 0 $opts{var} = '.' . $opts{var};
556 }
557  
558 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
559  
560 0 ▶ if ( $string eq "null\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "null\n") 0 0 0
561 0 return 0;
562 }
563  
564 0 return 1;
565 }
566  
567 =head2 is_hash
568  
569 Checks if the specified variable in a hash.
570  
571 Will die if called on a item that is not a array.
572  
573 - var :: Variable to check. If not matching /^\./,
574 a period will be prepended.
575  
576 if ( $yq->is_hash('vars.address-groups') ){
577 print "hash...\n:";
578 }
579  
580 =cut
581  
582 sub is_hash {
583 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_hash: not called CC=7 SCAR=40.3
Pod documented
584  
585 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
586 0 die('Nothing specified for var to check');
587 }
588 elsif ( $opts{var} !~ /^\./ ) {
589 0 $opts{var} = '.' . $opts{var};
590 }
591  
592 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
593  
594 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
595 0 return 0;
596 }
597 elsif ( $string =~ /\{\}/ ) {
598 0 return 1;
599 }
600 elsif ( $string eq "null\n" ) {
601 0 return 0;
602 }
603  
604 0 my $yaml = Load($string);
605  
606 0 ▶ if ( ref($yaml) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml eq "HASH") 0 0 0
607 0 return 1;
608 }
609  
610 0 return 0;
611 }
612  
613 =head2 is_hash_clear
614  
615 Checks if a hash is clear or not.
616  
617 Will die if called on a item that is not a hash.
618  
619 - var :: Variable to check. If not matching /^\./,
620 a period will be prepended.
621  
622 if ( ! $yq->is_hash_clear(var=>'vars') ){
623 print "not clear...\n:";
624 }
625  
626 =cut
627  
628 sub is_hash_clear {
629 0 ▶ my ( $self, %opts ) = @_;
Subroutine is_hash_clear: not called CC=5 SCAR=34.0
Pod documented
630  
631 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
632 0 die('Nothing specified for var to check');
633 }
634 elsif ( $opts{var} !~ /^\./ ) {
635 0 $opts{var} = '.' . $opts{var};
636 }
637  
638 0 ▶ if ( !$self->is_hash( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_hash("var", $opts{"var"})) 0 0 0
639 0 die( '"' . $opts{var} . '" is not a hash or is undef' );
640 }
641  
642 0 my $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
643 0 ▶ if ( $string =~ /\{\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\{\}/) 0 0 0
644 0 return 1;
645 }
646  
647 0 return 0;
648 }
649  
650 =head2 push_array
651  
652 Pushes the passed array onto the specified array.
653  
654 Will die if called on a item that is not a array or the array
655 does not exist.
656  
657 - var :: Variable to check. If not matching /^\./,
658 a period will be prepended.
659  
660 - vals :: Array of values to set the array to.
661  
662 $yq->set_array(var=>'rule-files',vals=>\@new_rules_files);
663  
664 =cut
665  
666 sub push_array {
667 0 ▶ my ( $self, %opts ) = @_;
Subroutine push_array: not called CC=9 SCAR=45.0
Pod documented
668  
669 0 ▶ if ( !defined( $opts{vals} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'vals'}) { } 0 0 0
670 0 die('Nothing specified for vars');
671 }
672 else {
673 0 ▶ if ( !defined $opts{vals}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vals"}[0]) 0 0 0
674 0 return;
675 }
676 }
677  
678 0 ▶ if ( !defined( $opts{dedup} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"dedup"}) 0 0 0
679 0 $opts{dedup} = 1;
680 }
681  
682 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
683 0 die('Nothing specified for vals');
684 }
685 elsif ( $opts{var} !~ /^\./ ) {
686 0 $opts{var} = '.' . $opts{var};
687 }
688  
689 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
690 0 $opts{var} =~ s/\[\]$//;
691 }
692  
693 0 my $string;
694 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
695 0 die( '"' . $opts{var} . '" is not a array or is undef' );
696 }
697  
698 0 $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
699 0 my $yaml;
700 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
701 0 print "blank\n";
702 0 $yaml = [];
703 }
704 else {
705 0 eval { $yaml = Load($string); };
706 }
707  
708 0 my @new_array;
709 0 push( @new_array, @{$yaml} );
710 0 push( @new_array, @{ $opts{vals} } );
711  
712 0 $self->set_array( var => $opts{var}, vals => \@new_array );
713 }
714  
715 =head2 set_array
716  
717 Creates an array and sets it to the values.
718  
719 If the array is already defined, it will clear it and set
720 the values to those specified.
721  
722 Will die if called on a item that is not a array.
723  
724 - var :: Variable to check. If not matching /^\./,
725 a period will be prepended.
726  
727 - vals :: Array of values to set the array to.
728  
729 $yq->set_array(var=>'rule-files',vals=>\@vals);
730  
731 =cut
732  
733 sub set_array {
734 0 ▶ my ( $self, %opts ) = @_;
Subroutine set_array: not called CC=7 SCAR=40.3
Pod documented
735  
736 0 ▶ if ( !defined( $opts{vals} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vals"}) 0 0 0
737 0 die('Nothing specified for vars');
738 }
739  
740 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
741 0 die('Nothing specified for vals');
742 }
743 elsif ( $opts{var} !~ /^\./ ) {
744 0 $opts{var} = '.' . $opts{var};
745 }
746  
747 0 my $string;
748 0 ▶ if ( $self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_defined('var', $opts{'var'})) { } 0 0 0
749 0 $string = `yq -i '$opts{var}=[]' $self->{qfile}`;
750 }
751 else {
752 0 $self->clear_array( var => $opts{var} );
753 }
754  
755 0 ▶ if ( $opts{var} !~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\[\]$/) 0 0 0
756 0 $opts{var} =~ s/\[\]$//;
757 }
758  
759 0 my $int = 0;
760 0 ▶ while ( defined( $opts{vals}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"vals"}[$int]) 0 0 0
761 0 my $insert = $opts{var} . '[' . $int . ']="' . $opts{vals}[$int] . '"';
762 0 $string = `yq -i '$insert' $self->{qfile}`;
763 0 $int++;
764 }
765  
766 0 $self->ensure;
767 }
768  
769 =head2 set_hash
770  
771 Creates an hash and sets it to the values.
772  
773 If the hash is already defined, it will clear it and set
774 the values to those specified.
775  
776 Will die if called on a item that is not a array.
777  
778 - var :: Variable to check. If not matching /^\./,
779 a period will be prepended.
780  
781 - hash :: A hash to use for generating the hash to be
782 added. Any undef value will be set to null.
783  
784 $yq->set_array(var=>'vars',hash=>{a=>33,bar=>undef});
785  
786 =cut
787  
788 sub set_hash {
789 0 ▶ my ( $self, %opts ) = @_;
Subroutine set_hash: not called CC=14 SCAR=53.5
Pod documented
790  
791 0 my @keys;
792 0 ▶ if ( !defined( $opts{hash} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'hash'}) { } 0 0 0
793 0 die('Nothing specified for hash');
794 }
795 else {
796 0 ▶ if ( ref( $opts{hash} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"hash"} ne "HASH") 0 0 0
797 0 die( 'The passed value for hash is a ' . ref( $opts{hash} ) . ' and not HASH' );
798 }
799  
800 0 @keys = keys( %{ $opts{hash} } );
801  
802 0 foreach my $key (@keys) {
803 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
804 && ref( $opts{hash}{$key} ) ne 'SCALAR'
805 && ref( $opts{hash}{$key} ) ne '' )
806 {
807 die( 'The passed value for the key "'
808 . $key
809 . '" for the hash is a '
810 0 . ref( $opts{hash}{$key} )
811 . ' and not SCALAR or undef' );
812 }
813 }
814 }
815  
816 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
817 0 die('Nothing specified for vals');
818 }
819 elsif ( $opts{var} !~ /^\./ ) {
820 0 $opts{var} = '.' . $opts{var};
821 }
822  
823 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
824 0 die( 'vars, "' . $opts{var} . '", may not contains []' );
825 }
826  
827 0 ▶ if ( $opts{var} !~ /\.$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"var"} =~ /\.$/) 0 0 0
828 0 $opts{var} =~ s/\.$//;
829 }
830  
831 0 my $string;
832 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_defined('var', $opts{'var'})) { } 0 0 0
833 0 $string = `yq -i '$opts{var}={}' $self->{qfile}`;
834 }
835 else {
836 0 $self->clear_hash( var => $opts{var} );
837 }
838  
839 0 foreach my $key (@keys) {
840 0 my $insert;
841 0 ▶ if ( defined( $opts{hash}{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'hash'}{$key}) { } 0 0 0
842 0 $insert = $opts{var} . '.' . $key . '="' . $opts{hash}{$key} . '"';
843 }
844 else {
845 0 $insert = $opts{var} . '.' . $key . '=null';
846 }
847 0 $string = `yq -i '$insert' $self->{qfile}`;
848 }
849  
850 0 $self->ensure;
851 }
852  
853 =head2 set_in_array
854  
855 Ensures the values specified exist at any point in the array.
856  
857 Will create the array if it does not already exist.
858  
859 Will die if called on a item that is not a array.
860  
861 - var :: Variable to check. If not matching /^\./,
862 a period will be prepended.
863  
864 - vals :: Array of values to set the array to.
865  
866 - dedup :: If it should deduplicate the existing items
867 in the array or not.
868 Default :: 1
869  
870 $yq->set_array(var=>'rule-files',vals=>\@vals);
871  
872 =cut
873  
874 sub set_in_array {
875 0 ▶ my ( $self, %opts ) = @_;
Subroutine set_in_array: not called CC=14 SCAR=53.5
Pod documented
876  
877 0 my $to_exist = {};
878 0 ▶ if ( !defined( $opts{vals} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'vals'}) { } 0 0 0
879 0 die('Nothing specified for vars');
880 }
881 else {
882 0 ▶ if ( !defined $opts{vals}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vals"}[0]) 0 0 0
883 0 return;
884 }
885  
886 0 my $int = 0;
887 0 ▶ while ( defined( $opts{vals}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"vals"}[$int]) 0 0 0
888 0 $to_exist->{ $opts{vals}[$int] } = 1;
889 0 $int++;
890 }
891  
892 }
893  
894 0 ▶ if ( !defined( $opts{dedup} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"dedup"}) 0 0 0
895 0 $opts{dedup} = 1;
896 }
897  
898 0 ▶ if ( !defined( $opts{var} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'var'}) { } 0 0 0
elsif (not $opts{'var'} =~ /^\./) { } 0 0 0
899 0 die('Nothing specified for vals');
900 }
901 elsif ( $opts{var} !~ /^\./ ) {
902 0 $opts{var} = '.' . $opts{var};
903 }
904  
905 0 ▶ if ( $opts{var} =~ /\[\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"var"} =~ /\[\]$/) 0 0 0
906 0 $opts{var} =~ s/\[\]$//;
907 }
908  
909 0 my $string;
910 0 ▶ if ( !$self->is_defined( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_defined('var', $opts{'var'})) { } 0 0 0
911 0 $string = `yq -i '$opts{var}=[]' $self->{qfile}`;
912 }
913 else {
914 0 ▶ if ( !$self->is_array( var => $opts{var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_array("var", $opts{"var"})) 0 0 0
915 0 die( '"' . $opts{var} . '" is not a array or is undef' );
916 }
917  
918 }
919  
920 0 $string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
921 0 my $yaml;
922 0 ▶ if ( $string =~ /\[\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\[\]/) { } 0 0 0
923 0 print "blank\n";
924 0 $yaml = [];
925 }
926 else {
927 0 eval { $yaml = Load($string); };
928 }
929  
930 0 my $int = 0;
931 0 my @exiting_a;
932 0 my $existing_h = {};
933 0 ▶ while ( defined( $yaml->[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $yaml->[$int]) 0 0 0
934 0 ▶ if ( defined( $to_exist->{ $yaml->[$int] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$to_exist{$yaml->[$int]}) 0 0 0
935 0 delete( $to_exist->{ $yaml->[$int] } );
936 }
937  
938 0 push( @exiting_a, $yaml->[$int] );
939  
940 0 $existing_h->{ $yaml->[$int] } = 1;
941  
942 0 $int++;
943 }
944  
945 0 my @new_array;
946 0 ▶ if ( $opts{dedup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'dedup'}) { } 0 0 0
947 0 push( @new_array, keys( %{$existing_h} ) );
948 0 push( @new_array, keys( %{$to_exist} ) );
949 }
950 else {
951 0 push( @new_array, @exiting_a );
952 0 push( @new_array, keys( %{$to_exist} ) );
953 }
954  
955 0 $self->set_array( var => $opts{var}, vals => \@new_array );
956 }
957  
958 =head1 AUTHOR
959  
960 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
961  
962 =head1 BUGS
963  
964 Please report any bugs or feature requests to C<bug-yaml-yq-helper at rt.cpan.org>, or through
965 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=YAML-yq-Helper>. I will be notified, and then you'll
966 automatically be notified of progress on your bug as I make changes.
967  
968  
969  
970  
971 =head1 SUPPORT
972  
973 You can find documentation for this module with the perldoc command.
974  
975 perldoc YAML::yq::Helper
976  
977  
978 You can also look for information at:
979  
980 =over 4
981  
982 =item * RT: CPAN's request tracker (report bugs here)
983  
984 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=YAML-yq-Helper>
985  
986 =item * CPAN Ratings
987  
988 L<https://cpanratings.perl.org/d/YAML-yq-Helper>
989  
990 =item * Search CPAN
991  
992 L<https://metacpan.org/release/YAML-yq-Helper>
993  
994 =back
995  
996  
997 =head1 ACKNOWLEDGEMENTS
998  
999  
1000 =head1 LICENSE AND COPYRIGHT
1001  
1002 This software is Copyright (c) 2022 by Zane C. Bowers-Hadley.
1003  
1004 This is free software, licensed under:
1005  
1006 The Artistic License 2.0 (GPL Compatible)
1007  
1008  
1009 =cut
1010  
1011 1 1; # End of YAML::yq::Helper
↑ Summary