CLI.pm

filter: Statementstmt 85.0% 182 / 214 Branchbran 61.7% 58 / 94 Conditioncond 52.6% 49 / 93 MC/DCmcdc 14.2% 7 / 49 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 1 totaltotal 67.3% 321 / 477 CC 87 SCAR 31.4
CC
87
Cov
72%
_config
51.7
run
47.7
_check
17.2
CRAP 23.2
↑ Summary CLI/Util.pm »
1 # SPDX-FileCopyrightText: SUSE LLC
2 # SPDX-License-Identifier: GPL-2.0-or-later
3  
4 package Cavil::CLI;
5 6 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Cavil::CLI::Archive;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Cavil::CLI::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Cavil::CLI::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Cavil::CLI::Progress;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Cavil::CLI::Util qw(gate render_json render_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Mojo::File qw(path tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Mojo::JSON qw(to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Mojo::Util qw(encode extract_usage getopt);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 our $VERSION = '0.04';
18  
19 6 ▶has client => sub { Cavil::CLI::Client->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 # Exit codes: clean, gate failed, usage/config problem, server/connection error.
22 6 ▶use constant {EXIT_CLEAN => 0, EXIT_GATE => 1, EXIT_USAGE => 2, EXIT_SERVER => 3};
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Refuse before uploading an archive the server would reject anyway, so an accidental huge file (a build
25 # artifact, a data dump) fails fast with guidance instead of a slow doomed upload. Defaults to Cavil's own
26 # request limit; CAVIL_MAX_UPLOAD_MB raises it for an instance configured to accept more.
27 6 ▶use constant UPLOAD_MAX_MB => 250;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 9 ▶sub _max_upload_bytes () {
Subroutine _max_upload_bytes: called CC=2 SCAR=6.9
30 9 ▶ return (defined $ENV{CAVIL_MAX_UPLOAD_MB} ? $ENV{CAVIL_MAX_UPLOAD_MB} : UPLOAD_MAX_MB) * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'CAVIL_MAX_UPLOAD_MB'} ? : 1 8 9
31 }
32  
33 18 ▶sub run ($self) {
Subroutine run: called CC=26 SCAR=38.6
Pod undocumented
34 18 getopt
35 'url=s' => \my $url_opt,
36 'name=s' => \my $name,
37 'priority=i' => \my $priority,
38 'fail-on-risk=i' => \my $fail_on_risk,
39 'external-link=s' => \my $external_link,
40 'sbom:s' => \my $sbom,
41 'notice:s' => \my $notice,
42 'respect-gitignore' => \my $respect_gitignore,
43 'exclude-path=s@' => \my $exclude_path,
44 'timeout=i' => \my $timeout,
45 'format=s' => \my $format,
46 'show' => \my $show,
47 'no-color' => \my $no_color,
48 'quiet' => \my $quiet,
49 'h|help' => \my $help;
50  
51 18 ▶ return print(extract_usage) ? EXIT_CLEAN : EXIT_CLEAN if $help;
Branch: 2 branches
BranchTrueFalseTotal
if $help 0 18 18
print(extract_usage()) ? : 0 0 0
52  
53 18 my ($command, $path) = @ARGV;
54 18 ▶ unless (defined $command && ($command eq 'check' || $command eq 'whoami' || $command eq 'config')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command and $command eq "check" || $command eq "whoami" || $command eq "config") 0 18 18
Condition66%6/9
defined $command and $command eq "check" || $command eq "whoami" || $command eq "config"
!ll&&!rl&&r
0018
$command eq "check" or $command eq "whoami" or $command eq "config"
l!l&&r!l&&!r
1620
$command eq "check" or $command eq "whoami"
l!l&&r!l&&!r
972
MC/DC0%0/4
defined $command and $command eq "check" || $command eq "whoami" || $command eq "config"
defined $command $command eq "check" $command eq "whoami" $command eq "config"
Truth table60%3/5
A and B or C or D
A= defined $commandB= $command eq "check"C= $command eq "whoami"D= $command eq "config"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
55 0 print STDERR extract_usage;
56 0 return EXIT_USAGE;
57 }
58  
59 18 my $config = Cavil::CLI::Config->new;
60  
61 # Saving settings needs no server, and is the only place a URL may be given: see below.
62 18 ▶ return $self->_config($config, $url_opt, $show) if $command eq 'config';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "config" 2 16 18
63  
64 16 ▶ if (defined $url_opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url_opt) 1 15 16
65 1 print STDERR "--url only applies to 'cavil-cli config'; set CAVIL_URL (with CAVIL_API_KEY) to aim elsewhere\n";
66 1 return EXIT_USAGE;
67 }
68  
69 # Server and token are resolved together, from one source, and never mixed. Taking the URL from one place and
70 # the token from another is how a token saved for one instance ends up being sent to a different one. For the
71 # same reason there is no --token at all: an argument is world-readable in ps and stays in shell history.
72 my ($url, $token)
73 = defined $ENV{CAVIL_URL}
74 15 ▶ || defined $ENV{CAVIL_API_KEY} ? ($ENV{CAVIL_URL}, $ENV{CAVIL_API_KEY}) : @{$config->load}{qw(url token)};
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'CAVIL_URL'} || defined $ENV{'CAVIL_API_KEY'} ? : 14 1 15
Condition100%3/3
defined $ENV{"CAVIL_URL"} or defined $ENV{"CAVIL_API_KEY"}
l!l&&r!l&&!r
1311
MC/DC100%2/2
defined $ENV{"CAVIL_URL"} or defined $ENV{"CAVIL_API_KEY"}
defined $ENV{"CAVIL_URL"} defined $ENV{"CAVIL_API_KEY"}
Truth table100%3/3
A or B
A= defined $ENV{"CAVIL_URL"}B= defined $ENV{"CAVIL_API_KEY"}
ABresult
1X1
011
000
75 15 ▶ unless (defined $url && defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url and defined $token) 2 13 15
Condition100%3/3
defined $url and defined $token
!ll&&!rl&&r
1113
MC/DC100%2/2
defined $url and defined $token
defined $url defined $token
Truth table100%3/3
A and B
A= defined $urlB= defined $token
ABresult
0X0
100
111
76 2 print STDERR "A Cavil URL and API token are required, from the same source:\n"
77 . " run 'cavil-cli config' to save both, or set CAVIL_URL and CAVIL_API_KEY together\n";
78 2 return EXIT_USAGE;
79 }
80  
81 13 ▶ $format //= 'text';
Condition100%2/2
$format //= "text"
l!l
112
MC/DC100%1/1
$format //= "text"
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
82 13 ▶ return $self->_whoami({url => $url, token => $token, format => $format}) if $command eq 'whoami';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "whoami" 4 9 13
83  
84 9 my %seen;
85 0 ▶ my @exclude_paths = grep { !$seen{$_}++ } map {s{/\z}{}r} grep {length} @{$exclude_path // []}, split /[\s,]+/,
Condition50%2/4
$exclude_path // []
l!l
09
$ENV{"CAVIL_EXCLUDE_PATHS"} // ""
l!l
09
MC/DC0%0/1
$exclude_path // []
$exclude_path
MC/DC0%0/1
$ENV{"CAVIL_EXCLUDE_PATHS"} // ""
$ENV{"CAVIL_EXCLUDE_PATHS"}
Truth table50%1/2
A
A= $exclude_path
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"CAVIL_EXCLUDE_PATHS"}
Aresult
11
00
86 9 ($ENV{CAVIL_EXCLUDE_PATHS} // '');
87  
88 9 ▶ my $color = $format eq 'text' && !$no_color && !$ENV{NO_COLOR} && -t STDOUT;
Condition33%3/9
$format eq 'text' && !$no_color && !$ENV{'NO_COLOR'} && -t STDOUT
!ll&&!rl&&r
900
$format eq "text" and not $no_color and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
090
$format eq "text" and not $no_color
!ll&&!rl&&r
009
MC/DC0%0/4
$format eq 'text' && !$no_color && !$ENV{'NO_COLOR'} && -t STDOUT
$format eq "text" not $no_color not $ENV{"NO_COLOR"} -t STDOUT
Truth table20%1/5
A and not(B) and not(C) && D
A= $format eq "text"B= not $no_colorC= not $ENV{"NO_COLOR"}D= -t STDOUT
ABCDresult
0XXX0
10XX0
110X0
11100
11111
89  
90 9 ▶ return $self->_check(
Branch: 2 branches
BranchTrueFalseTotal
$respect_gitignore ? : 0 9 9
!$quiet && -t STDERR ? : 0 9 9
Condition44%4/9
$path // "."
l!l
90
$priority // 5
l!l
09
$timeout // 900
l!l
09
not $quiet and -t STDERR
!ll&&!rl&&r
090
MC/DC0%0/1
$path // "."
$path
MC/DC0%0/1
$priority // 5
$priority
MC/DC0%0/1
$timeout // 900
$timeout
MC/DC0%0/2
not $quiet and -t STDERR
not $quiet -t STDERR
Truth table50%1/2
A
A= $path
Aresult
11
00
Truth table50%1/2
A
A= $priority
Aresult
11
00
Truth table50%1/2
A
A= $timeout
Aresult
11
00
Truth table33%1/3
not(A) and B
A= not $quietB= -t STDERR
ABresult
0X0
100
111
91 $path // '.',
92 {
93 url => $url,
94 token => $token,
95 name => $name,
96 priority => $priority // 5,
97 fail_on_risk => $fail_on_risk,
98 external_link => $external_link,
99 sbom => $sbom,
100 notice => $notice,
101 respect_gitignore => $respect_gitignore ? 1 : 0,
102 exclude_paths => \@exclude_paths,
103 timeout => $timeout // 900,
104 format => $format,
105 color => $color,
106 progress => (!$quiet && -t STDERR ? 1 : 0)
107 }
108 );
109 }
110  
111 # Save the Cavil URL and API token to the config file, or show what is stored. The token is read from a hidden
112 # prompt, or from stdin when piped; there is no option to pass it, anywhere. --show never prints it.
113 2 ▶sub _config ($self, $config, $url, $show) {
Subroutine _config: called CC=16 SCAR=39.5
114 2 my $saved = $config->load;
115  
116 2 ▶ if ($show) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show) 1 1 2
117 1 print STDOUT 'Configuration file: ' . $config->file . "\n";
118 1 ▶ print STDOUT ' url: ' . ($saved->{url} // '(not set)') . "\n";
Condition50%1/2
$saved->{"url"} // "(not set)"
l!l
10
MC/DC0%0/1
$saved->{"url"} // "(not set)"
$saved->{"url"}
Truth table50%1/2
A
A= $saved->{"url"}
Aresult
11
00
119 1 ▶ print STDOUT ' token: ' . ($saved->{token} ? '******** (set)' : '(not set)') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$saved->{'token'} ? : 1 0 1
120 1 return EXIT_CLEAN;
121 }
122  
123 # The URL is not secret, so it may come from --url; otherwise prompt, offering the current value as default.
124 1 ▶ if (defined $url) { $saved->{url} = $url }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) { } 1 0 1
125 else {
126 0 my $current = $saved->{url};
127 0 ▶ print STDERR 'Cavil URL' . (defined $current ? " [$current]" : '') . ': ';
Branch: 1 branch
BranchTrueFalseTotal
defined $current ? : 0 0 0
128 0 my $line = readline STDIN;
129 0 ▶ chomp $line if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
130 0 ▶ $saved->{url} = $line if defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line and length $line 0 0 0
Condition0%0/3
defined $line and length $line
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line and length $line
defined $line length $line
Truth table0%0/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
131 }
132  
133 # The token is secret: hidden prompt on a terminal, one stdin line when piped. Empty input keeps the existing.
134 1 ▶ my $entered = _read_secret('Cavil API token' . ($saved->{token} ? ' [keep existing]' : '') . ': ');
Branch: 1 branch
BranchTrueFalseTotal
$saved->{'token'} ? : 0 1 1
135 1 ▶ $saved->{token} = $entered if defined $entered && length $entered;
Branch: 1 branch
BranchTrueFalseTotal
if defined $entered and length $entered 1 0 1
Condition33%1/3
defined $entered and length $entered
!ll&&!rl&&r
001
MC/DC0%0/2
defined $entered and length $entered
defined $entered length $entered
Truth table33%1/3
A and B
A= defined $enteredB= length $entered
ABresult
0X0
100
111
136  
137 1 ▶ unless (length($saved->{url} // '') && length($saved->{token} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length($saved->{"url"} // "") and length($saved->{"token"} // "")) 0 1 1
Condition42%3/7
length($saved->{"url"} // "") and length($saved->{"token"} // "")
!ll&&!rl&&r
001
$saved->{"url"} // ""
l!l
10
$saved->{"token"} // ""
l!l
10
MC/DC0%0/2
length($saved->{"url"} // "") and length($saved->{"token"} // "")
$saved->{"url"} $saved->{"token"}
Truth table33%1/3
A and B
A= $saved->{"url"}B= $saved->{"token"}
ABresult
0X0
100
111
138 0 print STDERR "A Cavil URL and API token are both required\n";
139 0 return EXIT_USAGE;
140 }
141  
142 1 $config->save($saved);
143 1 print STDOUT 'Saved configuration to ' . $config->file . " (token hidden)\n";
144 1 return EXIT_CLEAN;
145 }
146  
147 # Read one line without echoing it, so a typed token never appears on screen. Only toggles the terminal when
148 # stdin is one; piped input (tests, scripts) is read as a plain line.
149 1 ▶sub _read_secret ($prompt) {
Subroutine _read_secret: called CC=4 SCAR=15.2
150 1 print STDERR $prompt;
151 1 my $hide = -t STDIN;
152 1 ▶ system('stty', '-echo') if $hide;
Branch: 1 branch
BranchTrueFalseTotal
if $hide 0 1 1
153 1 my $line = readline STDIN;
154 0 ▶ if ($hide) { system('stty', 'echo'); print STDERR "\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($hide) 0 1 1
155 1 ▶ chomp $line if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 1 0 1
156 1 return $line;
157 }
158  
159 # Confirm the url and token work by asking the instance who the token belongs to, and time the round trip so a
160 # user can also see the instance is reachable and responsive.
161 4 ▶sub _whoami ($self, $opts) {
Subroutine _whoami: called CC=7 SCAR=19.6
162 4 my $client = $self->client->url($opts->{url})->token($opts->{token});
163  
164 4 my $t0 = Time::HiRes::time;
165 4 my $info = eval { $client->whoami };
166 4 my $ms = int((Time::HiRes::time - $t0) * 1000);
167 4 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 3 4
168 1 chomp(my $msg = $err);
169 1 print STDERR "Not authenticated with $opts->{url}:\n $msg\n"
170 . "Check the saved config ('cavil-cli config --show'), or CAVIL_URL / CAVIL_API_KEY.\n";
171 1 return EXIT_SERVER;
172 }
173  
174 3 ▶ if ($opts->{format} eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"format"} eq "json") 1 2 3
175 1 print STDOUT encode('UTF-8', to_json({%$info, round_trip_ms => $ms}) . "\n");
176 1 return EXIT_CLEAN;
177 }
178  
179 2 ▶ my $roles = @{$info->{roles} || []} ? join(', ', @{$info->{roles}}) : 'none';
Branch: 1 branch
BranchTrueFalseTotal
@{$info->{'roles'} or [];} ? : 2 0 2
Condition50%1/2
$info->{'roles'} || []
l!l
20
MC/DC0%0/1
$info->{'roles'} || []
$info->{'roles'}
Truth table50%1/2
A
A= $info->{'roles'}
Aresult
11
00
180 2 print STDOUT encode('UTF-8',
181 2 ▶ "Authenticated as $info->{user} (id @{[$info->{id} // '?']}) on $opts->{url}\n"
Condition50%1/2
$info->{"id"} // "?"
l!l
20
MC/DC0%0/1
$info->{"id"} // "?"
$info->{"id"}
Truth table50%1/2
A
A= $info->{"id"}
Aresult
11
00
182 . " roles: $roles\n"
183 2 ▶ . " write access: @{[$info->{write_access} ? 'yes' : 'no']}\n"
Branch: 1 branch
BranchTrueFalseTotal
$info->{'write_access'} ? : 0 2 2
184 . " round-trip: $ms ms\n");
185 2 return EXIT_CLEAN;
186 }
187  
188 # Upload the working tree, wait for the standard review, then print the verdict and gate the exit code on risk.
189 9 ▶sub _check ($self, $dir, $opts) {
Subroutine _check: called CC=14 SCAR=28.4
190 9 my $client = $self->client->url($opts->{url})->token($opts->{token});
191 9 my $progress = Cavil::CLI::Progress->new(enabled => $opts->{progress});
192 0 ▶ $client->on_wait(sub { $progress->spin });
Subroutine __ANON__: not called CC=1 SCAR=1.2
193  
194 # Confirm access before packaging what may be a large tree, and fail with a clear reason if the key cannot.
195 9 eval { $client->whoami };
196 9 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 9 9
197 0 $progress->finish;
198 0 chomp(my $msg = $err);
199 0 print STDERR "Not authenticated with $opts->{url}:\n $msg\n";
200 0 return EXIT_SERVER;
201 }
202  
203 9 ▶ my $name = $opts->{name} // _package_name($dir);
Condition66%2/3
$opts->{"name"} // _package_name($dir)
l!l&&r!l&&!r
810
MC/DC0%0/2
$opts->{"name"} // _package_name($dir)
$opts->{"name"} _package_name($dir)
Truth table66%2/3
A // B
A= $opts->{"name"}B= _package_name($dir)
ABresult
1X1
011
000
204 9 ▶ my $link = $opts->{external_link} // _external_link($dir);
Condition33%1/3
$opts->{"external_link"} // _external_link($dir)
l!l&&r!l&&!r
009
MC/DC0%0/2
$opts->{"external_link"} // _external_link($dir)
$opts->{"external_link"} _external_link($dir)
Truth table33%1/3
A // B
A= $opts->{"external_link"}B= _external_link($dir)
ABresult
1X1
011
000
205  
206 9 my $tmp = tempdir;
207 9 my $archive = $tmp->child('upload.tar.gz')->to_string;
208 9 $progress->start("Packaging $name");
209 9 my $checksum = eval {
210 Cavil::CLI::Archive->new(
211 dir => $dir,
212 respect_gitignore => $opts->{respect_gitignore},
213 excludes => $opts->{exclude_paths}
214 9 )->build($archive);
215 };
216 0 ▶ if (my $err = $@) { $progress->finish; chomp(my $m = $err); print STDERR "$m\n"; return EXIT_USAGE; }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 9 9
217  
218 9 my $max_bytes = _max_upload_bytes();
219 9 my $size = -s $archive;
220 9 ▶ if ($size > $max_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $max_bytes) 1 8 9
221 1 $progress->finish;
222 1 printf STDERR "The archive is %d MiB, over the %d MiB upload limit. Trim it with a .cavilignore file or "
223 . "--exclude-path (or remove large generated files), then try again.\n", $size / 1024 / 1024,
224 $max_bytes / 1024 / 1024;
225 1 return EXIT_USAGE;
226 }
227  
228 8 $progress->start('Uploading');
229 8 my $saved = eval {
230 $client->upload($archive,
231 8 {name => $name, priority => $opts->{priority}, checksum => $checksum, external_link => $link});
232 };
233 8 ▶ if (my $err = $@) { $progress->finish; chomp(my $m = $err); print STDERR "$m\n"; return EXIT_SERVER; }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 3 5 8
234 5 my $id = $saved->{saved}{id};
235  
236 5 $progress->start('Reviewing');
237 5 my $json = eval { _poll($client, $id, $opts->{timeout}, $progress) };
238 5 my $poll_err = $@;
239 5 $progress->finish;
240 0 ▶ if ($poll_err) { chomp(my $m = $poll_err); print STDERR "$m\n"; return EXIT_SERVER }
Branch: 1 branch
BranchTrueFalseTotal
if ($poll_err) 0 5 5
241  
242 5 my $info = _verdict($json, $opts, $id, $name);
243  
244 # Optional documents, requested when --sbom / --notice are present (with or without an explicit path).
245 5 ▶ $progress->start('Fetching documents') if defined $opts->{sbom} || defined $opts->{notice};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"sbom"} or defined $opts->{"notice"} 1 4 5
Condition66%2/3
defined $opts->{"sbom"} or defined $opts->{"notice"}
l!l&&r!l&&!r
104
MC/DC50%1/2
defined $opts->{"sbom"} or defined $opts->{"notice"}
defined $opts->{"sbom"} defined $opts->{"notice"}
Truth table66%2/3
A or B
A= defined $opts->{"sbom"}B= defined $opts->{"notice"}
ABresult
1X1
011
000
246 $info->{sbom} = _save_document($client, $id, 'spdx', $opts->{sbom}, "$name.spdx.json", $opts->{timeout})
247 5 ▶ if defined $opts->{sbom};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"sbom"} 1 4 5
248 $info->{notice} = _save_document($client, $id, 'notice', $opts->{notice}, "$name.NOTICE.txt", $opts->{timeout})
249 5 ▶ if defined $opts->{notice};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"notice"} 0 5 5
250  
251 5 ▶ my $output = $opts->{format} eq 'json' ? render_json($info) : render_text($info, color => $opts->{color});
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'format'} eq 'json' ? : 0 5 5
252 5 print STDOUT encode('UTF-8', $output);
253  
254 5 ▶ return gate($info->{risk}, $info->{threshold})->{failed} ? EXIT_GATE : EXIT_CLEAN;
Branch: 1 branch
BranchTrueFalseTotal
gate($info->{'risk'}, $info->{'threshold'})->{'failed'} ? : 2 3 5
255 }
256  
257 # Server pipeline stages, mapped to what the progress line shows, so the wait names what is happening rather
258 # than sitting on a bare "Reviewing".
259 6 my %STAGE_LABEL = (
260 queued => 'Queued for review',
261 unpacking => 'Unpacking',
262 indexing => 'Indexing',
263 analyzing => 'Analyzing',
264 finalizing => 'Finalizing report'
265 );
266  
267 # Poll the report until it is ready, showing the current pipeline stage between attempts; die on timeout.
268 5 ▶sub _poll ($client, $id, $timeout, $progress) {
Subroutine _poll: called CC=6 SCAR=18.2
269 5 my $deadline = Time::HiRes::time + $timeout;
270 5 while (1) {
271 6 my $res = $client->report($id, 'json');
272 6 ▶ return $res->{data} if $res->{ready};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"ready"} 5 1 6
273 1 ▶ $progress->label($STAGE_LABEL{$res->{stage} // ''} // 'Reviewing');
Condition50%2/4
$STAGE_LABEL{$res->{"stage"} // ""} // "Reviewing"
l!l
10
$res->{"stage"} // ""
l!l
10
MC/DC0%0/1
$STAGE_LABEL{$res->{"stage"} // ""} // "Reviewing"
$STAGE_LABEL{$res->{"stage"} // ""}
MC/DC0%0/1
$res->{"stage"} // ""
$res->{"stage"}
Truth table50%1/2
A
A= $STAGE_LABEL{$res->{"stage"} // ""}
Aresult
11
00
Truth table50%1/2
A
A= $res->{"stage"}
Aresult
11
00
274 1 ▶ die "Timed out after ${timeout}s waiting for the review (package $id)\n" if Time::HiRes::time >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() >= $deadline 0 1 1
275 1 for (1 .. 10) { $progress->spin; Time::HiRes::sleep(0.2) }
276 }
277 }
278  
279 # Normalize a report response into the flat verdict the renderers consume.
280 5 ▶sub _verdict ($json, $opts, $id, $name) {
Subroutine _verdict: called CC=9 SCAR=22.9
281 5 ▶ my $pkg = $json->{package} // {};
Condition50%1/2
$json->{"package"} // {}
l!l
50
MC/DC0%0/1
$json->{"package"} // {}
$json->{"package"}
Truth table50%1/2
A
A= $json->{"package"}
Aresult
11
00
282 5 ▶ my $rep = $json->{report} // {};
Condition50%1/2
$json->{"report"} // {}
l!l
50
MC/DC0%0/1
$json->{"report"} // {}
$json->{"report"}
Truth table50%1/2
A
A= $json->{"report"}
Aresult
11
00
283  
284 5 ▶ my $accept = $json->{acceptable_risk} // 3;
Condition50%1/2
$json->{"acceptable_risk"} // 3
l!l
50
MC/DC0%0/1
$json->{"acceptable_risk"} // 3
$json->{"acceptable_risk"}
Truth table50%1/2
A
A= $json->{"acceptable_risk"}
Aresult
11
00
285 5 ▶ my $threshold = $opts->{fail_on_risk} // ($accept + 1);
Condition100%2/2
$opts->{"fail_on_risk"} // $accept + 1
l!l
14
MC/DC100%1/1
$opts->{"fail_on_risk"} // $accept + 1
$opts->{"fail_on_risk"}
Truth table100%2/2
A
A= $opts->{"fail_on_risk"}
Aresult
11
00
286  
287 my @licenses
288 4 ▶ = sort { $b->{risk} <=> $a->{risk} || $a->{name} cmp $b->{name} }
Condition66%2/3
$Cavil::CLI::b->{'risk'} <=> $Cavil::CLI::a->{'risk'} || $Cavil::CLI::a->{'name'} cmp $Cavil::CLI::b->{'name'}
l!l&&r!l&&!r
220
MC/DC0%0/2
$Cavil::CLI::b->{'risk'} <=> $Cavil::CLI::a->{'risk'} || $Cavil::CLI::a->{'name'} cmp $Cavil::CLI::b->{'name'}
$Cavil::CLI::b->{'risk'} <=> $Cavil::CLI::a->{'risk'} $Cavil::CLI::a->{'name'} cmp $Cavil::CLI::b->{'name'}
Truth table66%2/3
A || B
A= $Cavil::CLI::b->{'risk'} <=> $Cavil::CLI::a->{'risk'}B= $Cavil::CLI::a->{'name'} cmp $Cavil::CLI::b->{'name'}
ABresult
1X1
011
000
289 9 map { {name => $_, risk => $rep->{licenses}{$_}{risk}, spdx => $rep->{licenses}{$_}{spdx}} }
290 5 ▶ keys %{$rep->{licenses} // {}};
Condition50%1/2
$rep->{"licenses"} // {}
l!l
50
MC/DC0%0/1
$rep->{"licenses"} // {}
$rep->{"licenses"}
Truth table50%1/2
A
A= $rep->{"licenses"}
Aresult
11
00
291  
292 5 my $risk = $json->{risk};
293 return {
294 id => $id,
295 name => $name,
296 risk => $risk,
297 acceptable_risk => $accept,
298 threshold => $threshold,
299 state => $pkg->{state},
300 unresolved => $pkg->{unresolved_matches} // 0,
301 licenses => \@licenses,
302 report_url => "$opts->{url}/reviews/details/$id",
303 5 ▶ gate => (gate($risk, $threshold)->{failed} ? 'fail' : 'pass')
Branch: 1 branch
BranchTrueFalseTotal
gate($risk, $threshold)->{'failed'} ? : 2 3 5
Condition50%1/2
$pkg->{"unresolved_matches"} // 0
l!l
50
MC/DC0%0/1
$pkg->{"unresolved_matches"} // 0
$pkg->{"unresolved_matches"}
Truth table50%1/2
A
A= $pkg->{"unresolved_matches"}
Aresult
11
00
304 };
305 }
306  
307 # Download a generated document to a file, polling while the server builds it. Returns the path written.
308 1 ▶sub _save_document ($client, $id, $key, $requested, $default, $timeout) {
Subroutine _save_document: called CC=5 SCAR=17.0
309 1 ▶ my $file = length($requested // '') ? $requested : $default;
Branch: 1 branch
BranchTrueFalseTotal
length($requested // '') ? : 1 0 1
Condition50%1/2
$requested // ""
l!l
10
MC/DC0%0/1
$requested // ""
$requested
Truth table50%1/2
A
A= $requested
Aresult
11
00
310 1 my $deadline = Time::HiRes::time + $timeout;
311 1 while (1) {
312 2 my $body = $client->document($id, $key);
313 2 ▶ if (defined $body) { path($file)->spew($body); return $file }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 1 1 2
314 1 ▶ last if Time::HiRes::time >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() >= $deadline 0 1 1
315 1 Time::HiRes::sleep(1);
316 }
317 0 print STDERR "Timed out waiting for the $key document\n";
318 0 return undef;
319 }
320  
321 # A package name Cavil accepts (^[A-Za-z0-9.-]+$), derived from the directory being checked.
322 1 ▶sub _package_name ($dir) {
Subroutine _package_name: called CC=2 SCAR=7.0
323 1 my $base = path($dir)->to_abs->basename;
324 1 $base =~ s/[^A-Za-z0-9.\-]+/-/g;
325 1 $base =~ s/^-+|-+$//g;
326 1 ▶ return length $base ? $base : 'project';
Branch: 1 branch
BranchTrueFalseTotal
length $base ? : 1 0 1
327 }
328  
329 # Best-effort provenance for the review: the git remote and short commit, when the tree is a checkout.
330 9 ▶sub _external_link ($dir) {
Subroutine _external_link: called CC=3 SCAR=18.4
331 9 ▶ my $rev = _git($dir, 'rev-parse', '--short', 'HEAD') // return undef;
Condition50%1/2
_git($dir, "rev-parse", "--short", "HEAD") // (return undef)
l!l
09
MC/DC0%0/1
_git($dir, "rev-parse", "--short", "HEAD") // (return undef)
_git($dir, "rev-parse", "--short", "HEAD")
Truth table50%1/2
A
A= _git($dir, "rev-parse", "--short", "HEAD")
Aresult
11
00
332 0 my $url = _git($dir, 'config', '--get', 'remote.origin.url');
333 0 ▶ return defined $url ? "$url\@$rev" : $rev;
Branch: 1 branch
BranchTrueFalseTotal
defined $url ? : 0 0 0
334 }
335  
336 9 ▶sub _git ($dir, @args) {
Subroutine _git: called CC=4 SCAR=18.7
337 9 my $cmd = join ' ', 'git', '-C', quotemeta($dir), map { quotemeta $_ } @args;
338 9 my $out = `$cmd 2>/dev/null`;
339 9 ▶ return undef if !defined $out || $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $out or $? != 0 9 0 9
Condition33%1/3
not defined $out or $? != 0
l!l&&r!l&&!r
090
MC/DC0%0/2
not defined $out or $? != 0
not defined $out $? != 0
Truth table33%1/3
not(A) or B
A= not defined $outB= $? != 0
ABresult
1X1
011
000
340 0 chomp $out;
341 0 ▶ return length $out ? $out : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $out ? : 0 0 0
342 }
343  
344 6 1;
345  
346 =encoding utf8
347  
348 =head1 NAME
349  
350 Cavil::CLI - Submit a project to Cavil for a legal review and gate CI on the result
351  
352 =head1 SYNOPSIS
353  
354 Usage: cavil-cli <command> [DIR] [OPTIONS]
355  
356 # Save the URL and token once (prompts for the token without echoing it)
357 cavil-cli config --url https://legaldb.suse.de
358  
359 # Confirm the URL and token are set up right (and time the round trip)
360 cavil-cli whoami
361  
362 # Upload the current directory for a legal review and print the verdict
363 cavil-cli check
364  
365 # Check another directory, and also download the SBOM
366 cavil-cli check ./project --sbom
367  
368 # Machine-readable output for CI (URL and token from the environment)
369 CAVIL_URL=https://legaldb.suse.de CAVIL_API_KEY=1234 cavil-cli check --format json
370  
371 Commands:
372 check [DIR] Upload a project for a legal review and report its licensing risk (default DIR: .)
373 whoami Show the user the token belongs to, to verify login
374 config Save the URL and token to ~/.config/cavil-cli (--show to display, token masked)
375  
376 Credentials come from the saved config ("cavil-cli config"), or CAVIL_URL/CAVIL_API_KEY in CI, always as a
377 pair from one source. There is no --token (an argument is world-readable in ps and stays in shell history),
378 and --url is only accepted by "config", since aiming elsewhere would send it a token saved for this server.
379  
380 The uploaded archive is the working tree as it sits on disk, including installed vendored dependencies
381 (node_modules and the like) that a full legal review must cover; only .git and excludes are dropped. If the
382 archive is over the server's upload limit (250 MiB by default, or CAVIL_MAX_UPLOAD_MB) the check refuses
383 before uploading and tells you to trim it, so an accidental large file does not start a doomed upload.
384  
385 Options:
386 --url <url> Cavil server URL, when saving settings ("config" only)
387 --name <name> Package name to review under (default: the directory name)
388 --priority <n> Review priority 1-8 (default 5)
389 --fail-on-risk <n> Exit non-zero at this risk or above (default: the instance's acceptable risk + 1)
390 --external-link <s> Source label for traceability (default: the git remote and commit, if any)
391 --sbom [<file>] Download the SPDX SBOM (default file: <name>.spdx.json)
392 --notice [<file>] Download the NOTICE attribution file (default file: <name>.NOTICE.txt)
393 --respect-gitignore Also drop .gitignore'd paths from the archive (off by default, to keep vendored code)
394 --exclude-path <p> Drop this path from the archive (a tar pattern). Repeatable; also CAVIL_EXCLUDE_PATHS
395 --timeout <n> Seconds to wait for the review before giving up (default 900)
396 --format <format> Output format, "text" (default) or "json"
397 --no-color Disable coloured output
398 --quiet Do not show the progress line while working
399 -h, --help Show this summary of available options
400  
401 =head1 DESCRIPTION
402  
403 A command-line client that uploads the project you are working on to a Cavil instance, runs its standard legal
404 review, and reports the licensing risk, for a developer's laptop or a CI gate. See C<docs/Architecture.md> for
405 the design.
406  
407 =cut
↑ Summary CLI/Util.pm »