Skip to content

Commit

Permalink
feat: small ui for list and check (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs authored Dec 12, 2023
1 parent 7a2a979 commit 215f5f7
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 122 deletions.
22 changes: 22 additions & 0 deletions cmd/grant/cli/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/anchore/clio"
"github.com/anchore/grant/cmd/grant/cli/internal/check"
"github.com/anchore/grant/cmd/grant/cli/option"
"github.com/anchore/grant/event"
"github.com/anchore/grant/grant"
"github.com/anchore/grant/internal/bus"
"github.com/anchore/grant/internal/input"
)

Expand Down Expand Up @@ -87,6 +89,25 @@ func runCheck(cfg *CheckConfig, userInput []string) (errs error) {
return errors.Wrap(err, fmt.Sprintf("could not check licenses; could not build rules from config: %s", cfg.Config))
}

monitor := bus.PublishTask(
event.Title{
Default: "Check licenses",
WhileRunning: "Checking licenses",
OnSuccess: "Checked licenses",
},
"",
len(userInput),
)

defer func() {
if errs != nil {
monitor.SetError(errs)
} else {
monitor.AtomicStage.Set(strings.Join(userInput, ", "))
monitor.SetCompleted()
}
}()

policy, err := grant.NewPolicy(cfg.CheckNonSPDX, 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 @@ -98,6 +119,7 @@ func runCheck(cfg *CheckConfig, userInput []string) (errs error) {
ShowPackages: cfg.ShowPackages,
CheckNonSPDX: cfg.CheckNonSPDX,
OsiApproved: cfg.OsiApproved,
Monitor: monitor,
}
rep, err := check.NewReport(reportConfig, userInput...)
if err != nil {
Expand Down
26 changes: 25 additions & 1 deletion cmd/grant/cli/command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package command

import (
"slices"
"strings"

"github.com/spf13/cobra"

"github.com/anchore/clio"
"github.com/anchore/grant/cmd/grant/cli/internal/check"
"github.com/anchore/grant/cmd/grant/cli/option"
"github.com/anchore/grant/event"
"github.com/anchore/grant/grant"
"github.com/anchore/grant/internal/bus"
"github.com/anchore/grant/internal/input"
)

Expand Down Expand Up @@ -38,19 +41,40 @@ func List(app clio.Application) *cobra.Command {
}, cfg)
}

func runList(cfg *ListConfig, userInput []string) error {
func runList(cfg *ListConfig, userInput []string) (errs error) {
// check if user provided source by stdin
// note: cat sbom.json | grant check spdx.json - is supported
// it will generate results for both stdin and spdx.json
isStdin, _ := input.IsStdinPipeOrRedirect()
if isStdin && !slices.Contains(userInput, "-") {
userInput = append(userInput, "-")
}

monitor := bus.PublishTask(
event.Title{
Default: "List licenses",
WhileRunning: "Looking up licenses",
OnSuccess: "Found licenses",
},
"",
len(userInput),
)

defer func() {
if errs != nil {
monitor.SetError(errs)
} else {
monitor.AtomicStage.Set(strings.Join(userInput, ", "))
monitor.SetCompleted()
}
}()

reportConfig := check.ReportConfig{
Format: check.Format(cfg.Format),
ShowPackages: cfg.ShowPackages,
CheckNonSPDX: cfg.CheckNonSPDX,
Policy: grant.DefaultPolicy(),
Monitor: monitor,
}
rep, err := check.NewReport(reportConfig, userInput...)
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion cmd/grant/cli/internal/check/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package check

import (
"errors"
"strings"
"time"

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

"github.com/anchore/grant/event"
"github.com/anchore/grant/grant"
"github.com/anchore/grant/grant/evalutation"
"github.com/anchore/grant/internal/bus"
Expand All @@ -23,6 +25,7 @@ type Report struct {
Results evalutation.Results
Config ReportConfig
Timestamp string
Monitor *event.ManualStagedProgress
errors []error
}

Expand All @@ -32,6 +35,7 @@ type ReportConfig struct {
ShowPackages bool
CheckNonSPDX bool
OsiApproved bool
Monitor *event.ManualStagedProgress
}

// NewReport will generate a new report for the given format.
Expand Down Expand Up @@ -59,6 +63,7 @@ func NewReport(rc ReportConfig, userRequests ...string) (*Report, error) {
Results: results,
Config: rc,
Timestamp: time.Now().Format(time.RFC3339),
Monitor: rc.Monitor,
}, nil
}

Expand Down Expand Up @@ -86,6 +91,8 @@ func (r *Report) RenderList() error {
func (r *Report) renderCheckTree() error {
var uiLists []list.Writer
for _, res := range r.Results {
r.Monitor.Increment()
r.Monitor.AtomicStage.Set(res.Case.UserInput)
resulList := newList()
uiLists = append(uiLists, resulList)
resulList.AppendItem(color.Primary.Sprintf("%s", res.Case.UserInput))
Expand Down Expand Up @@ -118,9 +125,10 @@ func (r *Report) renderCheckTree() error {
renderOrphanPackages(resulList, res, false) // keep primary coloring for tree
}
}

r.Monitor.AtomicStage.Set(strings.Join(r.Results.UserInputs(), ", "))
// segment the results into lists by user input
// lists can optionally show the packages that were evaluated

for _, l := range uiLists {
bus.Report(l.Render())
}
Expand All @@ -130,6 +138,8 @@ func (r *Report) renderCheckTree() error {
func (r *Report) renderList() error {
var uiLists []list.Writer
for _, res := range r.Results {
r.Monitor.Increment()
r.Monitor.AtomicStage.Set(res.Case.UserInput)
resulList := newList()
uiLists = append(uiLists, resulList)
resulList.AppendItem(color.Primary.Sprintf("%s", res.Case.UserInput))
Expand All @@ -152,6 +162,7 @@ func (r *Report) renderList() error {
renderOrphanPackages(resulList, res, true)
}
}
r.Monitor.AtomicStage.Set(strings.Join(r.Results.UserInputs(), ", "))

// segment the results into lists by user input
// lists can optionally show the packages that were evaluated
Expand Down
109 changes: 0 additions & 109 deletions cmd/grant/cli/tui/handle_check.go

This file was deleted.

37 changes: 37 additions & 0 deletions cmd/grant/cli/tui/handle_task_started.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tui

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/wagoodman/go-partybus"

"github.com/anchore/bubbly/bubbles/taskprogress"
"github.com/anchore/grant/event"
)

func (m *Handler) handleTaskStarted(e partybus.Event) ([]tea.Model, tea.Cmd) {
cmd, prog, err := event.ParseTaskStarted(e)
if err != nil {
//log.Warnf("unable to parse event: %+v", err)
return nil, nil
}

tsk := taskprogress.New(
m.Running,
taskprogress.WithStagedProgressable(prog),
)

tsk.HideProgressOnSuccess = true
tsk.HideOnSuccess = true
tsk.TitleWidth = len(cmd.Title.WhileRunning)
tsk.HintEndCaps = nil
tsk.TitleOptions = taskprogress.Title{
Default: cmd.Title.Default,
Running: cmd.Title.WhileRunning,
Success: cmd.Title.OnSuccess,
Failed: cmd.Title.OnFail,
}
tsk.Context = []string{cmd.Context}
tsk.WindowSize = m.WindowSize

return []tea.Model{tsk}, nil
}
2 changes: 1 addition & 1 deletion cmd/grant/cli/tui/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func New(cfg HandlerConfig) *Handler {

// register all supported event types with the respective handler functions
d.AddHandlers(map[partybus.EventType]bubbly.EventHandlerFn{
event.CLICheckCmdStarted: h.handleCLICheckCmdStarted,
event.TaskStartedEvent: h.handleTaskStarted,
})

return h
Expand Down
14 changes: 5 additions & 9 deletions event/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,22 @@ func checkEventType(actual, expected partybus.EventType) error {
return nil
}

func ParseCheckCommandStarted(e partybus.Event) ([]string, progress.StagedProgressable, error) {
if err := checkEventType(e.Type, CLICheckCmdStarted); err != nil {
func ParseTaskStarted(e partybus.Event) (*Task, progress.StagedProgressable, error) {
if err := checkEventType(e.Type, TaskStartedEvent); err != nil {
return nil, nil, err
}

return parseSourcesAndStagedProgressable(e)
}

func parseSourcesAndStagedProgressable(e partybus.Event) ([]string, progress.StagedProgressable, error) {
sources, ok := e.Source.([]string)
cmd, ok := e.Source.(Task)
if !ok {
return nil, nil, newPayloadErr(e.Type, "Source", e.Source)
}

prog, ok := e.Value.(progress.StagedProgressable)
p, ok := e.Value.(progress.StagedProgressable)
if !ok {
return nil, nil, newPayloadErr(e.Type, "Value", e.Value)
}

return sources, prog, nil
return &cmd, p, nil
}

func ParseCLIReport(e partybus.Event) (string, string, error) {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/anchore/clio v0.0.0-20231128152715-767f62261f13
github.com/anchore/go-logger v0.0.0-20230725134548-c21dafa1ec5a
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
Expand Down Expand Up @@ -36,6 +35,7 @@ require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/acobaugh/osrelease v0.1.0 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/anchore/fangs v0.0.0-20231103141714-84c94dc43a2e // indirect
Expand All @@ -49,6 +49,8 @@ require (
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
github.com/charmbracelet/bubbles v0.16.1 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
Expand Down
Loading

0 comments on commit 215f5f7

Please sign in to comment.