-
Notifications
You must be signed in to change notification settings - Fork 219
/
orgs.go
171 lines (125 loc) · 4.7 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docstrings"
)
func newOrgsCommand() *Command {
orgsStrings := docstrings.KeyStrings{Usage: "orgs",
Short: "Commands for managing Fly organizations",
Long: `Commands for managing Fly organizations. list, create, show and
destroy organizations.
Organization admins can also invite or remove users from Organizations.
`}
orgscmd := BuildCommandKS(nil, nil, orgsStrings, os.Stdout, requireSession)
orgsListStrings := docstrings.KeyStrings{Usage: "list",
Short: "Lists organizations for current user",
Long: `Lists organizations available to current user.
`}
BuildCommandKS(orgscmd, runOrgsList, orgsListStrings, os.Stdout, requireSession)
orgsShowStrings := docstrings.KeyStrings{Usage: "show <org>", Short: "Show organization", Long: ""}
orgsShowCommand := BuildCommandKS(orgscmd, runOrgsShow, orgsShowStrings, os.Stdout, requireSession)
orgsShowCommand.Args = cobra.ExactArgs(1)
orgsInviteStrings := docstrings.KeyStrings{Usage: "invite <org> <email>", Short: "invite to organization", Long: ""}
orgsInviteCommand := BuildCommandKS(orgscmd, runOrgsInvite, orgsInviteStrings, os.Stdout, requireSession)
orgsInviteCommand.Args = cobra.MaximumNArgs(2)
orgsRevokeStrings := docstrings.KeyStrings{Usage: "revoke <org> <email>", Short: "revoke an invitation to an organization", Long: ""}
orgsRevokeCommand := BuildCommandKS(orgscmd, runOrgsRevoke, orgsRevokeStrings, os.Stdout, requireSession)
orgsRevokeCommand.Args = cobra.MaximumNArgs(2)
orgsRemoveStrings := docstrings.KeyStrings{Usage: "remove <org> <email>", Short: "revoke an invitation to an organization", Long: ""}
orgsRemoveCommand := BuildCommandKS(orgscmd, runOrgsRemove, orgsRemoveStrings, os.Stdout, requireSession)
orgsRemoveCommand.Args = cobra.MaximumNArgs(2)
orgsCreateStrings := docstrings.KeyStrings{Usage: "create <org>", Short: "Create an Organization", Long: ""}
orgsCreateCommand := BuildCommandKS(orgscmd, runOrgsCreate, orgsCreateStrings, os.Stdout, requireSession)
orgsCreateCommand.Args = cobra.MaximumNArgs(1)
orgsDeleteStrings := docstrings.KeyStrings{Usage: "delete <org>", Short: "Delete an Organization", Long: ""}
orgsDeleteCommand := BuildCommandKS(orgscmd, runOrgsDelete, orgsDeleteStrings, os.Stdout, requireSession)
orgsDeleteCommand.Args = cobra.MaximumNArgs(1)
return orgscmd
}
func getOrgId(ctx *cmdctx.CmdContext, slug string) (id string, err error) {
personalOrganization, organizations, err := ctx.Client.API().GetCurrentOrganizations()
if personalOrganization.Slug == slug {
return personalOrganization.ID, nil
}
for _, o := range organizations {
if o.Slug == slug {
return o.ID, nil
}
}
return "", fmt.Errorf("slug %s not found", slug)
}
func runOrgsList(cmdctx *cmdctx.CmdContext) error {
asJSON := cmdctx.OutputJSON()
personalOrganization, organizations, err := cmdctx.Client.API().GetCurrentOrganizations()
if err != nil {
return err
}
if asJSON {
type MyOrgs struct {
PersonalOrganization api.Organization
Organizations []api.Organization
}
cmdctx.WriteJSON(MyOrgs{PersonalOrganization: personalOrganization, Organizations: organizations})
return nil
}
printOrg(personalOrganization, true)
for _, o := range organizations {
if o.ID != personalOrganization.ID {
printOrg(o, false)
}
}
return nil
}
func printOrg(o api.Organization, headers bool) {
if headers {
fmt.Printf("%-20s %-20s %-10s\n", "Name", "Slug", "Type")
fmt.Printf("%-20s %-20s %-10s\n", "----", "----", "----")
}
fmt.Printf("%-20s %-20s %-10s\n", o.Name, o.Slug, o.Type)
}
func runOrgsShow(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Show Not implemented")
}
func runOrgsInvite(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Invite Not implemented")
}
func runOrgsCreate(ctx *cmdctx.CmdContext) error {
asJSON := ctx.OutputJSON()
orgname := ctx.Args[0]
organization, err := ctx.Client.API().CreateOrganization(orgname)
if err != nil {
return err
}
if asJSON {
ctx.WriteJSON(organization)
} else {
printOrg(*organization, true)
}
return nil
}
func runOrgsRemove(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Remove Not implemented")
}
func runOrgsRevoke(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Revoke Not implemented")
}
func runOrgsDelete(ctx *cmdctx.CmdContext) error {
orgslug := ctx.Args[0]
orgid, err := getOrgId(ctx, orgslug)
if err != nil {
return err
}
confirmed := confirm(fmt.Sprintf("Are you sure you want to delete the %s organization?", orgslug))
if !confirmed {
return nil
}
_, err = ctx.Client.API().DeleteOrganization(orgid)
if err != nil {
return err
}
return nil
}