Skip to content

Commit

Permalink
feat: show non spdx in list report (#35)
Browse files Browse the repository at this point in the history
* feat: show non spdx in list report

Signed-off-by: Christopher Phillips <[email protected]>

* chore: rename option

Signed-off-by: Christopher Phillips <[email protected]>

---------

Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs authored Jan 30, 2024
1 parent 2b8cd0e commit 77bfa44
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*"
Expand Down
4 changes: 2 additions & 2 deletions cmd/grant/cli/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/grant/cli/command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/grant/cli/internal/config.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 13 additions & 1 deletion cmd/grant/cli/internal/list/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/grant/cli/option/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}

0 comments on commit 77bfa44

Please sign in to comment.