Skip to content

Commit

Permalink
refactor(cli): Move cmdflags package under pkg/cli/client
Browse files Browse the repository at this point in the history
Signed-off-by: Alexei Dodon <[email protected]>
  • Loading branch information
adodon2go committed Sep 22, 2023
1 parent 8c55944 commit 9377f46
Show file tree
Hide file tree
Showing 18 changed files with 447 additions and 450 deletions.
14 changes: 6 additions & 8 deletions pkg/cli/client/cves_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package client

import (
"github.com/spf13/cobra"

"zotregistry.io/zot/pkg/cli/cmdflags"
)

func NewCVECommand(searchService SearchService) *cobra.Command {
Expand All @@ -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))
Expand Down
21 changes: 10 additions & 11 deletions pkg/cli/client/cves_sub_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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{
Expand All @@ -46,17 +45,17 @@ 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
}

func NewImagesByCVEIDCommand(searchService SearchService) *cobra.Command {
var (
repo string
imageListSortFlag = cmdflags.ImageListSortFlag(cmdflags.SortByAlphabeticAsc)
imageListSortFlag = ImageListSortFlag(SortByAlphabeticAsc)
)

imagesByCVEIDCmd := &cobra.Command{
Expand Down Expand Up @@ -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]",
Expand Down Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion pkg/cli/cmdflags/flags.go → pkg/cli/client/flags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package cmdflags
//go:build search
// +build search

package client

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cmdflags_test
//go:build search
// +build search

package client_test

import (
"testing"

. "github.com/smartystreets/goconvey/convey"

. "zotregistry.io/zot/pkg/cli/cmdflags"
. "zotregistry.io/zot/pkg/cli/client"
gql_gen "zotregistry.io/zot/pkg/extensions/search/gql_generated"
)

Expand Down
14 changes: 6 additions & 8 deletions pkg/cli/client/image_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"time"

"github.com/spf13/cobra"

"zotregistry.io/zot/pkg/cli/cmdflags"
)

const (
Expand All @@ -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))
Expand Down
39 changes: 19 additions & 20 deletions pkg/cli/client/image_sub_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,16 +34,16 @@ 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
}

func NewImageCVEListCommand(searchService SearchService) *cobra.Command {
var (
searchedCVEID string
cveListSortFlag = cmdflags.CVEListSortFlag(cmdflags.SortBySeverity)
cveListSortFlag = CVEListSortFlag(SortBySeverity)
)

cmd := &cobra.Command{
Expand All @@ -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]",
Expand All @@ -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]",
Expand All @@ -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]",
Expand All @@ -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]",
Expand Down Expand Up @@ -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
}
10 changes: 4 additions & 6 deletions pkg/cli/client/repo_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package client

import (
"github.com/spf13/cobra"

"zotregistry.io/zot/pkg/cli/cmdflags"
)

const prefix = "Searching... "
Expand All @@ -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))

Expand Down
8 changes: 3 additions & 5 deletions pkg/cli/client/repo_sub_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cli/client/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
14 changes: 6 additions & 8 deletions pkg/cli/client/search_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package client

import (
"github.com/spf13/cobra"

"zotregistry.io/zot/pkg/cli/cmdflags"
)

func NewSearchCommand(searchService SearchService) *cobra.Command {
Expand All @@ -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))
Expand Down
Loading

0 comments on commit 9377f46

Please sign in to comment.