mirror of
https://github.com/anchore/grype
synced 2024-09-20 06:21:56 +00:00
add CLI commands / subcommands
This commit is contained in:
parent
3c6ae01619
commit
d813ac84ca
12 changed files with 235 additions and 72 deletions
14
cmd/cache.go
Normal file
14
cmd/cache.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cacheCmd = &cobra.Command{
|
||||
Use: "cache",
|
||||
Short: "operate on the result cache",
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(cacheCmd)
|
||||
}
|
24
cmd/cache_clear.go
Normal file
24
cmd/cache_clear.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cacheClearCmd = &cobra.Command{
|
||||
Use: "clear",
|
||||
Short: "delete the results cache",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runCacheClearCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cacheCmd.AddCommand(cacheClearCmd)
|
||||
}
|
||||
|
||||
func runCacheClearCmd(cmd *cobra.Command, args []string) int {
|
||||
log.Error("cache CLEAR command...")
|
||||
return 0
|
||||
}
|
24
cmd/cache_show.go
Normal file
24
cmd/cache_show.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cacheShowCmd = &cobra.Command{
|
||||
Use: "show",
|
||||
Short: "show images that have been scanned",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runCacheShowCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cacheCmd.AddCommand(cacheShowCmd)
|
||||
}
|
||||
|
||||
func runCacheShowCmd(cmd *cobra.Command, args []string) int {
|
||||
log.Error("cache SHOW command...")
|
||||
return 0
|
||||
}
|
51
cmd/cli.go
51
cmd/cli.go
|
@ -1,51 +0,0 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/scope"
|
||||
"github.com/anchore/vulnscan/internal/config"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cliOpts = config.CliOnlyOptions{}
|
||||
|
||||
func setCliOptions() {
|
||||
rootCmd.PersistentFlags().StringVarP(&cliOpts.ConfigPath, "config", "c", "", "application config file")
|
||||
|
||||
// scan options
|
||||
flag := "scope"
|
||||
rootCmd.Flags().StringP(
|
||||
"scope", "s", scope.AllLayersScope.String(),
|
||||
fmt.Sprintf("selection of layers to analyze, options=%v", scope.Options))
|
||||
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
|
||||
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// output & formatting options
|
||||
flag = "output"
|
||||
rootCmd.Flags().StringP(
|
||||
// TODO: default option
|
||||
flag, "o", "text",
|
||||
// TODO: show all options
|
||||
fmt.Sprintf("report output formatter, options=%v", []string{}),
|
||||
)
|
||||
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
|
||||
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
flag = "quiet"
|
||||
rootCmd.Flags().BoolP(
|
||||
flag, "q", false,
|
||||
"suppress all logging output",
|
||||
)
|
||||
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
|
||||
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rootCmd.Flags().CountVarP(&cliOpts.Verbosity, "verbose", "v", "increase verbosity (-v = info, -vv = debug)")
|
||||
}
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/anchore/vulnscan/internal/format"
|
||||
"github.com/anchore/vulnscan/internal/logger"
|
||||
"github.com/anchore/vulnscan/vulnscan"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
@ -16,9 +17,39 @@ import (
|
|||
|
||||
var appConfig *config.Application
|
||||
var log *zap.SugaredLogger
|
||||
var cliOnlyOpts config.CliOnlyOptions
|
||||
|
||||
func init() {
|
||||
// setup global CLI options (available on all CLI commands)
|
||||
rootCmd.PersistentFlags().StringVarP(&cliOnlyOpts.ConfigPath, "config", "c", "", "application config file")
|
||||
|
||||
flag := "quiet"
|
||||
rootCmd.PersistentFlags().BoolP(
|
||||
flag, "q", false,
|
||||
"suppress all logging output",
|
||||
)
|
||||
if err := viper.BindPFlag(flag, rootCmd.PersistentFlags().Lookup(flag)); err != nil {
|
||||
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rootCmd.PersistentFlags().CountVarP(&cliOnlyOpts.Verbosity, "verbose", "v", "increase verbosity (-v = info, -vv = debug)")
|
||||
|
||||
// read in config and setup logger
|
||||
cobra.OnInitialize(initAppConfig)
|
||||
cobra.OnInitialize(initLogging)
|
||||
cobra.OnInitialize(logAppConfig)
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
log.Errorf("could not start application: %w", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func initAppConfig() {
|
||||
cfg, err := config.LoadConfigFromFile(viper.GetViper(), &cliOpts)
|
||||
cfg, err := config.LoadConfigFromFile(viper.GetViper(), &cliOnlyOpts)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load application config: \n\t%+v\n", err)
|
||||
os.Exit(1)
|
14
cmd/db.go
Normal file
14
cmd/db.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var dbCmd = &cobra.Command{
|
||||
Use: "db",
|
||||
Short: "vulnerability database operations",
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(dbCmd)
|
||||
}
|
24
cmd/db_check.go
Normal file
24
cmd/db_check.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var dbCheckCmd = &cobra.Command{
|
||||
Use: "check",
|
||||
Short: "check to see if there is a database update available",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runDbCheckCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
dbCmd.AddCommand(dbCheckCmd)
|
||||
}
|
||||
|
||||
func runDbCheckCmd(cmd *cobra.Command, args []string) int {
|
||||
log.Error("database CHECK command...")
|
||||
return 0
|
||||
}
|
24
cmd/db_clear.go
Normal file
24
cmd/db_clear.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var dbClearCmd = &cobra.Command{
|
||||
Use: "clear",
|
||||
Short: "delete the vulnerability database",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runDbClearCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
dbCmd.AddCommand(dbClearCmd)
|
||||
}
|
||||
|
||||
func runDbClearCmd(cmd *cobra.Command, args []string) int {
|
||||
log.Error("database CLEAR command...")
|
||||
return 0
|
||||
}
|
24
cmd/db_update.go
Normal file
24
cmd/db_update.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var dbUpdateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "download the latest vulnerability database",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runDbUpdateCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
dbCmd.AddCommand(dbUpdateCmd)
|
||||
}
|
||||
|
||||
func runDbUpdateCmd(cmd *cobra.Command, args []string) int {
|
||||
log.Error("database UPDATE command...")
|
||||
return 0
|
||||
}
|
47
cmd/root.go
47
cmd/root.go
|
@ -5,47 +5,58 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom"
|
||||
"github.com/anchore/imgbom/imgbom/scope"
|
||||
"github.com/anchore/stereoscope"
|
||||
"github.com/anchore/vulnscan/internal"
|
||||
"github.com/anchore/vulnscan/internal/format"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf("%s [IMAGE]", internal.ApplicationName),
|
||||
Short: "A container image BOM tool", // TODO: add copy
|
||||
Long: format.Tprintf(`\
|
||||
Supports the following image sources:
|
||||
Short: "A container image vulnerability scanner", // TODO: add copy
|
||||
Long: format.Tprintf(`Supports the following image sources:
|
||||
{{.appName}} yourrepo/yourimage:tag defaults to using images from a docker daemon
|
||||
{{.appName}} docker://yourrepo/yourimage:tag explicitly use the docker daemon
|
||||
{{.appName}} docker://yourrepo/yourimage:tag explicitly use a docker daemon
|
||||
{{.appName}} tar://path/to/yourimage.tar use a tarball from disk
|
||||
`, map[string]interface{}{
|
||||
"appName": internal.ApplicationName,
|
||||
}),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: runCmdWrapper,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runDefaultCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
setCliOptions()
|
||||
// setup CLI options specific to scanning an image
|
||||
|
||||
cobra.OnInitialize(initAppConfig)
|
||||
cobra.OnInitialize(initLogging)
|
||||
cobra.OnInitialize(logAppConfig)
|
||||
}
|
||||
// scan options
|
||||
flag := "scope"
|
||||
rootCmd.Flags().StringP(
|
||||
"scope", "s", scope.AllLayersScope.String(),
|
||||
fmt.Sprintf("selection of layers to analyze, options=%v", scope.Options))
|
||||
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
|
||||
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
log.Errorf("could not start application: %w", err)
|
||||
// output & formatting options
|
||||
flag = "output"
|
||||
rootCmd.Flags().StringP(
|
||||
// TODO: default option
|
||||
flag, "o", "text",
|
||||
// TODO: show all options
|
||||
fmt.Sprintf("report output formatter, options=%v", []string{}),
|
||||
)
|
||||
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
|
||||
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func runCmdWrapper(cmd *cobra.Command, args []string) {
|
||||
os.Exit(doRunCmd(cmd, args))
|
||||
}
|
||||
|
||||
func doRunCmd(cmd *cobra.Command, args []string) int {
|
||||
func runDefaultCmd(cmd *cobra.Command, args []string) int {
|
||||
userImageStr := args[0]
|
||||
log.Infof("Fetching image '%s'", userImageStr)
|
||||
img, err := stereoscope.GetImage(userImageStr)
|
||||
|
|
24
cmd/status.go
Normal file
24
cmd/status.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var statusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "display general status",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
os.Exit(runStatusCmd(cmd, args))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(statusCmd)
|
||||
}
|
||||
|
||||
func runStatusCmd(cmd *cobra.Command, args []string) int {
|
||||
log.Error("status command...")
|
||||
return 0
|
||||
}
|
|
@ -18,7 +18,7 @@ var version *Version
|
|||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "show the version",
|
||||
Run: printVersion,
|
||||
Run: runVersionCmd,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -29,6 +29,6 @@ func SetVersion(v *Version) {
|
|||
version = v
|
||||
}
|
||||
|
||||
func printVersion(cmd *cobra.Command, args []string) {
|
||||
func runVersionCmd(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("%s %s\n", internal.ApplicationName, version.Version)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue