-
Notifications
You must be signed in to change notification settings - Fork 224
/
create.go
70 lines (53 loc) · 1.51 KB
/
create.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
package orgs
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
)
func newCreate() *cobra.Command {
const (
long = `Create a new organization. Other users can be invited to join the
organization later.
`
short = "Create an organization"
usage = "create [name]"
)
cmd := command.New(usage, short, long, runCreate,
command.RequireSession)
cmd.Args = cobra.MaximumNArgs(1)
return cmd
}
func runCreate(ctx context.Context) error {
name, err := nameFromFirstArgOrPrompt(ctx)
if err != nil {
return err
}
client := client.FromContext(ctx).API()
org, err := client.CreateOrganization(ctx, name)
if err != nil {
return fmt.Errorf("failed creating organization: %w", err)
}
if io := iostreams.FromContext(ctx); config.FromContext(ctx).JSONOutput {
_ = render.JSON(io.Out, org)
} else {
printOrg(io.Out, org, true)
}
return nil
}
func nameFromFirstArgOrPrompt(ctx context.Context) (name string, err error) {
if name = flag.FirstArg(ctx); name != "" {
return
}
const msg = "Enter Organization Name:"
if err = prompt.String(ctx, &name, msg, "", true); prompt.IsNonInteractive(err) {
err = prompt.NonInteractiveError("name argument must be specified when not running interactively")
}
return
}