Skip to content

Commit

Permalink
Add list project cmd
Browse files Browse the repository at this point in the history
This command will help to list
all the available registry. It also
has different options to list registries.

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 15, 2023
1 parent 58a1714 commit 94cafa1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/project/list_project.go
Expand Up @@ -26,7 +26,7 @@ func NewListProjectCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "project",
Short: "list projects",
Short: "list project",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
Expand Down
55 changes: 55 additions & 0 deletions cmd/registry/list_registry.go
@@ -0,0 +1,55 @@
package registry

import (
"context"

"github.com/akshatdalton/harbor-cli/cmd/constants"
"github.com/akshatdalton/harbor-cli/cmd/utils"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/spf13/cobra"
)

type listRegistryOptions struct {
page int64
pageSize int64
q string
sort string
}

// NewListRegistryCommand creates a new `harbor list registry` command
func NewListRegistryCommand() *cobra.Command {
var opts listRegistryOptions

cmd := &cobra.Command{
Use: "registry",
Short: "list registry",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runListRegistry(opts, credentialName)
},
}

flags := cmd.Flags()
flags.Int64VarP(&opts.page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.pageSize, "page-size", "", 10, "Size of per page")
flags.StringVarP(&opts.q, "query", "q", "", "Query string to query resources")
flags.StringVarP(&opts.sort, "sort", "", "", "Sort the resource list in ascending or descending order")

return cmd
}

func runListRegistry(opts listRegistryOptions, credentialName string) error {
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.ListRegistries(ctx, &registry.ListRegistriesParams{Page: &opts.page, PageSize: &opts.pageSize, Q: &opts.q, Sort: &opts.sort})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response.GetPayload())
return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Expand Up @@ -32,6 +32,7 @@ func newListCommand() *cobra.Command {

cmd.PersistentFlags().String(constants.CredentialNameOption, "", "Name of the credential to use for authentication")
cmd.AddCommand(project.NewListProjectCommand())
cmd.AddCommand(registry.NewListRegistryCommand())
return cmd
}

Expand Down

0 comments on commit 94cafa1

Please sign in to comment.