-
Notifications
You must be signed in to change notification settings - Fork 221
/
orgs.go
123 lines (94 loc) · 2.9 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
package orgs
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/orgs/appsv2"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/sort"
)
// TODO: deprecate & remove
func New() *cobra.Command {
const (
long = `Commands for managing Fly organizations. list, create, show and
destroy organizations.
Organization admins can also invite or remove users from Organizations.
`
short = "Commands for managing Fly organizations"
)
// TODO: list should also accept the --org param
orgs := command.New("orgs", short, long, nil)
orgs.AddCommand(
newList(),
newShow(),
newInvite(),
newRemove(),
newCreate(),
newDelete(),
appsv2.New(),
)
return orgs
}
func emailFromSecondArgOrPrompt(ctx context.Context) (email string, err error) {
if args := flag.Args(ctx); len(args) > 1 {
email = args[1]
return
}
const msg = "Enter User Email:"
if err = prompt.String(ctx, &email, msg, "", true); prompt.IsNonInteractive(err) {
err = prompt.NonInteractiveError("email argument must be specified when not running interactively")
}
return
}
var errSlugArgMustBeSpecified = prompt.NonInteractiveError("slug argument must be specified when not running interactively")
func slugFromFirstArgOrSelect(ctx context.Context, filters ...api.OrganizationFilter) (slug string, err error) {
if slug = flag.FirstArg(ctx); slug != "" {
return
}
io := iostreams.FromContext(ctx)
if !io.IsInteractive() {
err = errSlugArgMustBeSpecified
return
}
client := client.FromContext(ctx).API()
var orgs []api.Organization
if orgs, err = client.GetOrganizations(ctx, filters...); err != nil {
return
}
sort.OrganizationsByTypeAndName(orgs)
var org *api.Organization
if org, err = prompt.SelectOrg(ctx, orgs); prompt.IsNonInteractive(err) {
err = errSlugArgMustBeSpecified
} else if err == nil {
slug = org.Slug
}
return
}
func OrgFromFirstArgOrSelect(ctx context.Context, filters ...api.OrganizationFilter) (*api.Organization, error) {
slug, err := slugFromFirstArgOrSelect(ctx, filters...)
if err != nil {
return nil, err
}
return OrgFromSlug(ctx, slug)
}
func OrgFromSlug(ctx context.Context, slug string) (*api.Organization, error) {
client := client.FromContext(ctx).API()
org, err := client.GetOrganizationBySlug(ctx, slug)
if err != nil {
return nil, fmt.Errorf("failed retrieving organization with slug %s: %w", slug, err)
}
return org, nil
}
func printOrg(w io.Writer, org *api.Organization, headers bool) {
if headers {
fmt.Fprintf(w, "%-20s %-20s %-10s\n", "Name", "Slug", "Type")
fmt.Fprintf(w, "%-20s %-20s %-10s\n", "----", "----", "----")
}
fmt.Fprintf(w, "%-20s %-20s %-10s\n", org.Name, org.Slug, org.Type)
}