@@ -4,24 +4,23 @@ import (
44 "fmt"
55
66 "github.com/codesphere-cloud/cs-go/api"
7- "github.com/codesphere-cloud/cs-go/pkg/cs"
87 "github.com/codesphere-cloud/cs-go/pkg/out"
98 "github.com/jedib0t/go-pretty/v6/table"
109
1110 "github.com/spf13/cobra"
1211)
1312
1413type ListWorkspacesCmd struct {
14+ Opts ListWorkspacesOptions
1515 cmd * cobra.Command
16- opts ListWorkspacesOptions
1716}
1817
1918type ListWorkspacesOptions struct {
20- cs. GlobalOptions
19+ GlobalOptions
2120 TeamId * int
2221}
2322
24- func addListWorkspacesCmd (p * cobra.Command , opts cs. GlobalOptions ) {
23+ func addListWorkspacesCmd (p * cobra.Command , opts GlobalOptions ) {
2524 l := ListWorkspacesCmd {
2625 cmd : & cobra.Command {
2726 Use : "workspaces" ,
@@ -33,34 +32,26 @@ List all workspaces:
3332$ cs list workspaces --team-id <team-id>
3433 ` ,
3534 },
36- opts : ListWorkspacesOptions {GlobalOptions : opts },
35+ Opts : ListWorkspacesOptions {GlobalOptions : opts },
3736 }
3837 l .cmd .RunE = l .RunE
3938 l .parseLogCmdFlags ()
4039 p .AddCommand (l .cmd )
4140}
4241
4342func (l * ListWorkspacesCmd ) parseLogCmdFlags () {
44- l .opts .TeamId = l .cmd .Flags ().IntP ("team-id" , "t" , - 1 , "ID of team to query" )
43+ l .Opts .TeamId = l .cmd .Flags ().IntP ("team-id" , "t" , - 1 , "ID of team to query" )
4544}
4645
4746func (l * ListWorkspacesCmd ) RunE (_ * cobra.Command , args []string ) (err error ) {
48- client , err := cs . NewClient (l .opts .GlobalOptions )
47+ client , err := NewClient (l .Opts .GlobalOptions )
4948 if err != nil {
5049 return fmt .Errorf ("failed to create Codesphere client: %e" , err )
5150 }
5251
53- teams , err := l .getTeamIds (client )
52+ workspaces , err := l .ListWorkspaces (client )
5453 if err != nil {
55- return fmt .Errorf ("failed to get teams: %e" , err )
56- }
57- workspaces := []api.Workspace {}
58- for _ , team := range teams {
59- teamWorkspaces , err := client .ListWorkspaces (team )
60- if err != nil {
61- return fmt .Errorf ("failed to list workspaces: %e" , err )
62- }
63- workspaces = append (workspaces , teamWorkspaces ... )
54+ return fmt .Errorf ("failed to list workspaces: %e" , err )
6455 }
6556
6657 t := out .GetTableWriter ()
@@ -77,9 +68,25 @@ func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
7768 return nil
7869}
7970
80- func (l * ListWorkspacesCmd ) getTeamIds (client cs.Client ) (teams []int , err error ) {
81- if l .opts .TeamId != nil && * l .opts .TeamId >= 0 {
82- teams = append (teams , * l .opts .TeamId )
71+ func (l * ListWorkspacesCmd ) ListWorkspaces (client Client ) ([]api.Workspace , error ) {
72+ teams , err := l .getTeamIds (client )
73+ if err != nil {
74+ return nil , fmt .Errorf ("failed to get teams: %e" , err )
75+ }
76+ workspaces := []api.Workspace {}
77+ for _ , team := range teams {
78+ teamWorkspaces , err := client .ListWorkspaces (team )
79+ if err != nil {
80+ return nil , fmt .Errorf ("failed to list workspaces: %e" , err )
81+ }
82+ workspaces = append (workspaces , teamWorkspaces ... )
83+ }
84+ return workspaces , nil
85+ }
86+
87+ func (l * ListWorkspacesCmd ) getTeamIds (client Client ) (teams []int , err error ) {
88+ if l .Opts .TeamId != nil && * l .Opts .TeamId >= 0 {
89+ teams = append (teams , * l .Opts .TeamId )
8390 return
8491 }
8592 var allTeams []api.Team
0 commit comments