From 0d785e74b87d817134dc8808ecfa6440c73b0f78 Mon Sep 17 00:00:00 2001 From: amands98 Date: Sun, 26 May 2024 15:53:02 +0530 Subject: [PATCH] feat: add handler for user and repos command Signed-off-by: amands98 --- cmd/harbor/root/registry/view.go | 9 +-- cmd/harbor/root/repository/delete.go | 24 +------- cmd/harbor/root/repository/info.go | 24 +------- cmd/harbor/root/repository/list.go | 26 +-------- cmd/harbor/root/user/create.go | 42 +------------- cmd/harbor/root/user/delete.go | 20 +------ cmd/harbor/root/user/elevate.go | 22 +------- cmd/harbor/root/user/list.go | 20 +++---- pkg/api/repository_handler.go | 55 +++++++++++++++++++ pkg/api/user_handler.go | 82 ++++++++++++++++++++++++++++ 10 files changed, 163 insertions(+), 161 deletions(-) diff --git a/cmd/harbor/root/registry/view.go b/cmd/harbor/root/registry/view.go index bf44b991..8540e3d6 100644 --- a/cmd/harbor/root/registry/view.go +++ b/cmd/harbor/root/registry/view.go @@ -17,20 +17,15 @@ func ViewCommand() *cobra.Command { Short: "get registry by id", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - var err error if len(args) > 0 { registryId, err := strconv.ParseInt(args[0], 10, 64) if err != nil { log.Errorf("failed to parse registry id: %v", err) } - err = api.GetRegistry(registryId) + api.GetRegistry(registryId) } else { registryId := utils.GetRegistryNameFromUser() - err = api.GetRegistry(registryId) - } - - if err != nil { - log.Errorf("failed to get registry: %v", err) + api.GetRegistry(registryId) } }, } diff --git a/cmd/harbor/root/repository/delete.go b/cmd/harbor/root/repository/delete.go index d78b7d33..0d83fb9b 100644 --- a/cmd/harbor/root/repository/delete.go +++ b/cmd/harbor/root/repository/delete.go @@ -1,13 +1,10 @@ package repository import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func RepoDeleteCmd() *cobra.Command { @@ -20,11 +17,11 @@ func RepoDeleteCmd() *cobra.Command { var err error if len(args) > 0 { projectName, repoName := utils.ParseProjectRepo(args[0]) - err = runRepoDelete(projectName, repoName) + err = api.RepoDelete(projectName, repoName) } else { projectName := utils.GetProjectNameFromUser() repoName := utils.GetRepoNameFromUser(projectName) - err = runRepoDelete(projectName, repoName) + err = api.RepoDelete(projectName, repoName) } if err != nil { log.Errorf("failed to delete repository: %v", err) @@ -33,18 +30,3 @@ func RepoDeleteCmd() *cobra.Command { } return cmd } - -func runRepoDelete(projectName, repoName string) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - - _, err := client.Repository.DeleteRepository(ctx, &repository.DeleteRepositoryParams{ProjectName: projectName, RepositoryName: repoName}) - - if err != nil { - return err - } - - log.Infof("Repository %s/%s deleted successfully", projectName, repoName) - return nil -} diff --git a/cmd/harbor/root/repository/info.go b/cmd/harbor/root/repository/info.go index b9752ec5..9a365196 100644 --- a/cmd/harbor/root/repository/info.go +++ b/cmd/harbor/root/repository/info.go @@ -1,13 +1,10 @@ package repository import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func RepoInfoCmd() *cobra.Command { @@ -20,11 +17,11 @@ func RepoInfoCmd() *cobra.Command { var err error if len(args) > 0 { projectName, repoName := utils.ParseProjectRepo(args[0]) - err = runRepoInfo(projectName, repoName) + err = api.RepoInfo(projectName, repoName) } else { projectName := utils.GetProjectNameFromUser() repoName := utils.GetRepoNameFromUser(projectName) - err = runRepoInfo(projectName, repoName) + err = api.RepoInfo(projectName, repoName) } if err != nil { log.Errorf("failed to get repository information: %v", err) @@ -35,18 +32,3 @@ func RepoInfoCmd() *cobra.Command { return cmd } - -func runRepoInfo(projectName, repoName string) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - - response, err := client.Repository.GetRepository(ctx, &repository.GetRepositoryParams{ProjectName: projectName, RepositoryName: repoName}) - - if err != nil { - return err - } - - utils.PrintPayloadInJSONFormat(response.Payload) - return nil -} diff --git a/cmd/harbor/root/repository/list.go b/cmd/harbor/root/repository/list.go index 42dc61b1..428cab0f 100644 --- a/cmd/harbor/root/repository/list.go +++ b/cmd/harbor/root/repository/list.go @@ -1,14 +1,10 @@ package repository import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" - "github.com/goharbor/harbor-cli/pkg/views/repository/list" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func ListRepositoryCommand() *cobra.Command { @@ -20,10 +16,10 @@ func ListRepositoryCommand() *cobra.Command { var err error if len(args) > 0 { - err = runListRepository(args[0]) + err = api.ListRepository(args[0]) } else { projectName := utils.GetProjectNameFromUser() - err = runListRepository(projectName) + err = api.ListRepository(projectName) } if err != nil { log.Errorf("failed to list repositories: %v", err) @@ -33,19 +29,3 @@ func ListRepositoryCommand() *cobra.Command { return cmd } - -func runListRepository(ProjectName string) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - - response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: ProjectName}) - - if err != nil { - return err - } - - list.ListRepositories(response.Payload) - return nil - -} diff --git a/cmd/harbor/root/user/create.go b/cmd/harbor/root/user/create.go index 69fde8d2..6b873cad 100644 --- a/cmd/harbor/root/user/create.go +++ b/cmd/harbor/root/user/create.go @@ -1,19 +1,11 @@ package user import ( - // "context" - - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" - "github.com/goharbor/go-client/pkg/sdk/v2.0/models" - - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/api" log "github.com/sirupsen/logrus" "github.com/goharbor/harbor-cli/pkg/views/user/create" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func UserCreateCmd() *cobra.Command { @@ -34,7 +26,7 @@ func UserCreateCmd() *cobra.Command { } if opts.Email != "" && opts.Realname != "" && opts.Comment != "" && opts.Password != "" && opts.Username != "" { - err = runCreateUser(opts) + err = api.CreateUser(opts) } else { err = createUserView(createView) } @@ -58,34 +50,6 @@ func UserCreateCmd() *cobra.Command { func createUserView(createView *create.CreateView) error { create.CreateUserView(createView) - return runCreateUser(*createView) - -} - -func runCreateUser(opts create.CreateView) error { - credentialName := viper.GetString("current-credential-name") - - client := utils.GetClientByCredentialName(credentialName) - - ctx := context.Background() - - response, err := client.User.CreateUser(ctx, &user.CreateUserParams{ - UserReq: &models.UserCreationReq{ - Email: opts.Email, - Realname: opts.Realname, - Comment: opts.Comment, - Password: opts.Password, - Username: opts.Username, - }, - }) - - if err != nil { - return err - } - - if response != nil { - log.Info("User created successfully") - } + return api.CreateUser(*createView) - return nil } diff --git a/cmd/harbor/root/user/delete.go b/cmd/harbor/root/user/delete.go index 42dd7bd5..5b287ba1 100644 --- a/cmd/harbor/root/user/delete.go +++ b/cmd/harbor/root/user/delete.go @@ -1,14 +1,12 @@ package user import ( - "context" "strconv" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func UserDeleteCmd() *cobra.Command { @@ -20,11 +18,11 @@ func UserDeleteCmd() *cobra.Command { var err error if len(args) > 0 { userId, _ := strconv.ParseInt(args[0], 10, 64) - err = runDeleteUser(userId) + err = api.DeleteUser(userId) } else { userId := utils.GetUserIdFromUser() - err = runDeleteUser(userId) + err = api.DeleteUser(userId) } if err != nil { @@ -37,15 +35,3 @@ func UserDeleteCmd() *cobra.Command { return cmd } - -func runDeleteUser(userId int64) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - _, err := client.User.DeleteUser(ctx, &user.DeleteUserParams{UserID: userId}) - if err != nil { - return err - } - log.Info("user deleted successfully") - return nil -} diff --git a/cmd/harbor/root/user/elevate.go b/cmd/harbor/root/user/elevate.go index 760fb3c9..9e7e495b 100644 --- a/cmd/harbor/root/user/elevate.go +++ b/cmd/harbor/root/user/elevate.go @@ -1,15 +1,12 @@ package user import ( - "context" "strconv" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" - "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func ElevateUserCmd() *cobra.Command { @@ -30,7 +27,7 @@ func ElevateUserCmd() *cobra.Command { // Todo : Ask for the confirmation before elevating the user to admin role - err = runElevateUser(userId) + err = api.ElevateUser(userId) if err != nil { log.Errorf("failed to elevate user: %v", err) @@ -41,18 +38,3 @@ func ElevateUserCmd() *cobra.Command { return cmd } - -func runElevateUser(userId int64) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - UserSysAdminFlag := &models.UserSysAdminFlag{ - SysadminFlag: true, - } - _, err := client.User.SetUserSysAdmin(ctx, &user.SetUserSysAdminParams{UserID: userId, SysadminFlag: UserSysAdminFlag}) - if err != nil { - return err - } - log.Info("user elevated role to admin successfully") - return nil -} diff --git a/cmd/harbor/root/user/list.go b/cmd/harbor/root/user/list.go index 539c48aa..b9f356e9 100644 --- a/cmd/harbor/root/user/list.go +++ b/cmd/harbor/root/user/list.go @@ -1,11 +1,10 @@ package user import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" "github.com/goharbor/harbor-cli/pkg/views/user/list" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -17,7 +16,11 @@ func UserListCmd() *cobra.Command { Args: cobra.NoArgs, Aliases: []string{"ls"}, Run: func(cmd *cobra.Command, args []string) { - response := runListUsers() + response, err := api.ListUsers() + if err != nil { + log.Errorf("failed to list users: %v", err) + return + } FormatFlag := viper.GetString("output-format") if FormatFlag != "" { utils.PrintPayloadInJSONFormat(response.Payload) @@ -30,12 +33,3 @@ func UserListCmd() *cobra.Command { return cmd } - -func runListUsers() *user.ListUsersOK { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, _ := client.User.ListUsers(ctx, &user.ListUsersParams{}) - - return response -} diff --git a/pkg/api/repository_handler.go b/pkg/api/repository_handler.go index 778f64ec..02dfa81c 100644 --- a/pkg/api/repository_handler.go +++ b/pkg/api/repository_handler.go @@ -1 +1,56 @@ package api + +import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/repository/list" + log "github.com/sirupsen/logrus" +) + +func RepoDelete(projectName, repoName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + _, err = client.Repository.DeleteRepository(ctx, &repository.DeleteRepositoryParams{ProjectName: projectName, RepositoryName: repoName}) + + if err != nil { + return err + } + + log.Infof("Repository %s/%s deleted successfully", projectName, repoName) + return nil +} + +func RepoInfo(projectName, repoName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.Repository.GetRepository(ctx, &repository.GetRepositoryParams{ProjectName: projectName, RepositoryName: repoName}) + + if err != nil { + return err + } + + utils.PrintPayloadInJSONFormat(response.Payload) + return nil +} + +func ListRepository(ProjectName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: ProjectName}) + + if err != nil { + return err + } + + list.ListRepositories(response.Payload) + return nil + +} diff --git a/pkg/api/user_handler.go b/pkg/api/user_handler.go index 778f64ec..4b599420 100644 --- a/pkg/api/user_handler.go +++ b/pkg/api/user_handler.go @@ -1 +1,83 @@ package api + +import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/user/create" + + log "github.com/sirupsen/logrus" +) + +func CreateUser(opts create.CreateView) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.User.CreateUser(ctx, &user.CreateUserParams{ + UserReq: &models.UserCreationReq{ + Email: opts.Email, + Realname: opts.Realname, + Comment: opts.Comment, + Password: opts.Password, + Username: opts.Username, + }, + }) + + if err != nil { + return err + } + + if response != nil { + log.Infof("User `%s` created successfully", opts.Username) + } + + return nil +} + +func DeleteUser(userId int64) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.User.DeleteUser(ctx, &user.DeleteUserParams{UserID: userId}) + if err != nil { + return err + } + log.Infof("User deleted successfully with id %d", userId) + return nil +} + +func ElevateUser(userId int64) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + UserSysAdminFlag := &models.UserSysAdminFlag{ + SysadminFlag: true, + } + _, err = client.User.SetUserSysAdmin(ctx, &user.SetUserSysAdminParams{UserID: userId, SysadminFlag: UserSysAdminFlag}) + if err != nil { + return err + } + log.Infof("user elevated role to admin successfully with id %d", userId) + return nil +} + +func ListUsers() (*user.ListUsersOK, error) { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return nil, err + } + + response, err := client.User.ListUsers(ctx, &user.ListUsersParams{}) + + if err != nil { + return nil, err + } + + return response, nil +}