-
Notifications
You must be signed in to change notification settings - Fork 52
/
create.go
113 lines (85 loc) · 3.24 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package database
import (
"fmt"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
ps "github.com/planetscale/planetscale-go/planetscale"
"github.com/spf13/cobra"
)
// CreateCmd is the command for creating a database.
func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
createReq := &ps.CreateDatabaseRequest{}
cmd := &cobra.Command{
Use: "create <database>",
Short: "Create a database instance",
Args: cmdutil.RequiredArgs("database"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
plan, err := cmd.Flags().GetString("plan")
if err != nil {
return err
}
createReq.Plan = ps.Plan(plan)
clusterSize, err := cmd.Flags().GetString("cluster-size")
if err != nil {
return err
}
createReq.ClusterSize = ps.ClusterSize(clusterSize)
createReq.Organization = ch.Config.Organization
createReq.Name = args[0]
client, err := ch.Client()
if err != nil {
return err
}
end := ch.Printer.PrintProgress("Creating database...")
defer end()
database, err := client.Databases.Create(ctx, createReq)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
return fmt.Errorf("organization %s does not exist", printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}
end()
if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Database %s was successfully created.\n\nView this database in the browser: %s\n", printer.BoldBlue(database.Name), printer.BoldBlue(database.HtmlURL))
return nil
}
return ch.Printer.PrintResource(toDatabase(database))
},
}
cmd.Flags().StringVar(&createReq.Notes, "notes", "", "notes for the database")
cmd.Flags().MarkDeprecated("notes", "is no longer available.")
cmd.Flags().StringVar(&createReq.Region, "region", "", "region for the database")
cmd.Flags().String("plan", "", "plan for the database. Options: hobby or scaler_pro")
cmd.Flags().String("cluster-size", "", "cluster size for Scaler Pro databases. Options: PS_10, PS_20, PS_40, PS_80, PS_160, PS_320, PS_400")
cmd.RegisterFlagCompletionFunc("region", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := cmd.Context()
client, err := ch.Client()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
regions, err := client.Regions.List(ctx, &ps.ListRegionsRequest{})
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
regionStrs := make([]string, 0)
for _, r := range regions {
if r.Enabled {
regionStrs = append(regionStrs, r.Slug)
}
}
return regionStrs, cobra.ShellCompDirectiveDefault
})
cmd.RegisterFlagCompletionFunc("cluster_size", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
clusterSizes := []string{"PS_10", "PS_20", "PS_40", "PS_80", "PS_160", "PS_320", "PS_400"}
return clusterSizes, cobra.ShellCompDirectiveDefault
})
cmd.RegisterFlagCompletionFunc("plan", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
plans := []string{"hobby", "scaler", "scaler_pro"}
return plans, cobra.ShellCompDirectiveDefault
})
return cmd
}