| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package URI::PackageURL::CLI; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 86191 | use strict; | 
|  | 2 |  |  |  |  | 14 |  | 
|  | 2 |  |  |  |  | 61 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 46 |  | 
| 5 | 2 |  |  | 2 |  | 540 | use utf8; | 
|  | 2 |  |  |  |  | 17 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 1962 | use Getopt::Long qw( GetOptionsFromArray :config gnu_compat pass_through ); | 
|  | 2 |  |  |  |  | 22125 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 8 | 2 |  |  | 2 |  | 1608 | use Pod::Usage; | 
|  | 2 |  |  |  |  | 109319 |  | 
|  | 2 |  |  |  |  | 245 |  | 
| 9 | 2 |  |  | 2 |  | 17 | use Carp; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 124 |  | 
| 10 | 2 |  |  | 2 |  | 898 | use JSON qw(encode_json); | 
|  | 2 |  |  |  |  | 8916 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 11 | 2 |  |  | 2 |  | 1512 | use Data::Dumper; | 
|  | 2 |  |  |  |  | 12665 |  | 
|  | 2 |  |  |  |  | 133 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 2 |  |  | 2 |  | 534 | use URI::PackageURL; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1686 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our $VERSION = '2.02'; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub cli_error { | 
| 18 | 0 |  |  | 0 | 0 | 0 | my ($error) = @_; | 
| 19 | 0 |  |  |  |  | 0 | $error =~ s/ at .* line \d+.*//; | 
| 20 | 0 |  |  |  |  | 0 | print STDERR "ERROR: $error\n"; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub run { | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 |  |  | 1 | 0 | 2289 | my ($class, @args) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 1 |  |  |  |  | 4 | my %options = (format => 'json'); | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 1 | 50 |  |  |  | 6 | GetOptionsFromArray( | 
| 30 |  |  |  |  |  |  | \@args, \%options, qw( | 
| 31 |  |  |  |  |  |  | help|h | 
| 32 |  |  |  |  |  |  | man | 
| 33 |  |  |  |  |  |  | version | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | download-url | 
| 36 |  |  |  |  |  |  | repository-url | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | null|0 | 
| 39 |  |  |  |  |  |  | format=s | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | json | 
| 42 |  |  |  |  |  |  | yaml | 
| 43 |  |  |  |  |  |  | dumper | 
| 44 |  |  |  |  |  |  | env | 
| 45 |  |  |  |  |  |  | ) | 
| 46 |  |  |  |  |  |  | ) or pod2usage(-verbose => 0); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 1 | 50 |  |  |  | 877 | pod2usage(-exitstatus => 0, -verbose => 2) if defined $options{man}; | 
| 49 | 1 | 50 |  |  |  | 3 | pod2usage(-exitstatus => 0, -verbose => 0) if defined $options{help}; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 | 50 |  |  |  | 3 | if (defined $options{version}) { | 
| 52 | 0 |  |  |  |  | 0 | print "PackageURL CLI v$URI::PackageURL::VERSION\n"; | 
| 53 | 0 |  |  |  |  | 0 | return 0; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 1 |  |  |  |  | 3 | my ($purl_string) = @args; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 1 | 50 |  |  |  | 4 | pod2usage(-verbose => 1) if !$purl_string; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 1 | 50 |  |  |  | 5 | $options{format} = 'json'   if defined $options{json}; | 
| 61 | 1 | 50 |  |  |  | 5 | $options{format} = 'yaml'   if defined $options{yaml}; | 
| 62 | 1 | 50 |  |  |  | 3 | $options{format} = 'dumper' if defined $options{dumper}; | 
| 63 | 1 | 50 |  |  |  | 3 | $options{format} = 'env'    if defined $options{env}; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 1 |  |  |  |  | 1 | my $purl = eval { URI::PackageURL->from_string($purl_string) }; | 
|  | 1 |  |  |  |  | 7 |  | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 1 | 50 |  |  |  | 3 | if ($@) { | 
| 68 | 0 |  |  |  |  | 0 | cli_error($@); | 
| 69 | 0 |  |  |  |  | 0 | return 1; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 1 |  |  |  |  | 4 | my $purl_urls = $purl->to_urls; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 1 | 50 |  |  |  | 4 | if ($options{'download-url'}) { | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 | 0 |  |  |  | 0 | return 2 unless defined $purl_urls->{download}; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 | 0 |  |  |  | 0 | print $purl_urls->{download} . (defined $options{null} ? "\0" : "\n"); | 
| 79 | 0 |  |  |  |  | 0 | return 0; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 1 | 50 |  |  |  | 4 | if ($options{'repository-url'}) { | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 0 | 0 |  |  |  | 0 | exit(2) unless defined $purl_urls->{repository}; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 0 | 0 |  |  |  | 0 | print $purl_urls->{repository} . ($options{null} ? "\0" : "\n"); | 
| 88 | 0 |  |  |  |  | 0 | return 0; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 1 | 50 |  |  |  | 3 | if ($options{format} eq 'json') { | 
| 92 | 1 |  |  |  |  | 42 | print JSON->new->canonical->pretty(1)->convert_blessed(1)->encode($purl); | 
| 93 | 1 |  |  |  |  | 16 | return 0; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 | 0 |  |  |  |  | if ($options{format} eq 'dumper') { | 
| 97 | 0 |  |  |  |  |  | print Data::Dumper->new([$purl])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump; | 
| 98 | 0 |  |  |  |  |  | return 0; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 | 0 |  |  |  |  | if ($options{format} eq 'yaml') { | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 | 0 |  |  |  |  | if (eval { require YAML::XS }) { | 
|  | 0 |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | print YAML::XS::Dump($purl); | 
| 105 | 0 |  |  |  |  |  | return 0; | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 0 | 0 |  |  |  |  | if (eval { require YAML }) { | 
|  | 0 |  |  |  |  |  |  | 
| 108 | 0 |  |  |  |  |  | print YAML::Dump($purl); | 
| 109 | 0 |  |  |  |  |  | return 0; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | cli_error 'YAML or YAML::XS module are missing'; | 
| 113 | 0 |  |  |  |  |  | return 255; | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 0 | 0 |  |  |  |  | if ($options{format} eq 'env') { | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | my %PURL_ENVS = ( | 
| 120 |  |  |  |  |  |  | PURL            => $purl->to_string, | 
| 121 |  |  |  |  |  |  | PURL_TYPE       => $purl->type, | 
| 122 |  |  |  |  |  |  | PURL_NAMESPACE  => $purl->namespace, | 
| 123 |  |  |  |  |  |  | PURL_NAME       => $purl->name, | 
| 124 |  |  |  |  |  |  | PURL_VERSION    => $purl->version, | 
| 125 |  |  |  |  |  |  | PURL_SUBPATH    => $purl->subpath, | 
| 126 | 0 |  |  |  |  |  | PURL_QUALIFIERS => (join ' ', sort keys %{$purl->qualifiers}), | 
|  | 0 |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | ); | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | # Preserve order | 
| 130 | 0 |  |  |  |  |  | my @PURL_ENVS = qw(PURL PURL_TYPE PURL_NAMESPACE PURL_NAME PURL_VERSION PURL_SUBPATH PURL_QUALIFIERS); | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 0 |  |  |  |  |  | my $qualifiers = $purl->qualifiers; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 0 |  |  |  |  |  | foreach my $qualifier (sort keys %{$qualifiers}) { | 
|  | 0 |  |  |  |  |  |  | 
| 135 | 0 |  |  |  |  |  | my $key = "PURL_QUALIFIER_$qualifier"; | 
| 136 | 0 |  |  |  |  |  | push @PURL_ENVS, $key; | 
| 137 | 0 |  |  |  |  |  | $PURL_ENVS{$key} = $qualifiers->{$qualifier}; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 0 | 0 |  |  |  |  | if ($purl_urls) { | 
| 141 | 0 | 0 |  |  |  |  | if (defined $purl_urls->{download}) { | 
| 142 | 0 |  |  |  |  |  | push @PURL_ENVS, 'PURL_DOWNLOAD_URL'; | 
| 143 | 0 |  |  |  |  |  | $PURL_ENVS{PURL_DOWNLOAD_URL} = $purl_urls->{download}; | 
| 144 |  |  |  |  |  |  | } | 
| 145 | 0 | 0 |  |  |  |  | if (defined $purl_urls->{repository}) { | 
| 146 | 0 |  |  |  |  |  | push @PURL_ENVS, 'PURL_REPOSITORY_URL'; | 
| 147 | 0 |  |  |  |  |  | $PURL_ENVS{PURL_REPOSITORY_URL} = $purl_urls->{repository}; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | foreach my $key (@PURL_ENVS) { | 
| 152 | 0 |  | 0 |  |  |  | print sprintf qq{%s="%s"\n}, $key, $PURL_ENVS{$key} || q{}; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 0 |  |  |  |  |  | return 0; | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | 1; | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | __END__ |