CLI/Util.pm

filter: Statementstmt 91.2% 52 / 57 Branchbran 70.5% 24 / 34 Conditioncond 51.6% 16 / 31 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 5 totaltotal 68.3% 110 / 161 CC 32 SCAR 20.5
CC
32
Cov
75%
render_text
15.3
have_tool
7.7
_glyph
6.4
CRAP 7.8
« CLI.pm ↑ Summary CLI/Archive.pm »
1 # SPDX-FileCopyrightText: SUSE LLC
2 # SPDX-License-Identifier: GPL-2.0-or-later
3  
4 package Cavil::CLI::Util;
5 7 ▶use Mojo::Base -strict, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Mojo::JSON qw(to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Term::ANSIColor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 our @EXPORT_OK = qw(have_tool md5_file gate render_text render_json);
14  
15 # Cavil's authoritative risk scale (see the cavil-review-note skill): 1-2 are obligation-free, 3 is file-level
16 # copyleft, 4 is strong copyleft, 5 and up escalate, 6-7 are reject-lean, 9 is unresolved/unknown. Shown next to
17 # the number so a bare "risk 4" is not left to interpretation.
18 7 my %RISK_LABEL = (
19 1 => 'public domain',
20 2 => 'permissive',
21 3 => 'weak copyleft',
22 4 => 'strong copyleft',
23 5 => 'managed obligations',
24 6 => 'restrictive obligations',
25 7 => 'non-commercial',
26 9 => 'unresolved'
27 );
28  
29 # Whether an external command is on PATH, so a missing one is a clear "please install X" up front rather than a
30 # cryptic non-zero exit after the fact.
31 19 ▶sub have_tool ($cmd) {
Subroutine have_tool: called CC=6 SCAR=20.5
Pod undocumented
32 19 ▶ return -x $cmd ? 1 : 0 if $cmd =~ m{/};
Branch: 2 branches
BranchTrueFalseTotal
if $cmd =~ m[/]u 0 19 19
-x $cmd ? : 0 0 0
33 19 ▶ -x "$_/$cmd" and return 1 for split /:/, ($ENV{PATH} // '');
Branch: 1 branch
BranchTrueFalseTotal
-x "$_/$cmd" and return 1 17 130 147
Condition50%1/2
$ENV{"PATH"} // ""
l!l
190
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
34 2 return 0;
35 }
36  
37 # MD5 of a file, streamed so a large vendored archive is never slurped into memory. Matches the hash Cavil
38 # computes server-side, so it doubles as the upload's integrity checksum.
39 15 ▶sub md5_file ($file) {
Subroutine md5_file: called CC=1 SCAR=0.0
Pod undocumented
40 15 my $md5 = Digest::MD5->new;
41 15 $md5->addfile(path($file)->open('r'));
42 15 return $md5->hexdigest;
43 }
44  
45 # The CI gate: fail at or above the risk threshold, matching the report's own acceptable/unacceptable line.
46 10 ▶sub gate ($risk, $threshold) {
Subroutine gate: called CC=3 SCAR=11.1
Pod undocumented
47 10 ▶ return {failed => (defined $risk && $risk >= $threshold) ? 1 : 0};
Branch: 1 branch
BranchTrueFalseTotal
defined $risk && $risk >= $threshold ? : 4 6 10
Condition66%2/3
defined $risk and $risk >= $threshold
!ll&&!rl&&r
064
MC/DC50%1/2
defined $risk and $risk >= $threshold
defined $risk $risk >= $threshold
Truth table66%2/3
A and B
A= defined $riskB= $risk >= $threshold
ABresult
0X0
100
111
48 }
49  
50 14 ▶sub _label ($risk) { return $RISK_LABEL{$risk} // 'unclassified' }
Subroutine _label: called CC=2 SCAR=7.6
Condition50%1/2
$RISK_LABEL{$risk} // "unclassified"
l!l
140
MC/DC0%0/1
$RISK_LABEL{$risk} // "unclassified"
$RISK_LABEL{$risk}
Truth table50%1/2
A
A= $RISK_LABEL{$risk}
Aresult
11
00
51  
52 14 ▶sub _glyph ($risk, $threshold) {
Subroutine _glyph: called CC=5 SCAR=18.6
53 14 ▶ return "\x{2717}" if defined $risk && $risk >= $threshold; # at or above the gate
Branch: 1 branch
BranchTrueFalseTotal
if defined $risk and $risk >= $threshold 4 10 14
Condition66%2/3
defined $risk and $risk >= $threshold
!ll&&!rl&&r
0104
MC/DC50%1/2
defined $risk and $risk >= $threshold
defined $risk $risk >= $threshold
Truth table66%2/3
A and B
A= defined $riskB= $risk >= $threshold
ABresult
0X0
100
111
54 10 ▶ return "\x{2713}" if !defined $risk || $risk <= 2; # obligation-free
Branch: 1 branch
BranchTrueFalseTotal
if not defined $risk or $risk <= 2 10 0 10
Condition33%1/3
not defined $risk or $risk <= 2
l!l&&r!l&&!r
0100
MC/DC0%0/2
not defined $risk or $risk <= 2
not defined $risk $risk <= 2
Truth table33%1/3
not(A) or B
A= not defined $riskB= $risk <= 2
ABresult
1X1
011
000
55 0 return "\x{2022}"; # obligations, but below the gate
56 }
57  
58 9 ▶sub _color ($risk, $threshold) {
Subroutine _color: called CC=5 SCAR=18.6
59 9 ▶ return 'red' if defined $risk && $risk >= $threshold;
Branch: 1 branch
BranchTrueFalseTotal
if defined $risk and $risk >= $threshold 2 7 9
Condition66%2/3
defined $risk and $risk >= $threshold
!ll&&!rl&&r
072
MC/DC50%1/2
defined $risk and $risk >= $threshold
defined $risk $risk >= $threshold
Truth table66%2/3
A and B
A= defined $riskB= $risk >= $threshold
ABresult
0X0
100
111
60 7 ▶ return 'green' if !defined $risk || $risk <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $risk or $risk <= 2 7 0 7
Condition33%1/3
not defined $risk or $risk <= 2
l!l&&r!l&&!r
070
MC/DC0%0/2
not defined $risk or $risk <= 2
not defined $risk $risk <= 2
Truth table33%1/3
not(A) or B
A= not defined $riskB= $risk <= 2
ABresult
1X1
011
000
61 0 return 'yellow';
62 }
63  
64 14 ▶sub _paint ($on, $color, $text) { return $on && $color ? Term::ANSIColor::colored($text, $color) : $text }
Subroutine _paint: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
$on && $color ? : 0 14 14
Condition33%1/3
$on and $color
!ll&&!rl&&r
1400
MC/DC0%0/2
$on and $color
$on $color
Truth table33%1/3
A and B
A= $onB= $color
ABresult
0X0
100
111
65  
66 # The machine format: the verdict plus the license list, for a CI step to police or store.
67 0 ▶sub render_json ($info) {
Subroutine render_json: not called CC=1 SCAR=6.9
Pod undocumented
68 return to_json(
69 {
70 0 map { $_ => $info->{$_} }
71 0 grep { defined $info->{$_} }
72 qw(id name risk acceptable_risk threshold state unresolved gate licenses report_url sbom notice)
73 }
74 ) . "\n";
75 }
76  
77 # The human format: a headline tied to the gate, a one-line tally, then the licenses, highest risk first.
78 5 ▶sub render_text ($info, %opts) {
Subroutine render_text: called CC=14 SCAR=27.3
Pod undocumented
79 5 my $color = $opts{color};
80 5 my $threshold = $info->{threshold};
81 5 my $risk = $info->{risk};
82 5 ▶ my $failed = defined $risk && $risk >= $threshold;
Condition66%2/3
defined $risk && $risk >= $threshold
!ll&&!rl&&r
032
MC/DC50%1/2
defined $risk && $risk >= $threshold
defined $risk $risk >= $threshold
Truth table66%2/3
A && B
A= defined $riskB= $risk >= $threshold
ABresult
0X0
100
111
83  
84 my $headline = defined $risk
85 ? sprintf(
86 '%s %s - risk %d (%s) %s threshold %d',
87 _glyph($risk, $threshold),
88 $info->{name}, $risk, _label($risk), $failed ? "\x{2265}" : 'within', $threshold
89 )
90 5 ▶ : sprintf('%s %s - no license risk detected', "\x{2713}", $info->{name});
Branch: 2 branches
BranchTrueFalseTotal
defined $risk ? : 5 0 5
$failed ? : 2 3 5
91 5 ▶ my $out = _paint($color, $failed ? 'red' : 'green', $headline) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$failed ? : 2 3 5
92  
93 5 ▶ my @licenses = @{$info->{licenses} || []};
Condition50%1/2
$info->{'licenses'} || []
l!l
50
MC/DC0%0/1
$info->{'licenses'} || []
$info->{'licenses'}
Truth table50%1/2
A
A= $info->{'licenses'}
Aresult
11
00
94 $out .= sprintf " %d %s \x{b7} %d unresolved \x{b7} review state: %s\n", scalar(@licenses),
95 5 ▶ (@licenses == 1 ? 'license' : 'licenses'), ($info->{unresolved} // 0), $info->{state} // '?';
Branch: 1 branch
BranchTrueFalseTotal
@licenses == 1 ? : 1 4 5
Condition50%2/4
$info->{"unresolved"} // 0
l!l
50
$info->{"state"} // "?"
l!l
50
MC/DC0%0/1
$info->{"unresolved"} // 0
$info->{"unresolved"}
MC/DC0%0/1
$info->{"state"} // "?"
$info->{"state"}
Truth table50%1/2
A
A= $info->{"unresolved"}
Aresult
11
00
Truth table50%1/2
A
A= $info->{"state"}
Aresult
11
00
96  
97 5 ▶ if (@licenses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@licenses) 5 0 5
98 5 $out .= "\n";
99 5 for my $l (@licenses) {
100 9 my $g = _paint($color, _color($l->{risk}, $threshold), _glyph($l->{risk}, $threshold));
101 9 $out .= sprintf " %s %-30s risk %d (%s)\n", $g, $l->{name}, $l->{risk}, _label($l->{risk});
102 }
103 }
104  
105 5 ▶ $out .= "\n Report: $info->{report_url}\n" if $info->{report_url};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"report_url"} 5 0 5
106 5 ▶ $out .= " SBOM: $info->{sbom}\n" if $info->{sbom};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"sbom"} 1 4 5
107 5 ▶ $out .= " NOTICE: $info->{notice}\n" if $info->{notice};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"notice"} 0 5 5
108  
109 5 return $out;
110 }
111  
112 7 1;
« CLI.pm ↑ Summary CLI/Archive.pm »