-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathworkspace.go
More file actions
101 lines (86 loc) · 2.61 KB
/
workspace.go
File metadata and controls
101 lines (86 loc) · 2.61 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package command
import (
"encoding/json"
"errors"
"github.com/DefangLabs/defang/src/pkg"
"github.com/DefangLabs/defang/src/pkg/auth"
"github.com/DefangLabs/defang/src/pkg/cli"
"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/DefangLabs/defang/src/pkg/types"
"github.com/spf13/cobra"
)
func listWorkspaces(cmd *cobra.Command, args []string) error {
jsonMode, _ := cmd.Flags().GetBool("json")
verbose := global.Verbose
token := client.GetExistingToken(global.Cluster)
if token == "" {
return errors.New("no access token found; please log in with `defang login`")
}
// Determine current selection from flag/env/token, then reconcile with WhoAmI.
currentWorkspace := global.Tenant
info, err := auth.FetchUserInfo(cmd.Context(), token)
if err != nil {
return err
}
rows := cli.WorkspaceRows(info, currentWorkspace)
if !verbose {
for i := range rows {
rows[i].ID = ""
}
}
if jsonMode {
out, err := json.MarshalIndent(rows, "", " ")
if err != nil {
return err
}
_, err = term.Println(string(out))
return err
}
if len(rows) == 0 {
term.Info("No workspaces found for this account.")
return nil
}
headers := []string{"Name", "Current"}
if verbose {
headers = []string{"Name", "ID", "Current"}
}
return term.Table(rows, headers...)
}
var workspaceCmd = &cobra.Command{
Use: "workspace",
Aliases: []string{"workspaces", "ws"},
Args: cobra.NoArgs,
Annotations: authNeededAnnotation,
Short: "Manage workspaces",
RunE: listWorkspaces,
}
var workspaceListCmd = &cobra.Command{
Use: "ls",
Aliases: []string{"list"},
Args: cobra.NoArgs,
Annotations: authNeededAnnotation,
Short: "List available workspaces",
RunE: listWorkspaces,
}
var workspaceSelectCmd = &cobra.Command{
Use: "select WORKSPACE",
Aliases: []string{"use", "switch"},
Args: cobra.ExactArgs(1),
// Annotations: authNeededAnnotation,
Short: "Select a workspace to use",
RunE: func(cmd *cobra.Command, args []string) error {
global.Tenant = types.TenantNameOrID(args[0])
if _, err := cli.ConnectWithTenant(cmd.Context(), global.Cluster, global.Tenant); err != nil {
return err
}
term.Infof("Switched to workspace %q\n", global.Tenant)
return client.SetCurrentTenant(global.Tenant)
},
}
func init() {
workspaceCmd.Flags().Bool("json", pkg.GetenvBool("DEFANG_JSON"), "print output in JSON format")
workspaceListCmd.Flags().Bool("json", pkg.GetenvBool("DEFANG_JSON"), "print output in JSON format")
workspaceCmd.AddCommand(workspaceListCmd)
workspaceCmd.AddCommand(workspaceSelectCmd)
}