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