-
Notifications
You must be signed in to change notification settings - Fork 32
/
orgs.go
141 lines (115 loc) · 2.87 KB
/
orgs.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cmd
import (
"context"
"fmt"
"sync"
"github.com/urfave/cli"
"github.com/arigatomachine/cli/api"
"github.com/arigatomachine/cli/config"
"github.com/arigatomachine/cli/promptui"
)
func init() {
orgs := cli.Command{
Name: "orgs",
Usage: "View and create organizations",
Category: "ORGANIZATIONS",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new organization",
ArgsUsage: "<name>",
Action: chain(ensureDaemon, orgsCreate),
},
{
Name: "list",
Usage: "List organizations associated with your account",
Action: chain(ensureDaemon, ensureSession, orgsListCmd),
},
},
}
Cmds = append(Cmds, orgs)
}
const orgCreateFailed = "Org creation failed, please try again."
func orgsCreate(ctx *cli.Context) error {
args := ctx.Args()
usage := usageString(ctx)
if len(args) > 1 {
text := "Too many arguments\n\n"
text += usage
return cli.NewExitError(text, -1)
}
var name string
var err error
if len(args) == 1 {
name = args[0]
}
label := "Org name"
if name == "" {
name, err = NamePrompt(&label, "")
if err != nil {
if err == promptui.ErrEOF || err == promptui.ErrInterrupt {
return err
}
fmt.Println("")
return cli.NewExitError(orgCreateFailed, -1)
}
} else {
fmt.Println(promptui.SuccessfulValue(label, name))
}
c := context.Background()
cfg, err := config.LoadConfig()
if err != nil {
return cli.NewExitError(orgCreateFailed, -1)
}
client := api.NewClient(cfg)
_, err = createOrgByName(ctx, c, client, name)
return err
}
func createOrgByName(ctx *cli.Context, c context.Context, client *api.Client, name string) (*api.OrgResult, error) {
org, err := client.Orgs.Create(c, name)
if err != nil {
return nil, cli.NewExitError(orgCreateFailed, -1)
}
err = generateKeypairsForOrg(ctx, c, client, org.ID, false)
if err != nil {
msg := fmt.Sprintf("Could not generate keypairs for org. Run '%s keypairs generate' to fix.", ctx.App.Name)
return nil, cli.NewExitError(msg, -1)
}
fmt.Println("Org " + org.Body.Name + " created.")
return org, nil
}
func orgsListCmd(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
var wg sync.WaitGroup
wg.Add(2)
var orgs []api.OrgResult
var self *api.UserResult
var oErr, sErr error
go func() {
orgs, oErr = client.Orgs.List(context.Background())
wg.Done()
}()
go func() {
self, sErr = client.Users.Self(context.Background())
wg.Done()
}()
wg.Wait()
if oErr != nil || sErr != nil {
return cli.NewExitError("Error fetching orgs list", -1)
}
withoutPersonal := orgs
for i, o := range orgs {
if o.Body.Name == self.Body.Username {
fmt.Printf(" %s [personal]\n", o.Body.Name)
withoutPersonal = append(orgs[:i], orgs[i+1:]...)
}
}
for _, o := range withoutPersonal {
fmt.Printf(" %s\n", o.Body.Name)
}
return nil
}