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

Implement UserGroup Command in CLI #165

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"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/usergroup"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/pkg/utils"
Expand Down Expand Up @@ -109,6 +110,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
usergroup.Usergroup(),
)

return root
Expand Down
26 changes: 26 additions & 0 deletions cmd/harbor/root/usergroup/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package usergroup

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

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

cmd.AddCommand(
UserGroupCreateCommand(),
UserGroupsListCommand(),
UserGroupDeleteCommand(),
UserGroupsSearchCommand(),
UserGroupUpdateCommand(),
UserGroupGetCommand(),

)

return cmd
}
99 changes: 99 additions & 0 deletions cmd/harbor/root/usergroup/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package usergroup

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/spf13/cobra"
)

type ErrorResponse struct {
Errors []struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"errors"`
}

func UserGroupCreateCommand() *cobra.Command {
var groupName string
var groupType int64
var ldapGroupDn string

cmd := &cobra.Command{
Use: "create",
Short: "create user group",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if groupName == "" {
fmt.Print("Enter group name: ")
fmt.Scanln(&groupName)
}

for {
if groupType == 0 {
fmt.Print("Enter group type (1 for LDAP, 2 for HTTP, 3 for OIDC group): ")
var input string
fmt.Scanln(&input)
var err error
groupType, err = strconv.ParseInt(input, 10, 64)
if err != nil {
fmt.Println("Invalid input, please enter an integer.")
groupType = 0
continue
}
}

if groupType < 1 || groupType > 3 {
fmt.Println("Invalid group type. Must be 1 (LDAP), 2 (HTTP), or 3 (OIDC).")
groupType = 0
continue
}

if groupType == 1 {
fmt.Print("Enter the DN of the LDAP group: ")
fmt.Scanln(&ldapGroupDn)
}

break
}

var ldapInfo string
if groupType == 1 {
ldapInfo = fmt.Sprintf(", LDAP DN: %s", ldapGroupDn)
}

fmt.Printf("Creating user group with name: %s, type: %d%s\n", groupName, groupType, ldapInfo)
err := api.CreateUserGroup(groupName, groupType, ldapGroupDn)
if err != nil {
return formatError(err)
}

fmt.Printf("User group '%s' created successfully\n", groupName)
return nil
},
}

flags := cmd.Flags()
flags.StringVarP(&groupName, "name", "n", "", "Group name")
flags.Int64VarP(&groupType, "type", "t", 0, "Group type")
flags.StringVarP(&ldapGroupDn, "ldap-dn", "l", "", "The DN of the LDAP group if group type is 1 (LDAP group)")

return cmd
}

func formatError(err error) error {
errStr := err.Error()
if strings.Contains(errStr, "conflict:") {
var errResp ErrorResponse
jsonStr := strings.TrimPrefix(errStr, "conflict: ")
if err := json.Unmarshal([]byte(jsonStr), &errResp); err == nil {
if len(errResp.Errors) > 0 {
return fmt.Errorf("%s", errResp.Errors[0].Message)
}
}
}
return fmt.Errorf("failed to create user group: %v", err)
}
70 changes: 70 additions & 0 deletions cmd/harbor/root/usergroup/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package usergroup

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"

"github.com/goharbor/harbor-cli/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UserGroupDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [groupID]",
Short: "delete user group",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var groupId int64
var err error

if len(args) == 0 {
fmt.Print("Enter group ID: ")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
groupId, err = strconv.ParseInt(input, 10, 64)
if err != nil {
log.Errorf("invalid group ID: %v", err)
return
}
} else {
groupId, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Errorf("invalid group ID: %v", err)
return
}
}
response, err := api.ListUserGroups()
if err != nil {
log.Errorf("failed to list user groups: %v", err)
return
}

groupExists := false
for _, group := range response.Payload {
if group.ID == groupId {
groupExists = true
break
}
}

if !groupExists {
log.Errorf("group ID %d not found", groupId)
return
}

err = api.DeleteUserGroup(groupId)
if err != nil {
log.Errorf("failed to delete user group: %v", err)
}
fmt.Print("\033[K")

},
}

return cmd
}
58 changes: 58 additions & 0 deletions cmd/harbor/root/usergroup/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package usergroup

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"

"github.com/goharbor/harbor-cli/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
get "github.com/goharbor/harbor-cli/pkg/views/usergroup/get"
)

func UserGroupGetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get [groupID]",
Short: "get user group details",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var groupId int64
var err error

if len(args) == 0 {
fmt.Print("Enter group ID: ")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
groupId, err = strconv.ParseInt(input, 10, 64)
if err != nil {
log.Errorf("invalid group ID: %v", err)
return
}
} else {
groupId, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Errorf("invalid group ID: %v", err)
return
}
}

response, err := api.GetUserGroup(groupId)
if err != nil {
if strings.Contains(err.Error(), "404") {
log.Errorf("user group not found with id %d", groupId)
} else {
log.Errorf("failed to get user group: %v", err)
}
return
}

get.DisplayUserGroup(response.Payload)
},
}

return cmd
}
28 changes: 28 additions & 0 deletions cmd/harbor/root/usergroup/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package usergroup

import (
"github.com/goharbor/harbor-cli/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

list "github.com/goharbor/harbor-cli/pkg/views/usergroup/list"
)

func UserGroupsListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list user groups",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
userGroups, err := api.ListUserGroups()
if err != nil {
log.Errorf("failed to list user groups: %v", err)
return
}

list.ListUserGroups(userGroups)
},
}

return cmd
}
57 changes: 57 additions & 0 deletions cmd/harbor/root/usergroup/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package usergroup

import (
"bufio"
"fmt"
"os"
"strings"

search "github.com/goharbor/harbor-cli/pkg/views/usergroup/search"
"github.com/goharbor/harbor-cli/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UserGroupsSearchCommand() *cobra.Command {
var groupName string

cmd := &cobra.Command{
Use: "search [groupName]",
Short: "search user groups",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
groupName = args[0]
}

if groupName == "" {
fmt.Print("Enter group name: ")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
groupName = strings.TrimSpace(input)
}
fmt.Print("\033[K")

fmt.Printf("Searching for groups with name '%s'...\r", groupName)
response, err := api.SearchUserGroups(groupName)
if err != nil {
log.Errorf("failed to search user groups: %v", err)
return
}


if len(response.Payload) == 0 {
log.Infof("No user groups found with the name %s", groupName)
return
}

search.DisplayUserGroupSearchResults(response)

},
}

flags := cmd.Flags()
flags.StringVarP(&groupName, "name", "n", "", "Group name to search")

return cmd
}
36 changes: 36 additions & 0 deletions cmd/harbor/root/usergroup/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package usergroup

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/views/usergroup/update"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UserGroupUpdateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "update user group",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
userGroupsResp, err := api.ListUserGroups()
if err != nil {
log.Errorf("failed to list user groups: %v", err)
return
}
input, err := update.UpdateUserGroupView(userGroupsResp)
if err != nil {
log.Errorf("failed to get user input: %v", err)
return
}
err = api.UpdateUserGroup(input.GroupID, input.GroupName, input.GroupType)
if err != nil {
log.Errorf("failed to update user group: %v", err)
} else {
log.Infof("User group `%s` updated successfully", input.GroupName)
}
},
}

return cmd
}
Loading