2022-10-10 07:05:55 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
2020-10-14 16:47:20 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-07-14 12:11:01 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-16 20:52:40 -05:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/briandowns/spinner"
|
|
|
|
"github.com/spf13/cobra"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func NewImageCommand(searchService SearchService) *cobra.Command {
|
2020-06-16 20:52:40 -05:00
|
|
|
searchImageParams := make(map[string]*string)
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
var servURL, user, outputFormat string
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
var isSpinner, verifyTLS, verbose, debug bool
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imageCmd := &cobra.Command{
|
2020-06-16 20:52:40 -05:00
|
|
|
Use: "images [config-name]",
|
2022-01-10 20:15:35 -05:00
|
|
|
Short: "List images hosted on the zot registry",
|
|
|
|
Long: `List images hosted on the zot registry`,
|
2020-06-16 20:52:40 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-14 12:11:01 -05:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := path.Join(home + "/.zot")
|
2020-06-16 20:52:40 -05:00
|
|
|
if servURL == "" {
|
|
|
|
if len(args) > 0 {
|
|
|
|
urlFromConfig, err := getConfigValue(configPath, args[0], "url")
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
if urlFromConfig == "" {
|
|
|
|
return zotErrors.ErrNoURLProvided
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
servURL = urlFromConfig
|
|
|
|
} else {
|
|
|
|
return zotErrors.ErrNoURLProvided
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
var err error
|
2020-07-17 14:42:22 -05:00
|
|
|
isSpinner, err = parseBooleanConfig(configPath, args[0], showspinnerConfig)
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
verifyTLS, err = parseBooleanConfig(configPath, args[0], verifyTLSConfig)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
spin := spinner.New(spinner.CharSets[39], spinnerDuration, spinner.WithWriter(cmd.ErrOrStderr()))
|
|
|
|
spin.Prefix = "Searching... "
|
|
|
|
|
|
|
|
searchConfig := searchConfig{
|
|
|
|
params: searchImageParams,
|
|
|
|
searchService: searchService,
|
|
|
|
servURL: &servURL,
|
|
|
|
user: &user,
|
|
|
|
outputFormat: &outputFormat,
|
2021-05-28 11:27:17 -05:00
|
|
|
verbose: &verbose,
|
2022-09-23 11:24:01 -05:00
|
|
|
debug: &debug,
|
2020-07-17 14:42:22 -05:00
|
|
|
spinner: spinnerState{spin, isSpinner},
|
|
|
|
verifyTLS: &verifyTLS,
|
|
|
|
resultWriter: cmd.OutOrStdout(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = searchImage(searchConfig)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
setupImageFlags(imageCmd, searchImageParams, &servURL, &user, &outputFormat, &verbose, &debug)
|
2020-06-16 20:52:40 -05:00
|
|
|
imageCmd.SetUsageTemplate(imageCmd.UsageTemplate() + usageFooter)
|
|
|
|
|
|
|
|
return imageCmd
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func parseBooleanConfig(configPath, configName, configParam string) (bool, error) {
|
|
|
|
config, err := getConfigValue(configPath, configName, configParam)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
val, err := strconv.ParseBool(config)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
return val, nil
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func setupImageFlags(imageCmd *cobra.Command, searchImageParams map[string]*string,
|
2022-09-23 11:24:01 -05:00
|
|
|
servURL, user, outputFormat *string, verbose *bool, debug *bool,
|
2022-03-21 12:37:23 -05:00
|
|
|
) {
|
2020-06-16 20:52:40 -05:00
|
|
|
searchImageParams["imageName"] = imageCmd.Flags().StringP("name", "n", "", "List image details by name")
|
2021-05-26 12:22:31 -05:00
|
|
|
searchImageParams["digest"] = imageCmd.Flags().StringP("digest", "d", "",
|
|
|
|
"List images containing a specific manifest, config, or layer digest")
|
2022-09-23 11:23:31 -05:00
|
|
|
searchImageParams["derivedImage"] = imageCmd.Flags().StringP("derived-images", "D", "",
|
|
|
|
"List images that are derived from given image")
|
2022-09-22 14:08:58 -05:00
|
|
|
searchImageParams["baseImage"] = imageCmd.Flags().StringP("base-images", "b", "",
|
|
|
|
"List images that are base for the given image")
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
imageCmd.Flags().StringVar(servURL, "url", "", "Specify zot server URL if config-name is not mentioned")
|
|
|
|
imageCmd.Flags().StringVarP(user, "user", "u", "", `User Credentials of zot server in "username:password" format`)
|
|
|
|
imageCmd.Flags().StringVarP(outputFormat, "output", "o", "", "Specify output format [text/json/yaml]")
|
2021-05-28 11:27:17 -05:00
|
|
|
imageCmd.Flags().BoolVar(verbose, "verbose", false, "Show verbose output")
|
2022-09-23 11:24:01 -05:00
|
|
|
imageCmd.Flags().BoolVar(debug, "debug", false, "Show debug output")
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func searchImage(searchConfig searchConfig) error {
|
2022-01-19 10:57:10 -05:00
|
|
|
var searchers []searcher
|
|
|
|
|
2022-10-23 01:44:20 -05:00
|
|
|
if checkExtEndPoint(searchConfig) {
|
2022-01-19 10:57:10 -05:00
|
|
|
searchers = getImageSearchersGQL()
|
|
|
|
} else {
|
|
|
|
searchers = getImageSearchers()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, searcher := range searchers {
|
2020-07-17 14:42:22 -05:00
|
|
|
found, err := searcher.search(searchConfig)
|
2020-06-16 20:52:40 -05:00
|
|
|
if found {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zotErrors.ErrInvalidFlagsCombination
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
spinnerDuration = 150 * time.Millisecond
|
|
|
|
usageFooter = `
|
2022-01-10 20:15:35 -05:00
|
|
|
Run 'zli config -h' for details on [config-name] argument
|
2020-06-16 20:52:40 -05:00
|
|
|
`
|
|
|
|
)
|