diff --git a/cmd/harbor/root/artifact/list.go b/cmd/harbor/root/artifact/list.go index 22be9278..5c6fb33f 100644 --- a/cmd/harbor/root/artifact/list.go +++ b/cmd/harbor/root/artifact/list.go @@ -11,6 +11,8 @@ import ( ) func ListArtifactCommand() *cobra.Command { + var opts api.ListFlags + cmd := &cobra.Command{ Use: "list", Short: "list artifacts within a repository", @@ -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 { @@ -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 } diff --git a/cmd/harbor/root/user/delete.go b/cmd/harbor/root/user/delete.go index 1b5afc8a..43c24528 100644 --- a/cmd/harbor/root/user/delete.go +++ b/cmd/harbor/root/user/delete.go @@ -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" @@ -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() diff --git a/cmd/harbor/root/user/elevate.go b/cmd/harbor/root/user/elevate.go index 25cc7299..66a38567 100644 --- a/cmd/harbor/root/user/elevate.go +++ b/cmd/harbor/root/user/elevate.go @@ -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" @@ -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() diff --git a/cmd/harbor/root/user/list.go b/cmd/harbor/root/user/list.go index b9f356e9..d8a81aab 100644 --- a/cmd/harbor/root/user/list.go +++ b/cmd/harbor/root/user/list.go @@ -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 @@ -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 } diff --git a/pkg/api/artifact_handler.go b/pkg/api/artifact_handler.go index cdfc6764..a6fa1391 100644 --- a/pkg/api/artifact_handler.go +++ b/pkg/api/artifact_handler.go @@ -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 diff --git a/pkg/api/user_handler.go b/pkg/api/user_handler.go index 4b599420..b1e8a7d6 100644 --- a/pkg/api/user_handler.go +++ b/pkg/api/user_handler.go @@ -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 +}