Skip to content

Commit

Permalink
use simplified clio API
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman committed Jun 30, 2023
1 parent 95ef16f commit 777d5ff
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 295 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/oss-project-board-add.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Add to OSS board

on:
issues:
types:
- opened
- reopened
- transferred
- labeled

jobs:

run:
uses: "anchore/workflows/.github/workflows/oss-project-board-add.yaml@main"
secrets:
token: ${{ secrets.OSS_PROJECT_GH_TOKEN }}
43 changes: 16 additions & 27 deletions cmd/quill/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,20 @@ import (
"github.com/spf13/cobra"

"github.com/anchore/clio"
"github.com/anchore/go-logger"
"github.com/anchore/quill/cmd/quill/cli/commands"
"github.com/anchore/quill/cmd/quill/internal/ui"
"github.com/anchore/quill/internal"
"github.com/anchore/quill/internal/bus"
"github.com/anchore/quill/internal/log"
"github.com/anchore/quill/internal/redact"
)

func New(version clio.Version) *cobra.Command {
clioCfg := clio.NewConfig(internal.ApplicationName, version.Version).
WithLoggingConfig(
clio.LoggingConfig{
Level: logger.WarnLevel,
},
).
WithLoggerConstructor(
func(config clio.Config) (logger.Logger, error) {
l, err := clio.DefaultLogger(config)
if err != nil {
return nil, err
}
// immediately set the logger to account for redactions from any configurations
log.Set(l)
return log.Get(), nil
},
).
func New(id clio.Identification) *cobra.Command {
clioCfg := clio.NewSetupConfig(id).
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
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()
if !cfg.Log.AllowUI(os.Stdin) {
Expand All @@ -46,15 +33,21 @@ func New(version clio.Version) *cobra.Command {
},
).
WithInitializers(
func(cfg clio.Config, state clio.State) error {
func(state *clio.State) error {
// clio is setting up and providing the bus, redact store, and logger to the application. Once loaded,
// we can hoist them into the internal packages for global use.

bus.Set(state.Bus)
redact.Set(state.RedactStore)
log.Set(state.Logger)

return nil
},
)

app := clio.New(*clioCfg)

root := commands.Root(clioCfg, app)
root := commands.Root(app)

submission := commands.Submission(app)
submission.AddCommand(commands.SubmissionList(app))
Expand All @@ -68,7 +61,7 @@ func New(version clio.Version) *cobra.Command {
p12.AddCommand(commands.P12AttachChain(app))
p12.AddCommand(commands.P12Describe(app))

root.AddCommand(clio.VersionCommand(app, version))
root.AddCommand(clio.VersionCommand(id))
root.AddCommand(commands.Sign(app))
root.AddCommand(commands.Notarize(app))
root.AddCommand(commands.SignAndNotarize(app))
Expand All @@ -78,9 +71,5 @@ func New(version clio.Version) *cobra.Command {
root.AddCommand(extract)
root.AddCommand(p12)

// root.Example is set _after all added commands_ because it collects all the
// options structs in order to output an accurate "config file" summary
root.Example = app.SummarizeConfig(root)

return root
}
34 changes: 17 additions & 17 deletions cmd/quill/cli/commands/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ func Describe(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
var err error
buf := &strings.Builder{}
switch strings.ToLower(opts.Output) {
case "text":
err = extract.ShowText(opts.Path, buf, !opts.Detail)
case "json":
err = extract.ShowJSON(opts.Path, buf)
default:
err = fmt.Errorf("unknown format: %s", opts.Output)
}
defer bus.Exit()

if err != nil {
return err
}
var err error
buf := &strings.Builder{}
switch strings.ToLower(opts.Output) {
case "text":
err = extract.ShowText(opts.Path, buf, !opts.Detail)
case "json":
err = extract.ShowJSON(opts.Path, buf)
default:
err = fmt.Errorf("unknown format: %s", opts.Output)
}

bus.Report(buf.String())
if err != nil {
return err
}

return nil
}))
bus.Report(buf.String())

return nil
},
}, opts)
}
20 changes: 10 additions & 10 deletions cmd/quill/cli/commands/embedded_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ func EmbeddedCerts(app clio.Application) *cobra.Command {
Short: "show the certificates embedded into quill (typically the Apple root and intermediate certs)",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
var err error
buf := &strings.Builder{}
defer bus.Exit()

err = showAppleCerts(buf)
var err error
buf := &strings.Builder{}

if err != nil {
return err
}
err = showAppleCerts(buf)

bus.Report(buf.String())
if err != nil {
return err
}

return nil
}))
bus.Report(buf.String())

return nil
},
})
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/quill/cli/commands/extract_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ func ExtractCertificates(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
certs, err := extractCertificates(opts.Path, opts.Leaf)
if err != nil {
return err
}
defer bus.Exit()

bus.Report(certs)
bus.Notify("Try running 'openssl x509 -text -in <path-to-file-with-output>.pem' to view the certificate details")
certs, err := extractCertificates(opts.Path, opts.Leaf)
if err != nil {
return err
}

return nil
}))
bus.Report(certs)
bus.Notify("Try running 'openssl x509 -text -in <path-to-file-with-output>.pem' to view the certificate details")

return nil
},
}, opts)
}
Expand Down
21 changes: 11 additions & 10 deletions cmd/quill/cli/commands/notarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/anchore/clio"
"github.com/anchore/fangs"
"github.com/anchore/quill/cmd/quill/cli/options"
"github.com/anchore/quill/internal/bus"
"github.com/anchore/quill/internal/log"
"github.com/anchore/quill/quill"
"github.com/anchore/quill/quill/notary"
Expand Down Expand Up @@ -47,16 +48,16 @@ func Notarize(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
// TODO: verify path is a signed darwin binary
// ... however, we may want to allow notarization of other kinds of assets (zip with darwin binary, etc)
if opts.DryRun {
log.Warn("[DRY RUN] skipping notarization...")
return nil
}
_, err := notarize(opts.Path, opts.Notary, opts.Status)
return err
}))
defer bus.Exit()

// TODO: verify path is a signed darwin binary
// ... however, we may want to allow notarization of other kinds of assets (zip with darwin binary, etc)
if opts.DryRun {
log.Warn("[DRY RUN] skipping notarization...")
return nil
}
_, err := notarize(opts.Path, opts.Notary, opts.Status)
return err
},
}, opts)
}
Expand Down
26 changes: 13 additions & 13 deletions cmd/quill/cli/commands/p12_attach_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ func P12AttachChain(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
newFilename, err := writeP12WithChain(opts.Path, opts.P12.Password, opts.Keychain.Path, true)
if err != nil {
return fmt.Errorf("unable to write new p12 with chain attached file=%q : %w", opts.Path, err)
}
defer bus.Exit()

description, err := describeP12(newFilename, opts.P12.Password)
if err != nil {
return fmt.Errorf("unable to describe p12 file=%q : %w", newFilename, err)
}
newFilename, err := writeP12WithChain(opts.Path, opts.P12.Password, opts.Keychain.Path, true)
if err != nil {
return fmt.Errorf("unable to write new p12 with chain attached file=%q : %w", opts.Path, err)
}

bus.Report(description)
bus.Notify(fmt.Sprintf("Wrote new p12 file with certificate chain to %q", newFilename))
description, err := describeP12(newFilename, opts.P12.Password)
if err != nil {
return fmt.Errorf("unable to describe p12 file=%q : %w", newFilename, err)
}

return nil
}))
bus.Report(description)
bus.Notify(fmt.Sprintf("Wrote new p12 file with certificate chain to %q", newFilename))

return nil
},
}, opts)
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/quill/cli/commands/p12_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func P12Describe(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
description, err := describeP12(opts.Path, opts.Password)
if err != nil {
return err
}
defer bus.Exit()

bus.Report(description)
description, err := describeP12(opts.Path, opts.Password)
if err != nil {
return err
}

return nil
}))
bus.Report(description)

return nil
},
}, opts)
}
Expand Down
43 changes: 2 additions & 41 deletions cmd/quill/cli/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"

"github.com/anchore/clio"
"github.com/anchore/quill/internal"
)

func Root(cfg *clio.Config, app clio.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "",
Version: app.Config().Version,
}

cmd.SetUsageTemplate(helpUsageTemplate)
cmd.SetHelpTemplate(helpUsageTemplate)

cmd.SetVersionTemplate(fmt.Sprintf("%s {{.Version}}\n", internal.ApplicationName))

return app.SetupPersistentCommand(cmd, cfg)
func Root(app clio.Application) *cobra.Command {
return app.SetupRootCommand(&cobra.Command{})
}

var helpUsageTemplate = fmt.Sprintf(`{{if (or .Long .Short)}}{{.Long}}{{if not .Long}}{{.Short}}{{end}}
{{end}}Usage:{{if (and .Runnable (ne .CommandPath "%s"))}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if .HasExample}}
{{.Example}}{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{if not .CommandPath}}Global {{end}}Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if (and .HasAvailableInheritedFlags (not .CommandPath))}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{if .CommandPath}}{{.CommandPath}} {{end}}[command] --help" for more information about a command.{{end}}
`, internal.ApplicationName)
7 changes: 4 additions & 3 deletions cmd/quill/cli/commands/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/anchore/clio"
"github.com/anchore/quill/cmd/quill/cli/options"
"github.com/anchore/quill/internal/bus"
"github.com/anchore/quill/internal/log"
"github.com/anchore/quill/quill"
)
Expand Down Expand Up @@ -37,9 +38,9 @@ func Sign(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
return sign(opts.Path, opts.Signing)
}))
defer bus.Exit()

return sign(opts.Path, opts.Signing)
},
}, opts)
}
Expand Down
Loading

0 comments on commit 777d5ff

Please sign in to comment.