Skip to content

Commit

Permalink
feat: add handler for user and repos command
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed May 26, 2024
1 parent 887d550 commit 0d785e7
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 161 deletions.
9 changes: 2 additions & 7 deletions cmd/harbor/root/registry/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
24 changes: 3 additions & 21 deletions cmd/harbor/root/repository/delete.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
24 changes: 3 additions & 21 deletions cmd/harbor/root/repository/info.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
26 changes: 3 additions & 23 deletions cmd/harbor/root/repository/list.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand All @@ -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

}
42 changes: 3 additions & 39 deletions cmd/harbor/root/user/create.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
}
20 changes: 3 additions & 17 deletions cmd/harbor/root/user/delete.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
22 changes: 2 additions & 20 deletions cmd/harbor/root/user/elevate.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
20 changes: 7 additions & 13 deletions cmd/harbor/root/user/list.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
Expand All @@ -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
}
Loading

0 comments on commit 0d785e7

Please sign in to comment.