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

Expose cobra command in cli package #2097

Merged
merged 1 commit into from
Sep 5, 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
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
Loading