diff --git a/cmd/harbor/root/cmd.go b/cmd/harbor/root/cmd.go index 2e3d4bc2..693a041a 100644 --- a/cmd/harbor/root/cmd.go +++ b/cmd/harbor/root/cmd.go @@ -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" ) @@ -110,6 +111,7 @@ func New() *cobra.Command { newCreateCommand(), newDeleteCommand(), newUpdateCommand(), + user.UserCmd(), ) return cmd } diff --git a/cmd/harbor/root/user/create.go b/cmd/harbor/root/user/create.go new file mode 100644 index 00000000..b60ba44e --- /dev/null +++ b/cmd/harbor/root/user/create.go @@ -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 +} diff --git a/cmd/harbor/root/user/list.go b/cmd/harbor/root/user/list.go new file mode 100644 index 00000000..616f4ccb --- /dev/null +++ b/cmd/harbor/root/user/list.go @@ -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) +} diff --git a/cmd/harbor/root/user/user.go b/cmd/harbor/root/user/user.go new file mode 100644 index 00000000..5924c344 --- /dev/null +++ b/cmd/harbor/root/user/user.go @@ -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 +}