Skip to content

Commit

Permalink
Port to clio: Use simplified clio API (#63)
Browse files Browse the repository at this point in the history
* use simplified clio API

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* caller creates root

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* [wip]

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* chore: update to latest clio

Signed-off-by: Keith Zantow <kzantow@gmail.com>

* chore: update to latest clio

Signed-off-by: Keith Zantow <kzantow@gmail.com>

* use the standard log redactor

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* bump clio to get logger redactor

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* dont allow for events that wont be published to attempt to redact

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* allow for lib consumers to set a redactor

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* account for lib consumers without needing to pass a redact store

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* dont allow for replacing the redact store

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
Signed-off-by: Keith Zantow <kzantow@gmail.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Co-authored-by: Keith Zantow <kzantow@gmail.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman and kzantow committed Jun 30, 2023
1 parent 95ef16f commit 41d7d7f
Show file tree
Hide file tree
Showing 24 changed files with 283 additions and 287 deletions.
41 changes: 13 additions & 28 deletions cmd/quill/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,15 @@ 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).
WithUIConstructor(
func(cfg clio.Config) ([]clio.UI, error) {
noUI := ui.None()
Expand All @@ -46,15 +29,21 @@ func New(version clio.Version) *cobra.Command {
},
).
WithInitializers(
func(cfg clio.Config, state clio.State) error {
func(state *clio.State) error {
bus.Set(state.Bus)
redact.Set(state.RedactStore)
log.Set(state.Logger)

return nil
},
)
).
WithGlobalConfigFlag().
WithGlobalLoggingFlags().
WithConfigInRootHelp()

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 +57,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 +67,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
31 changes: 16 additions & 15 deletions cmd/quill/cli/commands/sign_and_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"
)

Expand Down Expand Up @@ -47,24 +48,24 @@ func SignAndNotarize(app clio.Application) *cobra.Command {
},
),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Run(cmd.Context(), async(func() error {
err := sign(opts.Path, opts.Signing)
if err != nil {
return fmt.Errorf("signing failed: %w", err)
}
defer bus.Exit()

if opts.DryRun {
log.Warn("[DRY RUN] skipping notarization...")
return nil
}

_, err = notarize(opts.Path, opts.Notary, opts.Status)
if err != nil {
return fmt.Errorf("notarization failed: %w", err)
}
err := sign(opts.Path, opts.Signing)
if err != nil {
return fmt.Errorf("signing failed: %w", err)
}

if opts.DryRun {
log.Warn("[DRY RUN] skipping notarization...")
return nil
}))
}

_, err = notarize(opts.Path, opts.Notary, opts.Status)
if err != nil {
return fmt.Errorf("notarization failed: %w", err)
}

return nil
},
}, opts)
}
Loading

0 comments on commit 41d7d7f

Please sign in to comment.