From 77bfa44391b7f0ffcb1118c1b6aaf4d5f97441b3 Mon Sep 17 00:00:00 2001 From: Christopher Angelo Phillips <32073428+spiffcs@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:35:09 -0500 Subject: [PATCH] feat: show non spdx in list report (#35) * feat: show non spdx in list report Signed-off-by: Christopher Phillips * chore: rename option Signed-off-by: Christopher Phillips --------- Signed-off-by: Christopher Phillips --- README.md | 2 +- cmd/grant/cli/command/check.go | 4 ++-- cmd/grant/cli/command/list.go | 2 +- cmd/grant/cli/internal/config.go | 5 +++++ cmd/grant/cli/internal/list/report.go | 14 +++++++++++++- cmd/grant/cli/option/list.go | 6 +++--- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8831521..f5918a0 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ It can also be used to allow specific licenses, denying all others. config: ".grant.yaml" format: table # table, json show-packages: false # show the packages which contain the licenses --show-packages -check-non-spdx: false # check licenses that could not be matched to an SPDX identifier --check-non-spdx +non-spdx: false # list only licenses that could not be matched to an SPDX identifier --non-spdx osi-approved: false # highlight licenses that are not OSI approved --osi-approved rules: - pattern: "*gpl*" diff --git a/cmd/grant/cli/command/check.go b/cmd/grant/cli/command/check.go index ec7c1f2..987c022 100644 --- a/cmd/grant/cli/command/check.go +++ b/cmd/grant/cli/command/check.go @@ -111,7 +111,7 @@ func runCheck(cfg *CheckConfig, userInput []string) (errs error) { } }() - policy, err := grant.NewPolicy(cfg.CheckNonSPDX, rules...) + policy, err := grant.NewPolicy(cfg.NonSPDX, rules...) if err != nil { return errors.Wrap(err, fmt.Sprintf("could not check licenses; could not build policy from config: %s", cfg.Config)) } @@ -121,7 +121,7 @@ func runCheck(cfg *CheckConfig, userInput []string) (errs error) { Options: internal.ReportOptions{ Format: internal.Format(cfg.Output), ShowPackages: cfg.ShowPackages, - CheckNonSPDX: cfg.CheckNonSPDX, + CheckNonSPDX: cfg.NonSPDX, OsiApproved: cfg.OsiApproved, }, Monitor: monitor, diff --git a/cmd/grant/cli/command/list.go b/cmd/grant/cli/command/list.go index dcc1858..043b3b2 100644 --- a/cmd/grant/cli/command/list.go +++ b/cmd/grant/cli/command/list.go @@ -73,7 +73,7 @@ func runList(cfg *ListConfig, userInput []string) (errs error) { Options: internal.ReportOptions{ Format: internal.Format(cfg.Output), ShowPackages: cfg.ShowPackages, - CheckNonSPDX: cfg.CheckNonSPDX, + CheckNonSPDX: cfg.NonSPDX, }, Monitor: monitor, } diff --git a/cmd/grant/cli/internal/config.go b/cmd/grant/cli/internal/config.go index 2b39173..ec4012c 100644 --- a/cmd/grant/cli/internal/config.go +++ b/cmd/grant/cli/internal/config.go @@ -1,5 +1,10 @@ package internal +// TODO: osi approved filter +// TODO: non spdx filter +// TODO: packages no licenses +// TODO: licenses no packages + type ReportOptions struct { Format Format ShowPackages bool diff --git a/cmd/grant/cli/internal/list/report.go b/cmd/grant/cli/internal/list/report.go index 7a7c712..e851d76 100644 --- a/cmd/grant/cli/internal/list/report.go +++ b/cmd/grant/cli/internal/list/report.go @@ -120,8 +120,12 @@ func (r *Report) renderList() error { resultList := list.NewWriter() uiLists = append(uiLists, resultList) resultList.AppendItem(color.Primary.Sprintf("%s", c.UserInput)) - _, licenses, _ := c.GetLicenses() + packages, licenses, _ := c.GetLicenses() for _, license := range licenses { + // Filter out SPDX licenses if requested to just show non-SPDX licenses + if r.Config.Options.CheckNonSPDX && license.IsSPDX() { + continue + } if license.IsSPDX() { unsortedLicenses = append(unsortedLicenses, license.SPDXExpression) continue @@ -135,6 +139,14 @@ func (r *Report) renderList() error { resultList.Indent() for _, license := range unsortedLicenses { resultList.AppendItem(license) + if r.Config.Options.ShowPackages { + pkgs := packages[license] + for _, pkg := range pkgs { + resultList.Indent() + resultList.AppendItem(pkg.Name) + resultList.UnIndent() + } + } } resultList.UnIndent() } diff --git a/cmd/grant/cli/option/list.go b/cmd/grant/cli/option/list.go index 47a1b7a..87a28bb 100644 --- a/cmd/grant/cli/option/list.go +++ b/cmd/grant/cli/option/list.go @@ -5,19 +5,19 @@ import "github.com/anchore/clio" type List struct { Output string `json:"output" yaml:"output" mapstructure:"output"` 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"` + NonSPDX bool `json:"non-spdx" yaml:"non-spdx" mapstructure:"non-spdx"` } func DefaultList() List { return List{ Output: "table", ShowPackages: false, - CheckNonSPDX: false, + NonSPDX: false, } } func (o *List) AddFlags(flags clio.FlagSet) { flags.BoolVarP(&o.ShowPackages, "show-packages", "", "expand the license lists to show packages that contained the detected license") - flags.BoolVarP(&o.CheckNonSPDX, "check-non-spdx", "", "show licenses that could not be matched to the SPDX license list") + flags.BoolVarP(&o.NonSPDX, "non-spdx", "", "show licenses that could not be matched to the SPDX license list") flags.StringVarP(&o.Output, "output", "o", "output format (table, json, yaml)") }