Cavil-CLI

 view release on metacpan or  search on metacpan

lib/Cavil/CLI/Util.pm  view on Meta::CPAN

sub gate ($risk, $threshold) {
  return {failed => (defined $risk && $risk >= $threshold) ? 1 : 0};
}

sub _label ($risk) { return $RISK_LABEL{$risk} // 'unclassified' }

sub _glyph ($risk, $threshold) {
  return "\x{2717}" if defined $risk && $risk >= $threshold;    # at or above the gate
  return "\x{2713}" if !defined $risk || $risk <= 2;            # obligation-free
  return "\x{2022}";                                            # obligations, but below the gate
}

sub _color ($risk, $threshold) {
  return 'red'   if defined $risk && $risk >= $threshold;
  return 'green' if !defined $risk || $risk <= 2;
  return 'yellow';
}

sub _paint ($on, $color, $text) { return $on && $color ? Term::ANSIColor::colored($text, $color) : $text }

# The machine format: the verdict plus the license list, for a CI step to police or store.
sub render_json ($info) {
  return to_json(
    {
      map  { $_ => $info->{$_} }
      grep { defined $info->{$_} }
        qw(id name risk acceptable_risk threshold state unresolved gate licenses report_url sbom notice)
    }
  ) . "\n";
}

# The human format: a headline tied to the gate, a one-line tally, then the licenses, highest risk first.
sub render_text ($info, %opts) {
  my $color     = $opts{color};
  my $threshold = $info->{threshold};
  my $risk      = $info->{risk};
  my $failed    = defined $risk && $risk >= $threshold;

  my $headline = defined $risk
    ? sprintf(
    '%s %s - risk %d (%s) %s threshold %d',
    _glyph($risk, $threshold),
    $info->{name}, $risk, _label($risk), $failed ? "\x{2265}" : 'within', $threshold
    )
    : sprintf('%s %s - no license risk detected', "\x{2713}", $info->{name});
  my $out = _paint($color, $failed ? 'red' : 'green', $headline) . "\n";

  my @licenses = @{$info->{licenses} || []};
  $out .= sprintf "  %d %s \x{b7} %d unresolved \x{b7} review state: %s\n", scalar(@licenses),
    (@licenses == 1 ? 'license' : 'licenses'), ($info->{unresolved} // 0), $info->{state} // '?';

  if (@licenses) {
    $out .= "\n";
    for my $l (@licenses) {
      my $g = _paint($color, _color($l->{risk}, $threshold), _glyph($l->{risk}, $threshold));
      $out .= sprintf "  %s  %-30s risk %d (%s)\n", $g, $l->{name}, $l->{risk}, _label($l->{risk});
    }
  }

  $out .= "\n  Report: $info->{report_url}\n" if $info->{report_url};
  $out .= "  SBOM:   $info->{sbom}\n"         if $info->{sbom};
  $out .= "  NOTICE: $info->{notice}\n"       if $info->{notice};

  return $out;
}

1;



( run in 2.181 seconds using v1.01-cache-2.11-cpan-364913b4093 )