refactor(cli): Move cmdflags package under pkg/cli/client (#1840)

Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
Alexei Dodon
2023-09-22 16:33:18 +03:00
committed by GitHub
parent 8c559441e6
commit 4e04be420e
18 changed files with 447 additions and 450 deletions
+6 -8
View File
@@ -5,8 +5,6 @@ package client
import (
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
func NewCVECommand(searchService SearchService) *cobra.Command {
@@ -19,15 +17,15 @@ func NewCVECommand(searchService SearchService) *cobra.Command {
cvesCmd.SetUsageTemplate(cvesCmd.UsageTemplate() + usageFooter)
cvesCmd.PersistentFlags().String(cmdflags.URLFlag, "",
cvesCmd.PersistentFlags().String(URLFlag, "",
"Specify zot server URL if config-name is not mentioned")
cvesCmd.PersistentFlags().String(cmdflags.ConfigFlag, "",
cvesCmd.PersistentFlags().String(ConfigFlag, "",
"Specify the registry configuration to use for connection")
cvesCmd.PersistentFlags().StringP(cmdflags.UserFlag, "u", "",
cvesCmd.PersistentFlags().StringP(UserFlag, "u", "",
`User Credentials of zot server in "username:password" format`)
cvesCmd.PersistentFlags().StringP(cmdflags.OutputFormatFlag, "f", "", "Specify output format [text/json/yaml]")
cvesCmd.PersistentFlags().Bool(cmdflags.VerboseFlag, false, "Show verbose output")
cvesCmd.PersistentFlags().Bool(cmdflags.DebugFlag, false, "Show debug output")
cvesCmd.PersistentFlags().StringP(OutputFormatFlag, "f", "", "Specify output format [text/json/yaml]")
cvesCmd.PersistentFlags().Bool(VerboseFlag, false, "Show verbose output")
cvesCmd.PersistentFlags().Bool(DebugFlag, false, "Show debug output")
cvesCmd.AddCommand(NewCveForImageCommand(searchService))
cvesCmd.AddCommand(NewImagesByCVEIDCommand(searchService))
+10 -11
View File
@@ -10,7 +10,6 @@ import (
"github.com/spf13/cobra"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/cli/cmdflags"
zcommon "zotregistry.io/zot/pkg/common"
)
@@ -21,7 +20,7 @@ const (
func NewCveForImageCommand(searchService SearchService) *cobra.Command {
var (
searchedCVEID string
cveListSortFlag = cmdflags.CVEListSortFlag(cmdflags.SortBySeverity)
cveListSortFlag = CVEListSortFlag(SortBySeverity)
)
cveForImageCmd := &cobra.Command{
@@ -46,9 +45,9 @@ func NewCveForImageCommand(searchService SearchService) *cobra.Command {
},
}
cveForImageCmd.Flags().StringVar(&searchedCVEID, cmdflags.SearchedCVEID, "", "Search for a specific CVE by name/id")
cveForImageCmd.Flags().Var(&cveListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.CVEListSortOptionsStr()))
cveForImageCmd.Flags().StringVar(&searchedCVEID, SearchedCVEID, "", "Search for a specific CVE by name/id")
cveForImageCmd.Flags().Var(&cveListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", CVEListSortOptionsStr()))
return cveForImageCmd
}
@@ -56,7 +55,7 @@ func NewCveForImageCommand(searchService SearchService) *cobra.Command {
func NewImagesByCVEIDCommand(searchService SearchService) *cobra.Command {
var (
repo string
imageListSortFlag = cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag = ImageListSortFlag(SortByAlphabeticAsc)
)
imagesByCVEIDCmd := &cobra.Command{
@@ -92,14 +91,14 @@ func NewImagesByCVEIDCommand(searchService SearchService) *cobra.Command {
}
imagesByCVEIDCmd.Flags().StringVar(&repo, "repo", "", "Search for a specific CVE by name/id")
imagesByCVEIDCmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
imagesByCVEIDCmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return imagesByCVEIDCmd
}
func NewFixedTagsCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
fixedTagsCmd := &cobra.Command{
Use: "fixed [repo] [cveId]",
@@ -136,8 +135,8 @@ func NewFixedTagsCommand(searchService SearchService) *cobra.Command {
},
}
fixedTagsCmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
fixedTagsCmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return fixedTagsCmd
}
+164
View File
@@ -0,0 +1,164 @@
//go:build search
// +build search
package client
import (
"fmt"
"strings"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/common"
)
const (
URLFlag = "url"
ConfigFlag = "config"
UserFlag = "user"
OutputFormatFlag = "format"
FixedFlag = "fixed"
VerboseFlag = "verbose"
VersionFlag = "version"
DebugFlag = "debug"
SearchedCVEID = "cve-id"
SortByFlag = "sort-by"
)
const (
SortByRelevance = "relevance"
SortByUpdateTime = "update-time"
SortByAlphabeticAsc = "alpha-asc"
SortByAlphabeticDsc = "alpha-dsc"
SortBySeverity = "severity"
)
const stringType = "string"
func ImageListSortOptions() []string {
return []string{SortByUpdateTime, SortByAlphabeticAsc, SortByAlphabeticDsc}
}
func ImageListSortOptionsStr() string {
return strings.Join(ImageListSortOptions(), ", ")
}
func ImageSearchSortOptions() []string {
return []string{SortByRelevance, SortByUpdateTime, SortByAlphabeticAsc, SortByAlphabeticDsc}
}
func ImageSearchSortOptionsStr() string {
return strings.Join(ImageSearchSortOptions(), ", ")
}
func CVEListSortOptions() []string {
return []string{SortByAlphabeticAsc, SortByAlphabeticDsc, SortBySeverity}
}
func CVEListSortOptionsStr() string {
return strings.Join(CVEListSortOptions(), ", ")
}
func RepoListSortOptions() []string {
return []string{SortByAlphabeticAsc, SortByAlphabeticDsc}
}
func RepoListSortOptionsStr() string {
return strings.Join(RepoListSortOptions(), ", ")
}
func Flag2SortCriteria(sortBy string) string {
switch sortBy {
case SortByRelevance:
return "RELEVANCE"
case SortByUpdateTime:
return "UPDATE_TIME"
case SortByAlphabeticAsc:
return "ALPHABETIC_ASC"
case SortByAlphabeticDsc:
return "ALPHABETIC_DSC"
case SortBySeverity:
return "SEVERITY"
default:
return "BAD_SORT_CRITERIA"
}
}
type CVEListSortFlag string
func (e *CVEListSortFlag) String() string {
return string(*e)
}
func (e *CVEListSortFlag) Set(val string) error {
if !common.Contains(CVEListSortOptions(), val) {
return fmt.Errorf("%w %s", zerr.ErrFlagValueUnsupported, CVEListSortOptionsStr())
}
*e = CVEListSortFlag(val)
return nil
}
func (e *CVEListSortFlag) Type() string {
return stringType
}
type ImageListSortFlag string
func (e *ImageListSortFlag) String() string {
return string(*e)
}
func (e *ImageListSortFlag) Set(val string) error {
if !common.Contains(ImageListSortOptions(), val) {
return fmt.Errorf("%w %s", zerr.ErrFlagValueUnsupported, ImageListSortOptionsStr())
}
*e = ImageListSortFlag(val)
return nil
}
func (e *ImageListSortFlag) Type() string {
return stringType
}
type ImageSearchSortFlag string
func (e *ImageSearchSortFlag) String() string {
return string(*e)
}
func (e *ImageSearchSortFlag) Set(val string) error {
if !common.Contains(ImageSearchSortOptions(), val) {
return fmt.Errorf("%w %s", zerr.ErrFlagValueUnsupported, ImageSearchSortOptionsStr())
}
*e = ImageSearchSortFlag(val)
return nil
}
func (e *ImageSearchSortFlag) Type() string {
return stringType
}
type RepoListSortFlag string
func (e *RepoListSortFlag) String() string {
return string(*e)
}
func (e *RepoListSortFlag) Set(val string) error {
if !common.Contains(RepoListSortOptions(), val) {
return fmt.Errorf("%w %s", zerr.ErrFlagValueUnsupported, RepoListSortOptionsStr())
}
*e = RepoListSortFlag(val)
return nil
}
func (e *RepoListSortFlag) Type() string {
return stringType
}
+48
View File
@@ -0,0 +1,48 @@
//go:build search
// +build search
package client_test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
. "zotregistry.io/zot/pkg/cli/client"
gql_gen "zotregistry.io/zot/pkg/extensions/search/gql_generated"
)
func TestSortFlagsMapping(t *testing.T) {
// We do this to not import the whole gql_gen in the CLI
Convey("Make sure the sort-by values map correctly to the gql enum type", t, func() {
So(Flag2SortCriteria(SortByRelevance), ShouldResemble, string(gql_gen.SortCriteriaRelevance))
So(Flag2SortCriteria(SortByUpdateTime), ShouldResemble, string(gql_gen.SortCriteriaUpdateTime))
So(Flag2SortCriteria(SortByAlphabeticAsc), ShouldResemble, string(gql_gen.SortCriteriaAlphabeticAsc))
So(Flag2SortCriteria(SortByAlphabeticDsc), ShouldResemble, string(gql_gen.SortCriteriaAlphabeticDsc))
So(Flag2SortCriteria(SortBySeverity), ShouldResemble, string(gql_gen.SortCriteriaSeverity))
})
}
func TestSortFlags(t *testing.T) {
Convey("Flags", t, func() {
cveSortFlag := CVEListSortFlag("")
err := cveSortFlag.Set("bad-flag")
So(err, ShouldNotBeNil)
imageListSortFlag := ImageListSortFlag("")
err = imageListSortFlag.Set("bad-flag")
So(err, ShouldNotBeNil)
imageSearchSortFlag := ImageSearchSortFlag("")
err = imageSearchSortFlag.Set("bad-flag")
So(err, ShouldNotBeNil)
repoListSearchFlag := RepoListSortFlag("")
err = repoListSearchFlag.Set("bad-flag")
So(err, ShouldNotBeNil)
})
Convey("Flag2SortCriteria", t, func() {
So(Flag2SortCriteria("bad-flag"), ShouldResemble, "BAD_SORT_CRITERIA")
})
}
+6 -8
View File
@@ -8,8 +8,6 @@ import (
"time"
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
const (
@@ -29,15 +27,15 @@ func NewImageCommand(searchService SearchService) *cobra.Command {
imageCmd.SetUsageTemplate(imageCmd.UsageTemplate() + usageFooter)
imageCmd.PersistentFlags().String(cmdflags.URLFlag, "",
imageCmd.PersistentFlags().String(URLFlag, "",
"Specify zot server URL if config-name is not mentioned")
imageCmd.PersistentFlags().String(cmdflags.ConfigFlag, "",
imageCmd.PersistentFlags().String(ConfigFlag, "",
"Specify the registry configuration to use for connection")
imageCmd.PersistentFlags().StringP(cmdflags.UserFlag, "u", "",
imageCmd.PersistentFlags().StringP(UserFlag, "u", "",
`User Credentials of zot server in "username:password" format`)
imageCmd.PersistentFlags().StringP(cmdflags.OutputFormatFlag, "f", "", "Specify output format [text/json/yaml]")
imageCmd.PersistentFlags().Bool(cmdflags.VerboseFlag, false, "Show verbose output")
imageCmd.PersistentFlags().Bool(cmdflags.DebugFlag, false, "Show debug output")
imageCmd.PersistentFlags().StringP(OutputFormatFlag, "f", "", "Specify output format [text/json/yaml]")
imageCmd.PersistentFlags().Bool(VerboseFlag, false, "Show verbose output")
imageCmd.PersistentFlags().Bool(DebugFlag, false, "Show debug output")
imageCmd.AddCommand(NewImageListCommand(searchService))
imageCmd.AddCommand(NewImageCVEListCommand(searchService))
+19 -20
View File
@@ -9,12 +9,11 @@ import (
"github.com/spf13/cobra"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/cli/cmdflags"
zcommon "zotregistry.io/zot/pkg/common"
)
func NewImageListCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "list",
@@ -35,8 +34,8 @@ func NewImageListCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
cmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return cmd
}
@@ -44,7 +43,7 @@ func NewImageListCommand(searchService SearchService) *cobra.Command {
func NewImageCVEListCommand(searchService SearchService) *cobra.Command {
var (
searchedCVEID string
cveListSortFlag = cmdflags.CVEListSortFlag(cmdflags.SortBySeverity)
cveListSortFlag = CVEListSortFlag(SortBySeverity)
)
cmd := &cobra.Command{
@@ -68,15 +67,15 @@ func NewImageCVEListCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().StringVar(&searchedCVEID, cmdflags.SearchedCVEID, "", "Search for a specific CVE by name/id")
cmd.Flags().Var(&cveListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.CVEListSortOptionsStr()))
cmd.Flags().StringVar(&searchedCVEID, SearchedCVEID, "", "Search for a specific CVE by name/id")
cmd.Flags().Var(&cveListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", CVEListSortOptionsStr()))
return cmd
}
func NewImageDerivedCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "derived [repo-name:tag]|[repo-name@digest]",
@@ -97,14 +96,14 @@ func NewImageDerivedCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
cmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return cmd
}
func NewImageBaseCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "base [repo-name:tag]|[repo-name@digest]",
@@ -125,14 +124,14 @@ func NewImageBaseCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
cmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return cmd
}
func NewImageDigestCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "digest [digest]",
@@ -155,14 +154,14 @@ zli image digest sha256:8a1930f0...`,
},
}
cmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
cmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return cmd
}
func NewImageNameCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "name [repo:tag]",
@@ -195,8 +194,8 @@ func NewImageNameCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
cmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return cmd
}
+4 -6
View File
@@ -5,8 +5,6 @@ package client
import (
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
const prefix = "Searching... "
@@ -21,13 +19,13 @@ func NewRepoCommand(searchService SearchService) *cobra.Command {
repoCmd.SetUsageTemplate(repoCmd.UsageTemplate() + usageFooter)
repoCmd.PersistentFlags().String(cmdflags.URLFlag, "",
repoCmd.PersistentFlags().String(URLFlag, "",
"Specify zot server URL if config-name is not mentioned")
repoCmd.PersistentFlags().String(cmdflags.ConfigFlag, "",
repoCmd.PersistentFlags().String(ConfigFlag, "",
"Specify the registry configuration to use for connection")
repoCmd.PersistentFlags().StringP(cmdflags.UserFlag, "u", "",
repoCmd.PersistentFlags().StringP(UserFlag, "u", "",
`User Credentials of zot server in "username:password" format`)
repoCmd.PersistentFlags().Bool(cmdflags.DebugFlag, false, "Show debug output")
repoCmd.PersistentFlags().Bool(DebugFlag, false, "Show debug output")
repoCmd.AddCommand(NewListReposCommand(searchService))
+3 -5
View File
@@ -7,12 +7,10 @@ import (
"fmt"
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
func NewListReposCommand(searchService SearchService) *cobra.Command {
repoListSortFlag := cmdflags.RepoListSortFlag(cmdflags.SortByAlphabeticAsc)
repoListSortFlag := RepoListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "list",
@@ -29,8 +27,8 @@ func NewListReposCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&repoListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.RepoListSortOptionsStr()))
cmd.Flags().Var(&repoListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", RepoListSortOptionsStr()))
return cmd
}
+1 -2
View File
@@ -9,7 +9,6 @@ import (
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
// "zli" - client-side cli.
@@ -36,7 +35,7 @@ func NewCliRootCmd() *cobra.Command {
// additional cmds
enableCli(rootCmd)
// "version"
rootCmd.Flags().BoolVarP(&showVersion, cmdflags.VersionFlag, "v", false, "show the version and exit")
rootCmd.Flags().BoolVarP(&showVersion, VersionFlag, "v", false, "show the version and exit")
return rootCmd
}
+6 -8
View File
@@ -5,8 +5,6 @@ package client
import (
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
func NewSearchCommand(searchService SearchService) *cobra.Command {
@@ -19,15 +17,15 @@ func NewSearchCommand(searchService SearchService) *cobra.Command {
searchCmd.SetUsageTemplate(searchCmd.UsageTemplate() + usageFooter)
searchCmd.PersistentFlags().String(cmdflags.URLFlag, "",
searchCmd.PersistentFlags().String(URLFlag, "",
"Specify zot server URL if config-name is not mentioned")
searchCmd.PersistentFlags().String(cmdflags.ConfigFlag, "",
searchCmd.PersistentFlags().String(ConfigFlag, "",
"Specify the registry configuration to use for connection")
searchCmd.PersistentFlags().StringP(cmdflags.UserFlag, "u", "",
searchCmd.PersistentFlags().StringP(UserFlag, "u", "",
`User Credentials of zot server in "username:password" format`)
searchCmd.PersistentFlags().StringP(cmdflags.OutputFormatFlag, "f", "", "Specify output format [text/json/yaml]")
searchCmd.PersistentFlags().Bool(cmdflags.VerboseFlag, false, "Show verbose output")
searchCmd.PersistentFlags().Bool(cmdflags.DebugFlag, false, "Show debug output")
searchCmd.PersistentFlags().StringP(OutputFormatFlag, "f", "", "Specify output format [text/json/yaml]")
searchCmd.PersistentFlags().Bool(VerboseFlag, false, "Show verbose output")
searchCmd.PersistentFlags().Bool(DebugFlag, false, "Show debug output")
searchCmd.AddCommand(NewSearchQueryCommand(searchService))
searchCmd.AddCommand(NewSearchSubjectCommand(searchService))
@@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/cli/cmdflags"
"zotregistry.io/zot/pkg/common"
)
@@ -693,7 +692,7 @@ func TestUtils(t *testing.T) {
// bad showspinner
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":"bad", "verify-tls": false}]}`)
cmd = &cobra.Command{}
cmd.Flags().String(cmdflags.ConfigFlag, "imagetest", "")
cmd.Flags().String(ConfigFlag, "imagetest", "")
isSpinner, verifyTLS, err = GetCliConfigOptions(cmd)
So(err, ShouldNotBeNil)
So(isSpinner, ShouldBeFalse)
@@ -703,7 +702,7 @@ func TestUtils(t *testing.T) {
// bad verify-tls
configPath = makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false, "verify-tls": "bad"}]}`)
cmd = &cobra.Command{}
cmd.Flags().String(cmdflags.ConfigFlag, "imagetest", "")
cmd.Flags().String(ConfigFlag, "imagetest", "")
isSpinner, verifyTLS, err = GetCliConfigOptions(cmd)
So(err, ShouldNotBeNil)
So(isSpinner, ShouldBeFalse)
@@ -713,7 +712,7 @@ func TestUtils(t *testing.T) {
Convey("GetServerURLFromFlags", t, func() {
cmd := &cobra.Command{}
cmd.Flags().String(cmdflags.URLFlag, "url", "")
cmd.Flags().String(URLFlag, "url", "")
url, err := GetServerURLFromFlags(cmd)
So(url, ShouldResemble, "url")
So(err, ShouldBeNil)
@@ -727,7 +726,7 @@ func TestUtils(t *testing.T) {
// err ulr from config is empty
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest"}]}`)
cmd = &cobra.Command{}
cmd.Flags().String(cmdflags.ConfigFlag, "imagetest", "")
cmd.Flags().String(ConfigFlag, "imagetest", "")
url, err = GetServerURLFromFlags(cmd)
So(url, ShouldResemble, "")
So(err, ShouldNotBeNil)
@@ -736,7 +735,7 @@ func TestUtils(t *testing.T) {
// err reading the server url from config
configPath = makeConfigFile("{}")
cmd = &cobra.Command{}
cmd.Flags().String(cmdflags.ConfigFlag, "imagetest", "")
cmd.Flags().String(ConfigFlag, "imagetest", "")
url, err = GetServerURLFromFlags(cmd)
So(url, ShouldResemble, "")
So(err, ShouldNotBeNil)
+7 -8
View File
@@ -9,12 +9,11 @@ import (
"github.com/spf13/cobra"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/cli/cmdflags"
zcommon "zotregistry.io/zot/pkg/common"
)
func NewSearchSubjectCommand(searchService SearchService) *cobra.Command {
imageListSortFlag := cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc)
cmd := &cobra.Command{
Use: "subject [repo:tag]|[repo@digest]",
@@ -39,14 +38,14 @@ func NewSearchSubjectCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&imageListSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageListSortOptionsStr()))
cmd.Flags().Var(&imageListSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr()))
return cmd
}
func NewSearchQueryCommand(searchService SearchService) *cobra.Command {
imageSearchSortFlag := cmdflags.ImageSearchSortFlag(cmdflags.SortByRelevance)
imageSearchSortFlag := ImageSearchSortFlag(SortByRelevance)
cmd := &cobra.Command{
Use: "query [repo]|[repo:tag]",
@@ -54,7 +53,7 @@ func NewSearchQueryCommand(searchService SearchService) *cobra.Command {
Long: "Fuzzy search for repos and their tags.",
Example: `# For repo search specify a substring of the repo name without the tag
zli search query "test/repo"
# For image search specify the full repo name followed by the tag or a prefix of the tag.
zli search query "test/repo:2.1."`,
Args: cobra.ExactArgs(1),
@@ -82,8 +81,8 @@ func NewSearchQueryCommand(searchService SearchService) *cobra.Command {
},
}
cmd.Flags().Var(&imageSearchSortFlag, cmdflags.SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", cmdflags.ImageSearchSortOptionsStr()))
cmd.Flags().Var(&imageSearchSortFlag, SortByFlag,
fmt.Sprintf("Options for sorting the output: [%s]", ImageSearchSortOptionsStr()))
return cmd
}
+12 -13
View File
@@ -22,7 +22,6 @@ import (
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/cli/cmdflags"
"zotregistry.io/zot/pkg/common"
)
@@ -108,7 +107,7 @@ func (service searchService) getDerivedImageListGQL(ctx context.Context, config
IsSigned
}
}
}`, derivedImage, cmdflags.Flag2SortCriteria(config.sortBy))
}`, derivedImage, Flag2SortCriteria(config.sortBy))
result := &common.DerivedImageListResponse{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -180,7 +179,7 @@ func (service searchService) globalSearchGQL(ctx context.Context, config searchC
StarCount
}
}
}`, query, cmdflags.Flag2SortCriteria(config.sortBy))
}`, query, Flag2SortCriteria(config.sortBy))
result := &common.GlobalSearchResultResp{}
@@ -216,7 +215,7 @@ func (service searchService) getBaseImageListGQL(ctx context.Context, config sea
IsSigned
}
}
}`, baseImage, cmdflags.Flag2SortCriteria(config.sortBy))
}`, baseImage, Flag2SortCriteria(config.sortBy))
result := &common.BaseImageListResponse{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -252,7 +251,7 @@ func (service searchService) getImagesGQL(ctx context.Context, config searchConf
IsSigned
}
}
}`, imageName, cmdflags.Flag2SortCriteria(config.sortBy))
}`, imageName, Flag2SortCriteria(config.sortBy))
result := &common.ImageListResponse{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -288,7 +287,7 @@ func (service searchService) getImagesForDigestGQL(ctx context.Context, config s
IsSigned
}
}
}`, digest, cmdflags.Flag2SortCriteria(config.sortBy))
}`, digest, Flag2SortCriteria(config.sortBy))
result := &common.ImagesForDigest{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -304,14 +303,14 @@ func (service searchService) getCveByImageGQL(ctx context.Context, config search
imageName, searchedCVE string,
) (*cveResult, error) {
query := fmt.Sprintf(`
{
CVEListForImage (image:"%s", searchedCVE:"%s", requestedPage: {sortBy: %s}) {
Tag CVEList {
Id Title Severity Description
{
CVEListForImage (image:"%s", searchedCVE:"%s", requestedPage: {sortBy: %s}) {
Tag CVEList {
Id Title Severity Description
PackageList {Name InstalledVersion FixedVersion}
}
}
}`, imageName, searchedCVE, cmdflags.Flag2SortCriteria(config.sortBy))
}`, imageName, searchedCVE, Flag2SortCriteria(config.sortBy))
result := &cveResult{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -348,7 +347,7 @@ func (service searchService) getTagsForCVEGQL(ctx context.Context, config search
}
}
}`,
cveID, cmdflags.Flag2SortCriteria(config.sortBy))
cveID, Flag2SortCriteria(config.sortBy))
result := &common.ImagesForCve{}
err := service.makeGraphQLQuery(ctx, config, username, password, query, result)
@@ -1337,7 +1336,7 @@ func (service searchService) getRepos(ctx context.Context, config searchConfig,
fmt.Fprintln(config.resultWriter, "\nREPOSITORY NAME")
if config.sortBy == cmdflags.SortByAlphabeticAsc {
if config.sortBy == SortByAlphabeticAsc {
for i := 0; i < len(catalog.Repositories); i++ {
fmt.Fprintln(config.resultWriter, catalog.Repositories[i])
}
+10 -12
View File
@@ -18,7 +18,6 @@ import (
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
const (
@@ -370,12 +369,12 @@ func GetSearchConfigFromFlags(cmd *cobra.Command, searchService SearchService) (
}
flags := cmd.Flags()
user := defaultIfError(flags.GetString(cmdflags.UserFlag))
fixed := defaultIfError(flags.GetBool(cmdflags.FixedFlag))
debug := defaultIfError(flags.GetBool(cmdflags.DebugFlag))
verbose := defaultIfError(flags.GetBool(cmdflags.VerboseFlag))
outputFormat := defaultIfError(flags.GetString(cmdflags.OutputFormatFlag))
sortBy := defaultIfError(flags.GetString(cmdflags.SortByFlag))
user := defaultIfError(flags.GetString(UserFlag))
fixed := defaultIfError(flags.GetBool(FixedFlag))
debug := defaultIfError(flags.GetBool(DebugFlag))
verbose := defaultIfError(flags.GetBool(VerboseFlag))
outputFormat := defaultIfError(flags.GetString(OutputFormatFlag))
sortBy := defaultIfError(flags.GetString(SortByFlag))
spin := spinner.New(spinner.CharSets[39], spinnerDuration, spinner.WithWriter(cmd.ErrOrStderr()))
spin.Prefix = prefix
@@ -406,7 +405,7 @@ func defaultIfError[T any](out T, err error) T {
}
func GetCliConfigOptions(cmd *cobra.Command) (bool, bool, error) {
configName, err := cmd.Flags().GetString(cmdflags.ConfigFlag)
configName, err := cmd.Flags().GetString(ConfigFlag)
if err != nil {
return false, false, err
}
@@ -436,19 +435,18 @@ func GetCliConfigOptions(cmd *cobra.Command) (bool, bool, error) {
}
func GetServerURLFromFlags(cmd *cobra.Command) (string, error) {
serverURL, err := cmd.Flags().GetString(cmdflags.URLFlag)
serverURL, err := cmd.Flags().GetString(URLFlag)
if err == nil && serverURL != "" {
return serverURL, nil
}
configName, err := cmd.Flags().GetString(cmdflags.ConfigFlag)
configName, err := cmd.Flags().GetString(ConfigFlag)
if err != nil {
return "", err
}
if configName == "" {
return "", fmt.Errorf("%w: specify either '--%s' or '--%s' flags", zerr.ErrNoURLProvided, cmdflags.URLFlag,
cmdflags.ConfigFlag)
return "", fmt.Errorf("%w: specify either '--%s' or '--%s' flags", zerr.ErrNoURLProvided, URLFlag, ConfigFlag)
}
serverURL, err = ReadServerURLFromConfig(configName)