Skip to content

Commit

Permalink
expose cobra command in cli package (#2097)
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 Sep 5, 2023
1 parent 007b034 commit 49e7f39
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions cmd/syft/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd"
"github.com/spf13/cobra"

"github.com/anchore/clio"
"github.com/anchore/stereoscope"
Expand All @@ -15,12 +16,24 @@ import (
"github.com/anchore/syft/internal/redact"
)

// New constructs the `syft packages` command, aliases the root command to `syft packages`,
// Application constructs the `syft packages` command, aliases the root command to `syft packages`,
// and constructs the `syft power-user` command. It is also responsible for
// organizing flag usage and injecting the application config for each command.
// It also constructs the syft attest command and the syft version command.
// `RunE` is the earliest that the complete application configuration can be loaded.
func New(id clio.Identification) clio.Application {
func Application(id clio.Identification) clio.Application {
app, _ := create(id)
return app
}

// Command returns the root command for the syft CLI application. This is useful for embedding the entire syft CLI
// into an existing application.
func Command(id clio.Identification) *cobra.Command {
_, cmd := create(id)
return cmd
}

func create(id clio.Identification) (clio.Application, *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
Expand Down Expand Up @@ -77,5 +90,5 @@ func New(id clio.Identification) clio.Application {
cranecmd.NewCmdAuthLogin(id.Name), // syft login uses the same command as crane
)

return app
return app, rootCmd
}
2 changes: 1 addition & 1 deletion cmd/syft/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
)

func main() {
app := cli.New(
app := cli.Application(
clio.Identification{
Name: applicationName,
Version: version,
Expand Down

0 comments on commit 49e7f39

Please sign in to comment.