Skip to content

Commit

Permalink
feat: add colors and packages flags
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs committed Dec 12, 2023
1 parent 5e3f4eb commit 8381567
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
28 changes: 6 additions & 22 deletions cmd/grant/cli/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions cmd/grant/cli/internal/check/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/grant/cli/option/check.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
}
}

0 comments on commit 8381567

Please sign in to comment.