2020-10-14 16:47:20 -05:00
|
|
|
// +build extended
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
zotErrors "github.com/anuvu/zot/errors"
|
|
|
|
"github.com/briandowns/spinner"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
var isSpinner, verifyTLS, verbose bool
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
var imageCmd = &cobra.Command{
|
|
|
|
Use: "images [config-name]",
|
|
|
|
Short: "List hosted images",
|
|
|
|
Long: `List images hosted on zot`,
|
|
|
|
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
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if urlFromConfig == "" {
|
|
|
|
return zotErrors.ErrNoURLProvided
|
|
|
|
}
|
|
|
|
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
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
verifyTLS, err = parseBooleanConfig(configPath, args[0], verifyTLSConfig)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
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,
|
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
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
setupImageFlags(imageCmd, searchImageParams, &servURL, &user, &outputFormat, &verbose)
|
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,
|
2021-05-28 11:27:17 -05:00
|
|
|
servURL, user, outputFormat *string, verbose *bool) {
|
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")
|
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")
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func searchImage(searchConfig searchConfig) error {
|
2020-07-06 17:44:32 -05:00
|
|
|
for _, searcher := range getImageSearchers() {
|
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 = `
|
|
|
|
Run 'zot config -h' for details on [config-name] argument
|
|
|
|
`
|
|
|
|
)
|