feat(cli): add sort-by flag to sub commands (#1768)

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
LaurentiuNiculae
2023-09-14 20:51:17 +03:00
committed by GitHub
parent c210e3f377
commit aae8b7b4e3
23 changed files with 714 additions and 98 deletions
+148
View File
@@ -1,5 +1,13 @@
package cmdflags
import (
"fmt"
"strings"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/common"
)
const (
URLFlag = "url"
ConfigFlag = "config"
@@ -10,4 +18,144 @@ const (
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
}
+45
View File
@@ -0,0 +1,45 @@
package cmdflags_test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
. "zotregistry.io/zot/pkg/cli/cmdflags"
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")
})
}