File: modify_needle

package info (click to toggle)
openqa 5.1764349525.ffb594867-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,264 kB
  • sloc: perl: 57,599; sql: 26,462; javascript: 8,466; xml: 2,229; sh: 1,705; makefile: 443; python: 249
file content (60 lines) | stat: -rwxr-xr-x 1,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl

# Copyright 2015-2021 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

=head1 modify_needle

modify_needle - manipulate needle (tags) on command line

=head1 SYNOPSIS

modify_needle [OPTIONS] FILE.json [FILEs...]

You can pass multiple files to e.g. 

modify_needle --add-tags COOLTHING *the-cool-needle*.json

=head1 OPTIONS

=over 4

=item B<--add-tags>

check the needle and add the given tags (comma separated) if not yet present

=item B<--help, -h>

print help

=back

=cut

use Mojo::Base -strict, -signatures;
use Mojo::File qw(path);
use Cpanel::JSON::XS ();
use Getopt::Long;

sub usage ($r) { require Pod::Usage; Pod::Usage::pod2usage($r) }

my %options;
GetOptions(\%options, "add-tags=s", "help|h",) or usage(1);

usage(0) if $options{help};

my @add_tags = split(q{,}, $options{'add-tags'});

for my $needle (@ARGV) {
    my $info = Cpanel::JSON::XS->new->relaxed->decode(path($needle)->slurp);

    my $changed = 0;
    my %tags = map { $_ => 1 } @{$info->{tags}};
    for my $at (@add_tags) {
        $changed = 1 unless $tags{$at};
        $tags{$at} = 1;
    }
    $info->{tags} = [sort keys %tags];
    next unless $changed;
    path($needle)->spew(Cpanel::JSON::XS->new->pretty->encode($info));
}