-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist-workspaces.go
More file actions
77 lines (66 loc) · 1.77 KB
/
list-workspaces.go
File metadata and controls
77 lines (66 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
*/
package cmd
import (
"context"
"errors"
"fmt"
"github.com/codesphere-cloud/cs-go/pkg/api"
"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/codesphere-cloud/cs-go/pkg/out"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)
type ListWorkspacesCmd struct {
cmd *cobra.Command
opts ListWorkspacesOptions
}
type ListWorkspacesOptions struct {
GlobalOptions
TeamId *int
}
func addListWorkspacesCmd(p *cobra.Command, opts GlobalOptions) {
l := ListWorkspacesCmd{
cmd: &cobra.Command{
Use: "workspaces",
Short: "list workspaces",
Long: `list workspaces available in Codesphere`,
Example: `
List all workspaces:
$ cs list workspaces --team-id <team-id>
`,
},
opts: ListWorkspacesOptions{GlobalOptions: opts},
}
l.cmd.RunE = l.RunE
l.parseLogCmdFlags()
p.AddCommand(l.cmd)
}
func (l *ListWorkspacesCmd) parseLogCmdFlags() {
l.opts.TeamId = l.cmd.Flags().IntP("team-id", "t", -1, "ID of team to query")
}
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")
}
token, err := cs.GetApiToken()
if err != nil {
return fmt.Errorf("failed to get API token: %e", err)
}
client := api.NewClient(context.Background(), api.Configuration{
BaseUrl: l.opts.GetApiUrl(),
Token: token,
})
workspaces, err := client.ListWorkspaces(*l.opts.TeamId)
if err != nil {
return fmt.Errorf("failed to list workspaces: %e", err)
}
t := out.GetTableWriter()
t.AppendHeader(table.Row{"ID", "Name", "Repository"})
for _, w := range workspaces {
t.AppendRow(table.Row{w.Id, w.Name, *w.GitUrl.Get()})
}
t.Render()
return nil
}