Skip to content

Commit 1c255f9

Browse files
committed
feat: modifying usergrp list command
Signed-off-by: NucleoFusion <[email protected]>
1 parent cfa4b56 commit 1c255f9

File tree

2 files changed

+83
-54
lines changed

2 files changed

+83
-54
lines changed

cmd/harbor/root/usergroup/list.go

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
package usergroup
22

33
import (
4-
"github.com/goharbor/harbor-cli/pkg/api"
5-
log "github.com/sirupsen/logrus"
6-
"github.com/spf13/cobra"
4+
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
5+
"github.com/goharbor/harbor-cli/pkg/api"
6+
"github.com/spf13/cobra"
77

8-
list "github.com/goharbor/harbor-cli/pkg/views/usergroup/list"
8+
list "github.com/goharbor/harbor-cli/pkg/views/usergroup/list"
99
)
1010

1111
func UserGroupsListCommand() *cobra.Command {
12-
cmd := &cobra.Command{
13-
Use: "list",
14-
Short: "list user groups",
15-
Args: cobra.NoArgs,
16-
Run: func(cmd *cobra.Command, args []string) {
17-
userGroups, err := api.ListUserGroups()
18-
if err != nil {
19-
log.Errorf("failed to list user groups: %v", err)
20-
return
21-
}
22-
23-
list.ListUserGroups(userGroups)
24-
},
25-
}
26-
27-
return cmd
12+
var (
13+
searchq string
14+
searchID int64
15+
)
16+
17+
cmd := &cobra.Command{
18+
Use: "list",
19+
Short: "list user groups",
20+
Args: cobra.NoArgs,
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
var userGroups []*models.UserGroup
23+
24+
if searchID != -1 {
25+
resp, err := api.GetUserGroup(searchID)
26+
if err != nil {
27+
return err
28+
}
29+
30+
userGroups = []*models.UserGroup{resp.Payload}
31+
} else if searchq != "" {
32+
_, err := api.SearchUserGroups(searchq)
33+
if err != nil {
34+
return err
35+
}
36+
37+
// userGroups = []*models.UserGroup{&resp.Payload}
38+
} else {
39+
resp, err := api.ListUserGroups()
40+
if err != nil {
41+
return err
42+
}
43+
44+
userGroups = resp.Payload
45+
}
46+
47+
list.ListUserGroups(userGroups)
48+
return nil
49+
},
50+
}
51+
52+
flags := cmd.Flags()
53+
flags.StringVarP(&searchq, "search", "-s", "", "use to search for a specific groupname")
54+
flags.Int64VarP(&searchID, "id", "-i", -1, "use to search for a specific groupid")
55+
56+
return cmd
2857
}

pkg/views/usergroup/list/view.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
package list
22

33
import (
4-
"fmt"
5-
"os"
6-
"strconv"
4+
"fmt"
5+
"os"
6+
"strconv"
77

8-
"github.com/charmbracelet/bubbles/table"
9-
tea "github.com/charmbracelet/bubbletea"
10-
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/usergroup"
11-
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
8+
"github.com/charmbracelet/bubbles/table"
9+
tea "github.com/charmbracelet/bubbletea"
10+
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
11+
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
1212
)
1313

1414
var columns = []table.Column{
15-
{Title: "ID", Width: 10},
16-
{Title: "Group Name", Width: 30},
17-
{Title: "Group Type", Width: 15},
15+
{Title: "ID", Width: 10},
16+
{Title: "Group Name", Width: 30},
17+
{Title: "Group Type", Width: 15},
1818
}
1919

20-
func ListUserGroups(resp *usergroup.ListUserGroupsOK) {
21-
var rows []table.Row
22-
for _, group := range resp.Payload {
23-
groupType := "Unknown"
24-
switch group.GroupType {
25-
case 1:
26-
groupType = "LDAP"
27-
case 2:
28-
groupType = "HTTP"
29-
case 3:
30-
groupType = "OIDC"
31-
}
20+
func ListUserGroups(grps []*models.UserGroup) {
21+
var rows []table.Row
22+
for _, group := range grps {
23+
groupType := "Unknown"
24+
switch group.GroupType {
25+
case 1:
26+
groupType = "LDAP"
27+
case 2:
28+
groupType = "HTTP"
29+
case 3:
30+
groupType = "OIDC"
31+
}
3232

33-
rows = append(rows, table.Row{
34-
strconv.Itoa(int(group.ID)),
35-
group.GroupName,
36-
groupType,
37-
})
38-
}
33+
rows = append(rows, table.Row{
34+
strconv.Itoa(int(group.ID)),
35+
group.GroupName,
36+
groupType,
37+
})
38+
}
3939

40-
m := tablelist.NewModel(columns, rows, len(rows))
41-
if _, err := tea.NewProgram(m).Run(); err != nil {
42-
fmt.Println("Error running program:", err)
43-
os.Exit(1)
44-
}
45-
}
40+
m := tablelist.NewModel(columns, rows, len(rows))
41+
if _, err := tea.NewProgram(m).Run(); err != nil {
42+
fmt.Println("Error running program:", err)
43+
os.Exit(1)
44+
}
45+
}

0 commit comments

Comments
 (0)