Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
70 changes: 70 additions & 0 deletions cmd/list-teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
*/
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/out"
"github.com/jedib0t/go-pretty/v6/table"

"github.com/spf13/cobra"
)

type ListTeamsCmd struct {
cmd *cobra.Command
opts GlobalOptions
}

func addListTeamsCmd(p *cobra.Command, opts GlobalOptions) {
l := ListTeamsCmd{
cmd: &cobra.Command{
Use: "teams",
Short: "list teams",
Long: `list teams available in Codesphere`,
Example: `
List all teams:

$ cs list teams
`,
},
opts: opts,
}
l.cmd.RunE = l.RunE
l.parseLogCmdFlags()
p.AddCommand(l.cmd)
}

func (l *ListTeamsCmd) parseLogCmdFlags() {

}

func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
client, err := NewClient(l.opts)
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %e", err)
}

teams, err := client.ListTeams()
if err != nil {
return fmt.Errorf("failed to list teams: %e", err)
}

t := out.GetTableWriter()
t.AppendHeader(table.Row{"P", "ID", "Name", "Role", "Default DC"})
for _, team := range teams {
first := ""
if team.IsFirst != nil && *team.IsFirst {
first = "*"
}
role := "Admin"
if team.Role == 1 {
role = "Member"
}
t.AppendRow(table.Row{first, team.Id, team.Name, role, team.DefaultDataCenterId})
}
t.Render()

return nil
}
42 changes: 33 additions & 9 deletions cmd/list-workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Copyright © 2025 Codesphere Inc. <support@codesphere.com>
package cmd

import (
"errors"
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/api"
"github.com/codesphere-cloud/cs-go/pkg/out"
"github.com/jedib0t/go-pretty/v6/table"

Expand Down Expand Up @@ -47,26 +47,50 @@ func (l *ListWorkspacesCmd) parseLogCmdFlags() {
}

func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
if l.opts.TeamId == nil || *l.opts.TeamId < 0 {
return errors.New("team ID not set or invalid, please use --team-id to set one")
}

client, err := NewClient(l.opts.GlobalOptions)
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %e", err)
}

workspaces, err := client.ListWorkspaces(*l.opts.TeamId)
teams, err := l.getTeamIds(client)
if err != nil {
return fmt.Errorf("failed to list workspaces: %e", err)
return fmt.Errorf("failed to get teams: %e", err)
}
workspaces := []api.Workspace{}
for _, team := range teams {
teamWorkspaces, err := client.ListWorkspaces(team)
if err != nil {
return fmt.Errorf("failed to list workspaces: %e", err)
}
workspaces = append(workspaces, teamWorkspaces...)
}

t := out.GetTableWriter()
t.AppendHeader(table.Row{"ID", "Name", "Repository"})
t.AppendHeader(table.Row{"Team ID", "ID", "Name", "Repository"})
for _, w := range workspaces {
t.AppendRow(table.Row{w.Id, w.Name, *w.GitUrl.Get()})
gitUrl := ""
if w.GitUrl.Get() != nil {
gitUrl = *w.GitUrl.Get()
}
t.AppendRow(table.Row{w.TeamId, w.Id, w.Name, gitUrl})
}
t.Render()

return nil
}

func (l *ListWorkspacesCmd) getTeamIds(client *api.Client) (teams []int, err error) {
if l.opts.TeamId != nil && *l.opts.TeamId >= 0 {
teams = append(teams, *l.opts.TeamId)
return
}
var allTeams []api.Team
allTeams, err = client.ListTeams()
if err != nil {
return
}
for _, t := range allTeams {
teams = append(teams, int(t.Id))
}
return
}
1 change: 1 addition & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func addListCmd(rootCmd *cobra.Command, opts GlobalOptions) {
l.parseLogCmdFlags()
rootCmd.AddCommand(l.cmd)
addListWorkspacesCmd(l.cmd, opts)
addListTeamsCmd(l.cmd, opts)
}

func (l *ListCmd) parseLogCmdFlags() {
Expand Down