Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add users command #34

Merged
merged 9 commits into from
May 17, 2024
Merged
6 changes: 4 additions & 2 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -89,17 +90,18 @@ harbor help

cobra.OnInitialize(initConfig)

root.PersistentFlags().StringVarP(&output, "output", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)")
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

viper.BindPFlag("output", root.PersistentFlags().Lookup("output"))
viper.BindPFlag("output-format", root.PersistentFlags().Lookup("output-format"))

root.AddCommand(
versionCommand(),
LoginCommand(),
project.Project(),
registry.Registry(),
user.User(),
)

return root
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func ListProjectCommand() *cobra.Command {
if err != nil {
log.Fatalf("failed to get projects list: %v", err)
}
FormatFlag := viper.GetString("output")
if FormatFlag != "json" {
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(projects)
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/project/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func LogsProjectCommmand() *cobra.Command {
}
auditLog.LogsProject(resp.Payload)

FormatFlag := viper.GetString("output")
if FormatFlag != "json" {
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(resp)
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/registry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func ListRegistryCommand() *cobra.Command {
if err != nil {
log.Fatalf("failed to get projects list: %v", err)
}
FormatFlag := viper.GetString("output")
if FormatFlag != "json" {
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(registry)
return
}
Expand Down
23 changes: 23 additions & 0 deletions cmd/harbor/root/user/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package user

import (
"github.com/spf13/cobra"
)

func User() *cobra.Command {
cmd := &cobra.Command{
Use: "user",
Short: "Manage users",
Long: `Manage users in Harbor`,
Example: ` harbor user list`,
}

cmd.AddCommand(
UserListCmd(),
UserCreateCmd(),
UserDeleteCmd(),
ElevateUserCmd(),
)

return cmd
}
91 changes: 91 additions & 0 deletions cmd/harbor/root/user/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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"
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 {
var opts create.CreateView

cmd := &cobra.Command{
Use: "create",
Short: "create user",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error
createView := &create.CreateView{
Email: opts.Email,
Realname: opts.Realname,
Comment: opts.Comment,
Password: opts.Password,
Username: opts.Username,
}

if opts.Email != "" && opts.Realname != "" && opts.Comment != "" && opts.Password != "" && opts.Username != "" {
err = runCreateUser(opts)
} else {
err = createUserView(createView)
}

if err != nil {
log.Errorf("failed to create user: %v", err)
}

},
}

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 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 nil
}
51 changes: 51 additions & 0 deletions cmd/harbor/root/user/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package user

import (
"context"
"strconv"

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

func UserDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "delete user",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
userId, _ := strconv.ParseInt(args[0], 10, 64)
err = runDeleteUser(userId)

} else {
userId := utils.GetUserIdFromUser()
err = runDeleteUser(userId)
}

if err != nil {
log.Errorf("failed to delete user: %v", err)
}

},
}

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
}
58 changes: 58 additions & 0 deletions cmd/harbor/root/user/elevate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ElevateUserCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "elevate",
Short: "elevate user",
Long: "elevate user to admin role",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var userId int64
if len(args) > 0 {
userId, _ = strconv.ParseInt(args[0], 10, 64)

} else {
userId = utils.GetUserIdFromUser()
}

// Todo : Ask for the confirmation before elevating the user to admin role

err = runElevateUser(userId)

if err != nil {
log.Errorf("failed to elevate user: %v", err)
}

},
}

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
}
41 changes: 41 additions & 0 deletions cmd/harbor/root/user/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package user

import (
"context"

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

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) {
response := runListUsers()
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(response.Payload)
} else {
list.ListUsers(response.Payload)
}
},
}

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
}
20 changes: 20 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
pview "github.com/goharbor/harbor-cli/pkg/views/project/select"
rview "github.com/goharbor/harbor-cli/pkg/views/registry/select"
uview "github.com/goharbor/harbor-cli/pkg/views/user/select"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -88,3 +90,21 @@ func GetProjectNameFromUser() string {

return <-projectName
}

func GetUserIdFromUser() int64 {
userId := make(chan int64)

go func() {
credentialName := viper.GetString("current-credential-name")
client := GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.User.ListUsers(ctx, &user.ListUsersParams{})
if err != nil {
log.Fatal(err)
}
uview.UserList(response.Payload, userId)
}()

return <-userId

}
6 changes: 2 additions & 4 deletions pkg/views/project/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)

type model struct {
table table.Model
}
Expand All @@ -40,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
return views.BaseStyle.Render(m.table.View()) + "\n"
}

func ListProjects(projects []*models.Project) {
Expand Down
Loading