expose cobra command in cli package (#2097)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2023-09-05 10:33:38 -04:00 committed by GitHub
parent 007b034ee3
commit 49e7f399f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"os" "os"
cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd" cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd"
"github.com/spf13/cobra"
"github.com/anchore/clio" "github.com/anchore/clio"
"github.com/anchore/stereoscope" "github.com/anchore/stereoscope"
@ -15,12 +16,24 @@ import (
"github.com/anchore/syft/internal/redact" "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 // and constructs the `syft power-user` command. It is also responsible for
// organizing flag usage and injecting the application config for each command. // organizing flag usage and injecting the application config for each command.
// It also constructs the syft attest command and the syft version 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. // `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). clioCfg := clio.NewSetupConfig(id).
WithGlobalConfigFlag(). // add persistent -c <path> for reading an application config from WithGlobalConfigFlag(). // add persistent -c <path> for reading an application config from
WithGlobalLoggingFlags(). // add persistent -v and -q flags tied to the logging config WithGlobalLoggingFlags(). // add persistent -v and -q flags tied to the logging config
@ -77,5 +90,5 @@ func New(id clio.Identification) clio.Application {
cranecmd.NewCmdAuthLogin(id.Name), // syft login uses the same command as crane cranecmd.NewCmdAuthLogin(id.Name), // syft login uses the same command as crane
) )
return app return app, rootCmd
} }

View File

@ -20,7 +20,7 @@ var (
) )
func main() { func main() {
app := cli.New( app := cli.Application(
clio.Identification{ clio.Identification{
Name: applicationName, Name: applicationName,
Version: version, Version: version,