Skip to content

Commit

Permalink
feat: add users 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 Mar 27, 2024
1 parent aab05ad commit 89380b0
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/internal/version"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/pkg/constants"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -110,6 +111,7 @@ func New() *cobra.Command {
newCreateCommand(),
newDeleteCommand(),
newUpdateCommand(),
user.UserCmd(),
)
return cmd
}
83 changes: 83 additions & 0 deletions cmd/harbor/root/user/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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/constants"
"github.com/goharbor/harbor-cli/pkg/constants"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
)

// {
// "email": "string",
// "realname": "string",
// "comment": "string",
// "password": "string",
// "username": "string"
// }

type createUserOptions struct {
email string
realname string
comment string
password string
username string
}

func UserCreateCmd() *cobra.Command {
var opts createUserOptions

cmd := &cobra.Command{
Use: "create",
Short: "create user",
// Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)

if err != nil {
return err
}

return runCreateUser(credentialName, opts)
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.email, "email", "", "", "Email")
flags.StringVarP(&opts.realname, "realname", "", "", "Realname")
flags.StringVarP(&opts.comment, "comment", "", "", "Comment")
flags.StringVarP(&opts.password, "password", "", "", "Password")
flags.StringVarP(&opts.username, "username", "", "", "Username")

return cmd
}

func runCreateUser(credentialName string, opts createUserOptions) error {

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
}

utils.PrintPayloadInJSONFormat(response)
return nil
}
36 changes: 36 additions & 0 deletions cmd/harbor/root/user/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package user

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
// "github.com/goharbor/harbor-cli/pkg/constants"
"github.com/goharbor/harbor-cli/pkg/constants"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
)

func UserListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list users",
Args: cobra.NoArgs,
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
credentialName, _ := cmd.Flags().GetString(constants.CredentialNameOption)
runListUsers(credentialName)

},
}

return cmd

}

func runListUsers(credentialName string) {
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, _ := client.User.ListUsers(ctx, &user.ListUsersParams{})

utils.PrintPayloadInJSONFormat(response)
}
16 changes: 16 additions & 0 deletions cmd/harbor/root/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package user

import "github.com/spf13/cobra"

func UserCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "user",
Short: "manage users",
}

cmd.AddCommand(UserListCmd())
cmd.AddCommand(UserCreateCmd())

return cmd
}

0 comments on commit 89380b0

Please sign in to comment.