add push_array, dedup_array, and set_in_array

This commit is contained in:
Zane C. B-H 2022-10-25 17:14:54 -05:00
parent ba79124f46
commit 7b75367a6c
3 changed files with 282 additions and 1 deletions

View File

@ -48,6 +48,9 @@ make install
by a /=/.
Default :: undef
--dedup <bool> If it should dedup the data for the op.
Default :: 1
Action :: is_array
Description :: Returns 0 or 1 based on if it is a array.
Requires :: --var
@ -77,6 +80,10 @@ Action :: create_hash
Description :: Creates the specified hash if it does not exist.
Requires :: --var
Action :: dedup_array
Description :: Deduplicates an array.
Requires :: --var
Action :: delete
Description :: Deletes the var without checking the type.
Requires :: --var
@ -89,6 +96,10 @@ Action :: delete_hash
Description :: Deletes the specified hash.
Requires :: --var
Action :: push_array
Description :: Pushes a set of items onto an array.
Requires :: --var,--vals
Action :: set_array
Description :: Clears the array and sets it to specified values.
Requires :: --var,--vals
@ -96,4 +107,9 @@ Requires :: --var,--vals
Action :: set_hash
Description :: Clears the hash and sets it to specified values.
Requires :: --var,--hash
Action :: set_in_array
Description :: Make sure a set of values exist in a array and if not add them.
Requires :: --var,--vals
Optional :: --dedup
```

35
bin/yqh
View File

@ -31,6 +31,9 @@ sub help {
by a /=/.
Default :: undef
--dedup <bool> If it should dedup the data for the op.
Default :: 1
Action :: is_array
Description :: Returns 0 or 1 based on if it is a array.
Requires :: --var
@ -60,6 +63,10 @@ Action :: create_hash
Description :: Creates the specified hash if it does not exist.
Requires :: --var
Action :: dedup_array
Description :: Deduplicates an array.
Requires :: --var
Action :: delete
Description :: Deletes the var without checking the type.
Requires :: --var
@ -72,6 +79,10 @@ Action :: delete_hash
Description :: Deletes the specified hash.
Requires :: --var
Action :: push_array
Description :: Pushes a set of items onto an array.
Requires :: --var,--vals
Action :: set_array
Description :: Clears the array and sets it to specified values.
Requires :: --var,--vals
@ -79,6 +90,11 @@ Requires :: --var,--vals
Action :: set_hash
Description :: Clears the hash and sets it to specified values.
Requires :: --var,--hash
Action :: set_in_array
Description :: Make sure a set of values exist in a array and if not add them.
Requires :: --var,--vals
Optional :: --dedup
';
}
@ -94,6 +110,7 @@ my $vals_sep = ',';
my $key;
my $hash_string;
my $val;
my $dedup=1;
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('bundling');
GetOptions(
@ -108,7 +125,8 @@ GetOptions(
'vals=s' => \$vals_string,
'hash=s' => \$hash_string,
'val=s' => \$val_string,
'k=s' => \$key,
'k=s' => \$key,
'dedup=s' =>\$dedup,
);
$vals_sep = quotemeta($vals_sep);
@ -238,6 +256,21 @@ if ( $action eq 'set_val' ) {
exit 0;
}
if ( $action eq 'set_in_array' ) {
$yq->set_in_array( var => $var, vals => \@vals, dedup=>$dedup );
exit 0;
}
if ( $action eq 'push_array' ) {
$yq->push_array( var => $var, vals => \@vals, );
exit 0;
}
if ( $action eq 'dedup_array' ) {
$yq->dedup_array( var => $var );
exit 0;
}
if ( $action eq 'create_array' ) {
$yq->create_array( var => $var, vals => \@vals );
exit 0;

View File

@ -240,6 +240,68 @@ sub create_hash {
$self->ensure;
}
=head2 dedup_array
Dedup the specified array.
Will die if called on a item that is not a array or the array
does not exist.
- var :: Variable to check. If not matching /^\./,
a period will be prepended.
$yq->set_array(var=>'rule-files');
=cut
sub dedup_array {
my ( $self, %opts ) = @_;
if ( !defined( $opts{dedup} ) ) {
$opts{dedup} = 1;
}
if ( !defined( $opts{var} ) ) {
die('Nothing specified for vals');
}
elsif ( $opts{var} !~ /^\./ ) {
$opts{var} = '.' . $opts{var};
}
if ( $opts{var} =~ /\[\]$/ ) {
$opts{var} =~ s/\[\]$//;
}
my $string;
if ( !$self->is_array( var => $opts{var} ) ) {
die( '"' . $opts{var} . '" is not a array or is undef' );
}
$string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
my $yaml;
if ( $string =~ /\[\]/ ) {
print "blank\n";
$yaml = [];
}
else {
eval { $yaml = Load($string); };
}
my $int = 0;
my $existing = {};
my @new_array;
while ( defined( $yaml->[$int] ) ) {
if ( !defined( $existing->{ $yaml->[$int] } ) ) {
$existing->{ $yaml->[$int] } = 1;
push( @new_array, $yaml->[$int] );
}
$int++;
}
$self->set_array( var => $opts{var}, vals => \@new_array );
}
=head2 delete
Deletes an variable. If it is already undef, it will just return.
@ -585,6 +647,71 @@ sub is_hash_clear {
return 0;
}
=head2 push_array
Pushes the passed array onto the specified array.
Will die if called on a item that is not a array or the array
does not exist.
- var :: Variable to check. If not matching /^\./,
a period will be prepended.
- vals :: Array of values to set the array to.
$yq->set_array(var=>'rule-files',vals=>\@new_rules_files);
=cut
sub push_array {
my ( $self, %opts ) = @_;
if ( !defined( $opts{vals} ) ) {
die('Nothing specified for vars');
}
else {
if ( !defined $opts{vals}[0] ) {
return;
}
}
if ( !defined( $opts{dedup} ) ) {
$opts{dedup} = 1;
}
if ( !defined( $opts{var} ) ) {
die('Nothing specified for vals');
}
elsif ( $opts{var} !~ /^\./ ) {
$opts{var} = '.' . $opts{var};
}
if ( $opts{var} =~ /\[\]$/ ) {
$opts{var} =~ s/\[\]$//;
}
my $string;
if ( !$self->is_array( var => $opts{var} ) ) {
die( '"' . $opts{var} . '" is not a array or is undef' );
}
$string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
my $yaml;
if ( $string =~ /\[\]/ ) {
print "blank\n";
$yaml = [];
}
else {
eval { $yaml = Load($string); };
}
my @new_array;
push( @new_array, @{$yaml} );
push( @new_array, @{ $opts{vals} } );
$self->set_array( var => $opts{var}, vals => \@new_array );
}
=head2 set_array
Creates an array and sets it to the values.
@ -723,6 +850,111 @@ sub set_hash {
$self->ensure;
}
=head2 set_in_array
Ensures the values specified exist at any point in the array.
Will create the array if it does not already exist.
Will die if called on a item that is not a array.
- var :: Variable to check. If not matching /^\./,
a period will be prepended.
- vals :: Array of values to set the array to.
- dedup :: If it should deduplicate the existing items
in the array or not.
Default :: 1
$yq->set_array(var=>'rule-files',vals=>\@vals);
=cut
sub set_in_array {
my ( $self, %opts ) = @_;
my $to_exist = {};
if ( !defined( $opts{vals} ) ) {
die('Nothing specified for vars');
}
else {
if ( !defined $opts{vals}[0] ) {
return;
}
my $int = 0;
while ( defined( $opts{vals}[$int] ) ) {
$to_exist->{ $opts{vals}[$int] } = 1;
$int++;
}
}
if ( !defined( $opts{dedup} ) ) {
$opts{dedup} = 1;
}
if ( !defined( $opts{var} ) ) {
die('Nothing specified for vals');
}
elsif ( $opts{var} !~ /^\./ ) {
$opts{var} = '.' . $opts{var};
}
if ( $opts{var} =~ /\[\]$/ ) {
$opts{var} =~ s/\[\]$//;
}
my $string;
if ( !$self->is_defined( var => $opts{var} ) ) {
$string = `yq -i '$opts{var}=[]' $self->{qfile}`;
}
else {
if ( !$self->is_array( var => $opts{var} ) ) {
die( '"' . $opts{var} . '" is not a array or is undef' );
}
}
$string = `yq "$opts{var}" $self->{qfile} 2> /dev/null`;
my $yaml;
if ( $string =~ /\[\]/ ) {
print "blank\n";
$yaml = [];
}
else {
eval { $yaml = Load($string); };
}
my $int = 0;
my @exiting_a;
my $existing_h = {};
while ( defined( $yaml->[$int] ) ) {
if ( defined( $to_exist->{ $yaml->[$int] } ) ) {
delete( $to_exist->{ $yaml->[$int] } );
}
push( @exiting_a, $yaml->[$int] );
$existing_h->{ $yaml->[$int] } = 1;
$int++;
}
my @new_array;
if ( $opts{dedup} ) {
push( @new_array, keys( %{$existing_h} ) );
push( @new_array, keys( %{$to_exist} ) );
}
else {
push( @new_array, @exiting_a );
push( @new_array, keys( %{$to_exist} ) );
}
$self->set_array( var => $opts{var}, vals => \@new_array );
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>