-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |