Skip to content

Commit

Permalink
Added flags for user list cmd (goharbor#238)
Browse files Browse the repository at this point in the history
* added flags for user list cmd

Signed-off-by: ALTHAF <[email protected]>

* changed cli flows to the id to name

Signed-off-by: ALTHAF <[email protected]>

* added flags for artifact list cmd

Signed-off-by: ALTHAF <[email protected]>

---------

Signed-off-by: ALTHAF <[email protected]>
  • Loading branch information
Althaf66 authored Nov 19, 2024
1 parent e2a8d98 commit 963aac0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
12 changes: 10 additions & 2 deletions cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

func ListArtifactCommand() *cobra.Command {
var opts api.ListFlags

cmd := &cobra.Command{
Use: "list",
Short: "list artifacts within a repository",
Expand All @@ -21,11 +23,11 @@ func ListArtifactCommand() *cobra.Command {

if len(args) > 0 {
projectName, repoName := utils.ParseProjectRepo(args[0])
resp, err = api.ListArtifact(projectName, repoName)
resp, err = api.ListArtifact(projectName, repoName, opts)
} else {
projectName := prompt.GetProjectNameFromUser()
repoName := prompt.GetRepoNameFromUser(projectName)
resp, err = api.ListArtifact(projectName, repoName)
resp, err = api.ListArtifact(projectName, repoName, opts)
}

if err != nil {
Expand All @@ -35,5 +37,11 @@ func ListArtifactCommand() *cobra.Command {
},
}

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

return cmd
}
6 changes: 2 additions & 4 deletions cmd/harbor/root/user/delete.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package user

import (
"strconv"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
log "github.com/sirupsen/logrus"
Expand All @@ -17,8 +15,8 @@ func UserDeleteCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
userId, _ := strconv.ParseInt(args[0], 10, 64)
err = api.DeleteUser(userId)
userName, _ := api.GetUsersIdByName(args[0])
err = api.DeleteUser(userName)

} else {
userId := prompt.GetUserIdFromUser()
Expand Down
4 changes: 1 addition & 3 deletions cmd/harbor/root/user/elevate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package user

import (
"strconv"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views"
Expand All @@ -20,7 +18,7 @@ func ElevateUserCmd() *cobra.Command {
var err error
var userId int64
if len(args) > 0 {
userId, _ = strconv.ParseInt(args[0], 10, 64)
userId, _ = api.GetUsersIdByName(args[0])

} else {
userId = prompt.GetUserIdFromUser()
Expand Down
10 changes: 9 additions & 1 deletion cmd/harbor/root/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
)

func UserListCmd() *cobra.Command {
var opts api.ListFlags

cmd := &cobra.Command{
Use: "list",
Short: "list users",
Args: cobra.NoArgs,
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
response, err := api.ListUsers()
response, err := api.ListUsers(opts)
if err != nil {
log.Errorf("failed to list users: %v", err)
return
Expand All @@ -30,6 +32,12 @@ func UserListCmd() *cobra.Command {
},
}

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

return cmd

}
10 changes: 9 additions & 1 deletion pkg/api/artifact_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,23 @@ func InfoArtifact(projectName, repoName, reference string) error {
}

// RunListArtifact lists all artifacts in a repository.
func ListArtifact(projectName, repoName string) (artifact.ListArtifactsOK, error) {
func ListArtifact(projectName, repoName string, opts ...ListFlags) (artifact.ListArtifactsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return artifact.ListArtifactsOK{}, err
}

var listFlags ListFlags
if len(opts) > 0 {
listFlags = opts[0]
}
response, err := client.Artifact.ListArtifacts(ctx, &artifact.ListArtifactsParams{
ProjectName: projectName,
RepositoryName: repoName,
Page: &listFlags.Page,
PageSize: &listFlags.PageSize,
Q: &listFlags.Q,
Sort: &listFlags.Sort,
})
if err != nil {
return artifact.ListArtifactsOK{}, err
Expand Down
31 changes: 28 additions & 3 deletions pkg/api/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,42 @@ func ElevateUser(userId int64) error {
return nil
}

func ListUsers() (*user.ListUsersOK, error) {
func ListUsers(opts ...ListFlags) (*user.ListUsersOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}
var listFlags ListFlags
if len(opts) > 0 {
listFlags = opts[0]
}

response, err := client.User.ListUsers(ctx, &user.ListUsersParams{})

response, err := client.User.ListUsers(ctx, &user.ListUsersParams{
Page: &listFlags.Page,
PageSize: &listFlags.PageSize,
Q: &listFlags.Q,
Sort: &listFlags.Sort,
})
if err != nil {
return nil, err
}

return response, nil
}

func GetUsersIdByName(userName string) (int64, error) {
var opts ListFlags

u, err := ListUsers(opts)
if err != nil {
return 0, err
}

for _, user := range u.Payload {
if user.Username == userName {
return user.UserID, nil
}
}

return 0, err
}

0 comments on commit 963aac0

Please sign in to comment.