Skip to content

Commit ee04f28

Browse files
committed
Merge remote-tracking branch 'origin/main' into client-team-domain
2 parents fbfddbd + b5155e6 commit ee04f28

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

cmd/list-teams.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/codesphere-cloud/cs-go/pkg/out"
10+
"github.com/jedib0t/go-pretty/v6/table"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
type ListTeamsCmd struct {
16+
cmd *cobra.Command
17+
opts GlobalOptions
18+
}
19+
20+
func addListTeamsCmd(p *cobra.Command, opts GlobalOptions) {
21+
l := ListTeamsCmd{
22+
cmd: &cobra.Command{
23+
Use: "teams",
24+
Short: "list teams",
25+
Long: `list teams available in Codesphere`,
26+
Example: `
27+
List all teams:
28+
29+
$ cs list teams
30+
`,
31+
},
32+
opts: opts,
33+
}
34+
l.cmd.RunE = l.RunE
35+
p.AddCommand(l.cmd)
36+
}
37+
38+
func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
39+
client, err := NewClient(l.opts)
40+
if err != nil {
41+
return fmt.Errorf("failed to create Codesphere client: %e", err)
42+
}
43+
44+
teams, err := client.ListTeams()
45+
if err != nil {
46+
return fmt.Errorf("failed to list teams: %e", err)
47+
}
48+
49+
t := out.GetTableWriter()
50+
t.AppendHeader(table.Row{"P", "ID", "Name", "Role", "Default DC"})
51+
for _, team := range teams {
52+
first := ""
53+
if team.IsFirst != nil && *team.IsFirst {
54+
first = "*"
55+
}
56+
t.AppendRow(table.Row{first, team.Id, team.Name, GetRoleName(int(team.Role)), team.DefaultDataCenterId})
57+
}
58+
t.Render()
59+
60+
return nil
61+
}

cmd/list-workspaces.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Copyright © 2025 Codesphere Inc. <support@codesphere.com>
44
package cmd
55

66
import (
7-
"errors"
87
"fmt"
98

9+
"github.com/codesphere-cloud/cs-go/pkg/api"
1010
"github.com/codesphere-cloud/cs-go/pkg/out"
1111
"github.com/jedib0t/go-pretty/v6/table"
1212

@@ -47,26 +47,50 @@ func (l *ListWorkspacesCmd) parseLogCmdFlags() {
4747
}
4848

4949
func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
50-
if l.opts.TeamId == nil || *l.opts.TeamId < 0 {
51-
return errors.New("team ID not set or invalid, please use --team-id to set one")
52-
}
53-
5450
client, err := NewClient(l.opts.GlobalOptions)
5551
if err != nil {
5652
return fmt.Errorf("failed to create Codesphere client: %e", err)
5753
}
5854

59-
workspaces, err := client.ListWorkspaces(*l.opts.TeamId)
55+
teams, err := l.getTeamIds(client)
6056
if err != nil {
61-
return fmt.Errorf("failed to list workspaces: %e", err)
57+
return fmt.Errorf("failed to get teams: %e", err)
58+
}
59+
workspaces := []api.Workspace{}
60+
for _, team := range teams {
61+
teamWorkspaces, err := client.ListWorkspaces(team)
62+
if err != nil {
63+
return fmt.Errorf("failed to list workspaces: %e", err)
64+
}
65+
workspaces = append(workspaces, teamWorkspaces...)
6266
}
6367

6468
t := out.GetTableWriter()
65-
t.AppendHeader(table.Row{"ID", "Name", "Repository"})
69+
t.AppendHeader(table.Row{"Team ID", "ID", "Name", "Repository"})
6670
for _, w := range workspaces {
67-
t.AppendRow(table.Row{w.Id, w.Name, *w.GitUrl.Get()})
71+
gitUrl := ""
72+
if w.GitUrl.Get() != nil {
73+
gitUrl = *w.GitUrl.Get()
74+
}
75+
t.AppendRow(table.Row{w.TeamId, w.Id, w.Name, gitUrl})
6876
}
6977
t.Render()
7078

7179
return nil
7280
}
81+
82+
func (l *ListWorkspacesCmd) getTeamIds(client *api.Client) (teams []int, err error) {
83+
if l.opts.TeamId != nil && *l.opts.TeamId >= 0 {
84+
teams = append(teams, *l.opts.TeamId)
85+
return
86+
}
87+
var allTeams []api.Team
88+
allTeams, err = client.ListTeams()
89+
if err != nil {
90+
return
91+
}
92+
for _, t := range allTeams {
93+
teams = append(teams, int(t.Id))
94+
}
95+
return
96+
}

cmd/list.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ func addListCmd(rootCmd *cobra.Command, opts GlobalOptions) {
2424
`,
2525
},
2626
}
27-
l.parseLogCmdFlags()
2827
rootCmd.AddCommand(l.cmd)
2928
addListWorkspacesCmd(l.cmd, opts)
30-
}
31-
32-
func (l *ListCmd) parseLogCmdFlags() {
33-
29+
addListTeamsCmd(l.cmd, opts)
3430
}

cmd/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,10 @@ func NewClient(opts GlobalOptions) (*api.Client, error) {
104104
})
105105
return client, nil
106106
}
107+
108+
func GetRoleName(role int) string {
109+
if role == 1 {
110+
return "Member"
111+
}
112+
return "Admin"
113+
}

0 commit comments

Comments
 (0)