-
Notifications
You must be signed in to change notification settings - Fork 117
/
switch.go
78 lines (66 loc) · 1.7 KB
/
switch.go
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
package org
import (
"context"
"fmt"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)
func SwitchCmd(cfg *config.Config) *cobra.Command {
switchCmd := &cobra.Command{
Use: "switch [<org-name>]",
Short: "Switch to other organization",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdutil.Client(cfg)
if err != nil {
return err
}
defer client.Close()
var defaultOrg string
if len(args) == 0 {
res, err := client.ListOrganizations(context.Background(), &adminv1.ListOrganizationsRequest{})
if err != nil {
return err
}
defaultOrg, err = SwitchSelectFlow(res.Organizations)
if err != nil {
return err
}
} else {
_, err = client.GetOrganization(context.Background(), &adminv1.GetOrganizationRequest{
Name: args[0],
})
if err != nil {
return err
}
defaultOrg = args[0]
}
err = dotrill.SetDefaultOrg(defaultOrg)
if err != nil {
return err
}
cfg.Org = defaultOrg
fmt.Printf("Set default organization to %q.", defaultOrg)
return nil
},
}
return switchCmd
}
func SwitchSelectFlow(orgs []*adminv1.Organization) (string, error) {
if len(orgs) < 1 {
fmt.Println("No organizations found, run `rill org create` first.")
return "", nil
}
var orgNames []string
for _, org := range orgs {
orgNames = append(orgNames, org.Name)
}
org, err := dotrill.GetDefaultOrg()
if err != nil {
return "", err
}
return cmdutil.SelectPrompt("Select default org.", orgNames, org), nil
}