Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UI #6

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .binny.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tools:

- name: gh
version:
want: v2.33.0
want: v2.35.0
method: github-release
with:
repo: cli/cli
Expand Down Expand Up @@ -52,7 +52,7 @@ tools:

- name: goreleaser
version:
want: v1.20.0
want: v1.21.0
method: github-release
with:
repo: goreleaser/goreleaser
Expand All @@ -73,7 +73,7 @@ tools:

- name: task
version:
want: v3.29.1
want: v3.30.1
method: github-release
with:
repo: go-task/task
2 changes: 1 addition & 1 deletion .github/actions/bootstrap/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ inputs:
go-version:
description: "Go version to install"
required: true
default: "1.20.x"
default: "1.21.x"
use-go-cache:
description: "Restore go cache"
required: true
Expand Down
14 changes: 7 additions & 7 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tasks:
## High-level tasks #################################

default:
desc: Run all validation tasks
# desc: Run all validation tasks
aliases:
- pr-validations
- validations
Expand Down Expand Up @@ -84,7 +84,7 @@ tasks:
deps: [tools]
cmds:
- task: format
- "{{ .TOOL_DIR }}/golangci-lint run --tests=false"
- "{{ .TOOL_DIR }}/golangci-lint run --tests=false --fix"

lint:
desc: Run gofmt + golangci lint checks
Expand All @@ -106,12 +106,12 @@ tasks:


check-licenses:
desc: Ensure transitive dependencies are compliant with the current license policy
# desc: Ensure transitive dependencies are compliant with the current license policy
deps: [tools]
cmd: "{{ .TOOL_DIR }}/bouncer check ./..."

check-go-mod-tidy:
desc: Ensure go.mod and go.sum are up to date
# desc: Ensure go.mod and go.sum are up to date
cmds:
- .github/scripts/go-mod-tidy-check.sh && echo "go.mod and go.sum are tidy!"

Expand Down Expand Up @@ -171,20 +171,20 @@ tasks:
## CI-only targets #################################

ci-check:
desc: Are you in CI?
# desc: Are you in CI?
cmds:
- cmd: .github/scripts/ci-check.sh
silent: true

ci-validate:
desc: Run all CI validations
# desc: Run all CI validations
cmds:
- cmd: .github/scripts/ci-check.sh
silent: true
- task: default

ci-release:
desc: Create a release
# desc: Create a release
deps: [tools]
cmds:
- task: ci-check
Expand Down
24 changes: 22 additions & 2 deletions cmd/binny/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package cli

import (
"os"

"github.com/anchore/binny/cmd/binny/cli/command"
"github.com/anchore/binny/cmd/binny/cli/internal/ui"
handler "github.com/anchore/binny/cmd/binny/cli/ui"
"github.com/anchore/binny/internal/bus"
"github.com/anchore/binny/internal/log"
"github.com/anchore/binny/internal/redact"
Expand All @@ -19,9 +23,25 @@ func New(id clio.Identification) clio.Application {
WithGlobalConfigFlag(). // add persistent -c <path> for reading an application config from
WithGlobalLoggingFlags(). // add persistent -v and -q flags tied to the logging config
WithConfigInRootHelp(). // --help on the root command renders the full application config in the help text
WithNoBus().
WithUIConstructor(
// select a UI based on the logging configuration and state of stdin (if stdin is a tty)
func(cfg clio.Config) ([]clio.UI, error) {
noUI := ui.None(cfg.Log.Quiet)
if !cfg.Log.AllowUI(os.Stdin) || cfg.Log.Quiet {
return []clio.UI{noUI}, nil
}

return []clio.UI{
ui.New(cfg.Log.Quiet,
handler.New(handler.DefaultHandlerConfig()),
),
noUI,
}, nil
},
).
WithLoggingConfig(clio.LoggingConfig{
Level: logger.InfoLevel,
// TODO: this should really be logger.DisabledLevel, but that does not appear to be working as expected
Level: logger.ErrorLevel,
}).
WithInitializers(
func(state *clio.State) error {
Expand Down
14 changes: 11 additions & 3 deletions cmd/binny/cli/command/add_github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/anchore/binny/cmd/binny/cli/internal/yamlpatch"
"github.com/anchore/binny/cmd/binny/cli/option"
"github.com/anchore/binny/internal/bus"
"github.com/anchore/binny/internal/log"
"github.com/anchore/binny/tool/githubrelease"
"github.com/anchore/clio"
Expand Down Expand Up @@ -65,8 +66,9 @@ func runGithubReleaseConfig(cmdCfg AddGithubReleaseConfig, repoVersion string) e
name = fields[1]

if strset.New(cmdCfg.Tools.Names()...).Has(name) {
// TODO: should this be an error?
log.Warnf("tool %q already configured", name)
message := fmt.Sprintf("tool %q already configured", name)
bus.Report(message)
log.Warn(message)
return nil
}

Expand Down Expand Up @@ -96,5 +98,11 @@ func runGithubReleaseConfig(cmdCfg AddGithubReleaseConfig, repoVersion string) e
Parameters: installParamMap,
}

return yamlpatch.Write(cmdCfg.Config, yamlToolAppender{toolCfg: toolCfg})
if err := yamlpatch.Write(cmdCfg.Config, yamlToolAppender{toolCfg: toolCfg}); err != nil {
return fmt.Errorf("unable to write config: %w", err)
}

reportNewConfiguration(toolCfg)

return nil
}
8 changes: 7 additions & 1 deletion cmd/binny/cli/command/add_go_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,11 @@ func runAddGoInstallConfig(cmdCfg AddGoInstallConfig, nameVersion string) error
Parameters: installParamMap,
}

return yamlpatch.Write(cmdCfg.Config, yamlToolAppender{toolCfg: toolCfg})
if err := yamlpatch.Write(cmdCfg.Config, yamlToolAppender{toolCfg: toolCfg}); err != nil {
return fmt.Errorf("unable to write config: %w", err)
}

reportNewConfiguration(toolCfg)

return nil
}
91 changes: 57 additions & 34 deletions cmd/binny/cli/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package command

import (
"fmt"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"

"github.com/anchore/binny"
"github.com/anchore/binny/cmd/binny/cli/option"
"github.com/anchore/binny/event"
"github.com/anchore/binny/internal/bus"
"github.com/anchore/binny/internal/log"
"github.com/anchore/binny/tool"
"github.com/anchore/clio"
)

type CheckConfig struct {
Config string `json:"config" yaml:"config" mapstructure:"config"`
option.Core `json:"" yaml:",inline" mapstructure:",squash"`
Config string `json:"config" yaml:"config" mapstructure:"config"`
option.Check `json:"" yaml:",inline" mapstructure:",squash"`
option.Core `json:"" yaml:",inline" mapstructure:",squash"`
}

func Check(app clio.Application) *cobra.Command {
Expand All @@ -39,18 +43,11 @@ func Check(app clio.Application) *cobra.Command {
}, cfg)
}

func runCheck(cmdCfg CheckConfig, names []string) error {
if len(names) == 0 {
names = cmdCfg.Tools.Names()
}

toolOpts, err := cmdCfg.Tools.GetAllOptions(names)
if err != nil {
return err
}
func runCheck(cmdCfg CheckConfig, names []string) (errs error) {
names, toolOpts := selectNamesAndConfigs(cmdCfg.Core, names)

if len(toolOpts) == 0 {
// TODO: should this be an error?
bus.Report("no tools to verify")
log.Warn("no tools to verify")
return nil
}
Expand All @@ -61,43 +58,69 @@ func runCheck(cmdCfg CheckConfig, names []string) error {
return err
}

var errs error
monitor := bus.PublishTask(
event.Title{
Default: "Verify installed tools",
WhileRunning: "Verifying installed tools",
OnSuccess: "Verified installed tools",
},
cmdCfg.Root,
len(toolOpts),
)

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

var failedTools []string
for _, opt := range toolOpts {
t, intent, err := opt.ToTool()
monitor.Increment()
monitor.AtomicStage.Set(opt.Name)

resolvedVersion, err := checkTool(store, opt, cmdCfg.VerifySHA256Digest)
if err != nil {
failedTools = append(failedTools, opt.Name)
errs = multierror.Append(errs, fmt.Errorf("failed to check tool %q: %w", opt.Name, err))
continue
}

resolvedVersion, err := tool.ResolveVersion(t, *intent)
if err != nil {
failedTools = append(failedTools, t.Name())
errs = multierror.Append(errs, fmt.Errorf("failed to check tool %q: %w", t.Name(), err))
continue
}

// otherwise continue to install the tool
err = tool.Check(t.Name(), resolvedVersion, store, tool.VerifyConfig{
VerifyXXH64Digest: true,
VerifySHA256Digest: cmdCfg.VerifySHA256Digest,
})
if err != nil {
failedTools = append(failedTools, t.Name())
errs = multierror.Append(errs, fmt.Errorf("failed to check tool %q: %w", t.Name(), err))
continue
}

log.WithFields("tool", t.Name(), "version", resolvedVersion).Debug("installation verified")
log.WithFields("tool", opt.Name, "version", resolvedVersion).Debug("installation verified")
}

if errs != nil {
log.WithFields("tools", failedTools).Error("verification failed")
log.WithFields("tools", failedTools).Warn("verification failed")
return errs
}

log.Info("all tools verified")

return nil
}

func checkTool(store *binny.Store, opt option.Tool, verifySha256Digest bool) (string, error) {
t, intent, err := opt.ToTool()
if err != nil {
return "", err
}

resolvedVersion, err := tool.ResolveVersion(t, *intent)
if err != nil {
return "", err
}

// otherwise continue to install the tool
err = tool.Check(t.Name(), resolvedVersion, store, tool.VerifyConfig{
VerifyXXH64Digest: true,
VerifySHA256Digest: verifySha256Digest,
})
if err != nil {
return resolvedVersion, err
}

return resolvedVersion, nil
}
Loading