/
create.go
98 lines (77 loc) · 2.25 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
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
package orgs
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/iostreams"
"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)
flag.Add(cmd,
flag.Bool{
Name: "apps-v2-default-on",
Description: "Configure this org to use apps v2 by default for new apps (deprecated)",
Default: false,
Hidden: true,
},
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func runCreate(ctx context.Context) error {
client := fly.ClientFromContext(ctx)
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
user, err := client.GetCurrentUser(ctx)
if err != nil {
return err
}
var name string
name = flag.FirstArg(ctx)
if user.EnablePaidHobby {
fmt.Fprintf(io.Out, "New organizations start on the $5/mo Hobby Plan.\n\n")
if name == "" {
confirmed, err := prompt.Confirm(ctx, "Do you still want to create the organization?")
if err != nil {
return err
}
if !confirmed {
return nil
}
}
}
if name == "" {
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")
}
if err != nil {
return err
}
}
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 {
fmt.Fprintf(io.Out, "Your organization %s (%s) was created successfully. Visit %s to add a credit card and enable deployment.\n", org.Name, org.Slug, colorize.Green(fmt.Sprintf("https://fly.io/dashboard/%s/billing", org.Slug)))
}
return nil
}