Skip to content

Commit

Permalink
feat: update table presenter (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs authored Dec 8, 2023
1 parent 9915d67 commit 081c80d
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 106 deletions.
5 changes: 3 additions & 2 deletions cmd/grant/cli/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type CheckConfig struct {

func Check(app clio.Application) *cobra.Command {
cfg := &CheckConfig{
Check: option.DefaultCheck(),
Check: option.DefaultCheck(),
Format: string(check.Table),
}

// sources are the oci images, sboms, or directories/files to check
Expand Down Expand Up @@ -61,7 +62,7 @@ func runCheck(cfg CheckConfig, userInput []string) (errs error) {
Policy: policy,
}

rep, err := check.NewReport(check.Format(cfg.Format), checkConfig, userInput...)
rep, err := check.NewReport(check.Table, checkConfig, userInput...)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to create report for inputs %s", userInput))
}
Expand Down
78 changes: 37 additions & 41 deletions cmd/grant/cli/internal/check/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package check

import (
"errors"
"fmt"
"io"
"time"

list "github.com/jedib0t/go-pretty/v6/list"

"github.com/anchore/grant/grant"
"github.com/anchore/grant/grant/evalutation"
"github.com/anchore/grant/internal/bus"
)

// Report presents the results of a grant check command `grant alpine:latest ./foo`
Expand Down Expand Up @@ -34,13 +38,14 @@ type Config struct {
// If a request is provided, but the sbom cannot be generated, the source will be ignored and an error will be returned
func NewReport(f Format, cc Config, userRequests ...string) (*Report, error) {
if cc.Policy.IsEmpty() {
cc.Policy = grant.DefaultPolicy()
policy := grant.DefaultPolicy()
cc.Policy = &policy
}

format := validateFormat(f)
cases := grant.NewCases(cc.Policy, userRequests...)
ec := evalutation.EvaluationConfig{
Policy: cc.Policy,
Policy: *cc.Policy,
CheckNonSPDX: true,
}

Expand All @@ -56,36 +61,34 @@ func NewReport(f Format, cc Config, userRequests ...string) (*Report, error) {
// Render will call Render on each result in the report and return the report
func (r *Report) Render(out io.Writer) error {
switch r.Format {
case JSON:
return r.renderJSON(out)
case Table:
return r.renderTable(out)
}
return errors.Join(r.errors...)
}

func (r *Report) renderJSON(out io.Writer) error {
return errors.New("not implemented")
}

func (r *Report) renderTable(out io.Writer) error {
l := list.NewWriter()
l.SetStyle(list.StyleBulletStar)

for _, result := range r.Results {
l.AppendItem(result.Case.UserInput)
l.Indent()
if result.Evaluations.IsFailed() {
for _, lic := range result.Evaluations.FailedLicenses() {
l.AppendItem(fmt.Sprintf("%s", lic.LicenseID))
// TODO: update reason to render failed glob pattern
}
l.UnIndent()
continue
}
l.AppendItem("No License Violations: ✅")
l.UnIndent()
}
bus.Report(l.Render())
return nil
}

// l := list.NewWriter() // TODO: style me
// customStyle := list.Style{
// Format: text.FormatTitle,
// CharItemSingle: "",
// CharItemTop: "",
// CharItemFirst: "",
// CharItemMiddle: "",
// CharItemVertical: " ",
// CharItemBottom: "",
// CharNewline: "\n",
// LinePrefix: "",
// Name: "customStyle",
// }
// l.SetStyle(customStyle)
// for _, report := range reports {
// if len(report.PackageViolations) == 0 {
// l.AppendItem("No License Violations: ✅")
Expand All @@ -109,22 +112,15 @@ func (r *Report) renderTable(out io.Writer) error {
// return nil
//}

//type ResultSummary struct {
// CompliantPackages int `json:"compliant_packages" yaml:"compliant_packages"`
// PackageViolations int `json:"package_violations" yaml:"package_violations"`
// IgnoredPackages int `json:"ignored_packages" yaml:"ignored_packages"`
// LicenseViolations int `json:"license_violations" yaml:"license_violations"`
// CompliantLicenses int `json:"compliant_licenses" yaml:"compliant_licenses"`
// IgnoredLicenses int `json:"ignored_licenses" yaml:"ignored_licenses"`
//}
//
//func (r *Result) Summary() ResultSummary {
// return ResultSummary{
// CompliantPackages: len(r.CompliantPackages),
// PackageViolations: len(r.PackageViolations),
// IgnoredPackages: len(r.IgnoredPackages),
// LicenseViolations: len(r.LicenseViolations),
// CompliantLicenses: len(r.CompliantLicenses),
// IgnoredLicenses: len(r.IgnoredLicenses),
// }
//}
type ResultSummary struct {
CompliantPackages int `json:"compliant_packages" yaml:"compliant_packages"`
PackageViolations int `json:"package_violations" yaml:"package_violations"`
IgnoredPackages int `json:"ignored_packages" yaml:"ignored_packages"`
LicenseViolations int `json:"license_violations" yaml:"license_violations"`
CompliantLicenses int `json:"compliant_licenses" yaml:"compliant_licenses"`
IgnoredLicenses int `json:"ignored_licenses" yaml:"ignored_licenses"`
}

func Summary() ResultSummary {
return ResultSummary{}
}
44 changes: 28 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ require (
github.com/anchore/bubbly v0.0.0-20231115205105-6542675d79fe
github.com/anchore/clio v0.0.0-20231128152715-767f62261f13
github.com/anchore/go-logger v0.0.0-20230725134548-c21dafa1ec5a
github.com/anchore/syft v0.97.1
github.com/anchore/syft v0.98.0
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.9.1
github.com/github/go-spdx/v2 v2.2.0
github.com/gkampitakis/go-snaps v0.4.12
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.6.0
github.com/google/licenseclassifier/v2 v2.0.0
github.com/hashicorp/go-multierror v1.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
Expand All @@ -34,16 +35,16 @@ require (
github.com/Microsoft/hcsshim v0.11.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acobaugh/osrelease v0.1.0 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/anchore/fangs v0.0.0-20230818131516-2186b10924fe // indirect
github.com/anchore/fangs v0.0.0-20231103141714-84c94dc43a2e // indirect
github.com/anchore/go-macholibre v0.0.0-20220308212642-53e6d0aaf6fb // indirect
github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 // indirect
github.com/anchore/packageurl-go v0.1.1-0.20230104203445-02e0a6721501 // indirect
github.com/anchore/stereoscope v0.0.0-20231117203853-3610f4ef3e83 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/aquasecurity/go-pep440-version v0.0.0-20210121094942-22b2f8951d46 // indirect
github.com/aquasecurity/go-version v0.0.0-20210121072130-637058cfe492 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/becheran/wildmatch-go v1.0.0 // indirect
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
Expand All @@ -58,7 +59,7 @@ require (
github.com/containerd/ttrpc v1.2.2 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deitch/magic v0.0.0-20230404182410-1ff89d7342da // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/cli v24.0.0+incompatible // indirect
Expand All @@ -80,17 +81,18 @@ require (
github.com/gkampitakis/go-diff v1.3.2 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.10.0 // indirect
github.com/go-git/go-git/v5 v5.10.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/errors v0.20.4 // indirect
github.com/go-openapi/strfmt v0.21.9 // indirect
github.com/go-restruct/restruct v1.2.0-alpha // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
github.com/google/licensecheck v0.3.1 // indirect
github.com/google/licenseclassifier/v2 v2.0.0 // indirect
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gookit/color v1.5.4 // indirect
Expand All @@ -99,10 +101,13 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jedib0t/go-prettY v4.3.0+incompatible // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible // indirect
github.com/jedib0t/go-pretty/v6 v6.4.9 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/kastenhq/goversion v0.0.0-20230811215019-93b2f8823953 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/knqyf263/go-rpmdb v0.0.0-20230301153543-ba94b245509b // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand All @@ -128,34 +133,37 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/opencontainers/runtime-spec v1.1.0-rc.1 // indirect
github.com/opencontainers/selinux v1.11.0 // indirect
github.com/pborman/indent v1.2.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/profile v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/saferwall/pe v1.4.7 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/sassoftware/go-rpmutils v0.2.0 // indirect
github.com/scylladb/go-set v1.0.3-0.20200225121959-cc7b2070d91e // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spdx/tools-golang v0.5.3 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/spf13/viper v1.17.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/sylabs/sif/v2 v2.11.5 // indirect
github.com/sylabs/squashfs v0.6.1 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
Expand All @@ -170,20 +178,24 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.mongodb.org/mongo-driver v1.13.0 // indirect
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
Loading

0 comments on commit 081c80d

Please sign in to comment.