From 8381567256c6657b333076792ff49d33887ed9fa Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 11 Dec 2023 21:49:33 -0500 Subject: [PATCH] feat: add colors and packages flags Signed-off-by: Christopher Phillips --- cmd/grant/cli/command/check.go | 28 +++++----------------- cmd/grant/cli/internal/check/report.go | 8 +++++++ cmd/grant/cli/option/check.go | 32 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 cmd/grant/cli/option/check.go diff --git a/cmd/grant/cli/command/check.go b/cmd/grant/cli/command/check.go index 69bfa2f..b169d47 100644 --- a/cmd/grant/cli/command/check.go +++ b/cmd/grant/cli/command/check.go @@ -18,27 +18,8 @@ import ( ) type CheckConfig struct { - Config string `json:"config" yaml:"config" mapstructure:"config"` - Format string `json:"format" yaml:"format" mapstructure:"format"` - ShowPackages bool `json:"show-packages" yaml:"show-packages" mapstructure:"show-packages"` - CheckNonSPDX bool `json:"check-non-spdx" yaml:"check-non-spdx" mapstructure:"check-non-spdx"` - Quiet bool `json:"quiet" yaml:"quiet" mapstructure:"quiet"` - Rules []option.Rule `json:"rules" yaml:"rules" mapstructure:"rules"` -} - -func DefaultCheck() *CheckConfig { - return &CheckConfig{ - Config: "", - ShowPackages: false, - Rules: []option.Rule{ - { - Name: "deny-all", - Reason: "grant by default will deny all licenses", - Pattern: "*", - Severity: "high", - }, - }, - } + Config string `json:"config" yaml:"config" mapstructure:"config"` + option.Check `json:"" yaml:",inline" mapstructure:",squash"` } func (cfg *CheckConfig) RulesFromConfig() (rules grant.Rules, err error) { @@ -73,7 +54,10 @@ func (cfg *CheckConfig) RulesFromConfig() (rules grant.Rules, err error) { } func Check(app clio.Application) *cobra.Command { - cfg := DefaultCheck() + cfg := &CheckConfig{ + Check: option.DefaultCheck(), + } + // sources are the oci images, sboms, or directories/files to check var sources []string return app.SetupCommand(&cobra.Command{ diff --git a/cmd/grant/cli/internal/check/report.go b/cmd/grant/cli/internal/check/report.go index 2e15138..655d7bf 100644 --- a/cmd/grant/cli/internal/check/report.go +++ b/cmd/grant/cli/internal/check/report.go @@ -111,6 +111,14 @@ func renderEvaluations(rule grant.Rule, showPackages bool, l list.Writer, e eval licenseTracker[license] = struct{}{} l.Indent() l.AppendItem(color.Danger.Sprintf("%s", license)) + if showPackages { + packages := e.Packages(license) + l.Indent() + for _, pkg := range packages { + l.AppendItem(color.Light.Sprintf("%s", pkg)) + } + l.UnIndent() + } l.UnIndent() } } diff --git a/cmd/grant/cli/option/check.go b/cmd/grant/cli/option/check.go new file mode 100644 index 0000000..bf83966 --- /dev/null +++ b/cmd/grant/cli/option/check.go @@ -0,0 +1,32 @@ +package option + +import "github.com/anchore/clio" + +type Check struct { + Format string `json:"format" yaml:"format" mapstructure:"format"` + ShowPackages bool `json:"show-packages" yaml:"show-packages" mapstructure:"show-packages"` + CheckNonSPDX bool `json:"check-non-spdx" yaml:"check-non-spdx" mapstructure:"check-non-spdx"` + Quiet bool `json:"quiet" yaml:"quiet" mapstructure:"quiet"` + Rules []Rule `json:"rules" yaml:"rules" mapstructure:"rules"` +} + +func (o *Check) AddFlags(flags clio.FlagSet) { + flags.BoolVarP(&o.ShowPackages, "show-packages", "", "expand the license lists to show packages that contained the license violation") + flags.BoolVarP(&o.CheckNonSPDX, "check-non-spdx", "", "run the configured rules against licenses that could not be matched to the SPDX license list") +} + +func DefaultCheck() Check { + return Check{ + ShowPackages: false, + CheckNonSPDX: false, + Quiet: false, + Rules: []Rule{ + { + Name: "deny-all", + Reason: "grant by default will deny all licenses", + Pattern: "*", + Severity: "high", + }, + }, + } +}