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 project. It also
has different options to list projects.

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 15, 2023
1 parent 0957d83 commit 58a1714
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cmd/project/list_project.go
@@ -0,0 +1,63 @@
package project

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/project"
"github.com/spf13/cobra"
)

type listProjectOptions struct {
name string
owner string
page int64
pageSize int64
public bool
q string
sort string
withDetail bool
}

// NewListProjectCommand creates a new `harbor list project` command
func NewListProjectCommand() *cobra.Command {
var opts listProjectOptions

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

flags := cmd.Flags()
flags.StringVarP(&opts.name, "name", "", "", "Name of the project")
flags.StringVarP(&opts.owner, "owner", "", "", "Name of the project owner")
flags.Int64VarP(&opts.page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.pageSize, "page-size", "", 10, "Size of per page")
flags.BoolVarP(&opts.public, "public", "", true, "Project is public or private")
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")
flags.BoolVarP(&opts.withDetail, "with-detail", "", true, "Bool value indicating whether return detailed information of the project")

return cmd
}

func runListProject(opts listProjectOptions, credentialName string) error {
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Name: &opts.name, Owner: &opts.owner, Page: &opts.page, PageSize: &opts.pageSize, Public: &opts.public, Q: &opts.q, Sort: &opts.sort, WithDetail: &opts.withDetail})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response)
return nil
}
14 changes: 14 additions & 0 deletions cmd/root.go
Expand Up @@ -22,9 +22,23 @@ func newGetCommand() *cobra.Command {
return cmd
}

// newListCommand creates a new `harbor list` command
func newListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list [COMMAND]",
Short: "list project, registry, etc.",
Long: `List project, registry`,
}

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

func addCommands(cmd *cobra.Command) {
cmd.AddCommand(login.NewLoginCommand())
cmd.AddCommand(newGetCommand())
cmd.AddCommand(newListCommand())
}

// CreateHarborCLI creates a new Harbor CLI
Expand Down

0 comments on commit 58a1714

Please sign in to comment.