github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/cli/cli.go (about) 1 package cli 2 3 import ( 4 "io" 5 "os" 6 7 cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd" 8 "github.com/spf13/cobra" 9 10 "github.com/anchore/clio" 11 "github.com/anchore/syft/cmd/syft/internal" 12 "github.com/anchore/syft/cmd/syft/internal/commands" 13 ) 14 15 // Application constructs the `syft packages` command and aliases the root command to `syft packages`. 16 // It is also responsible for organizing flag usage and injecting the application config for each command. 17 // It also constructs the syft attest command and the syft version command. 18 // `RunE` is the earliest that the complete application configuration can be loaded. 19 func Application(id clio.Identification) clio.Application { 20 app, _ := create(id, os.Stdout) 21 return app 22 } 23 24 // Command returns the root command for the syft CLI application. This is useful for embedding the entire syft CLI 25 // into an existing application. 26 func Command(id clio.Identification) *cobra.Command { 27 _, cmd := create(id, os.Stdout) 28 return cmd 29 } 30 31 func create(id clio.Identification, out io.Writer) (clio.Application, *cobra.Command) { 32 clioCfg := internal.AppClioSetupConfig(id, out) 33 34 app := clio.New(*clioCfg) 35 36 // since root is aliased as the packages cmd we need to construct this command first 37 // we also need the command to have information about the `root` options because of this alias 38 scanCmd := commands.Scan(app) 39 40 // root is currently an alias for the scan command 41 rootCmd := commands.Root(app, scanCmd) 42 43 // add sub-commands 44 rootCmd.AddCommand( 45 scanCmd, 46 commands.Packages(app, scanCmd), // this is currently an alias for the scan command 47 commands.Cataloger(app), 48 commands.Attest(app), 49 commands.Convert(app), 50 clio.VersionCommand(id), 51 cranecmd.NewCmdAuthLogin(id.Name), // syft login uses the same command as crane 52 ) 53 54 // note: we would direct cobra to use our writer explicitly with rootCmd.SetOut(out) , however this causes 55 // deprecation warnings to be shown to stdout via the writer instead of stderr. This is unfortunate since this 56 // does not appear to be the correct behavior on cobra's part https://github.com/spf13/cobra/issues/1708 . 57 // In the future this functionality should be restored. 58 59 return app, rootCmd 60 }