Repository navigation
Expand file tree
/
Copy pathcreate.go
More file actions
131 lines (120 loc) · 3.83 KB
/
Copy pathcreate.go
File metadata and controls
131 lines (120 loc) · 3.83 KB
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
package create
import (
"context"
"fmt"
"os"
"strings"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
)
func Run(ctx context.Context, params api.V1CreateProjectBody, fsys afero.Fs) error {
if err := promptMissingParams(ctx, ¶ms); err != nil {
return err
}
resp, err := utils.GetSupabase().V1CreateAProjectWithResponse(ctx, params)
if err != nil {
return errors.Errorf("failed to create project: %w", err)
}
if resp.JSON201 == nil {
return errors.New("Unexpected error creating project: " + string(resp.Body))
}
flags.ProjectRef = resp.JSON201.Id
viper.Set("DB_PASSWORD", params.DbPass)
projectUrl := fmt.Sprintf("%s/project/%s", utils.GetSupabaseDashboardURL(), resp.JSON201.Id)
fmt.Fprintf(os.Stderr, "Created a new project at %s\n", utils.Bold(projectUrl))
if utils.OutputFormat.Value == utils.OutputPretty {
table := `|ORG ID|REFERENCE ID|NAME|REGION|CREATED AT (UTC)|
|-|-|-|-|-|
`
table += fmt.Sprintf(
"|`%s`|`%s`|`%s`|`%s`|`%s`|\n",
resp.JSON201.OrganizationSlug,
resp.JSON201.Id,
strings.ReplaceAll(resp.JSON201.Name, "|", "\\|"),
utils.FormatRegion(resp.JSON201.Region),
utils.FormatTimestamp(resp.JSON201.CreatedAt),
)
return utils.RenderTable(table)
}
return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, resp.JSON201)
}
func printKeyValue(key, value string) string {
indent := 20 - len(key)
spaces := strings.Repeat(" ", indent)
return key + ":" + spaces + value
}
func promptMissingParams(ctx context.Context, body *api.V1CreateProjectBody) error {
var err error
if len(body.Name) == 0 {
if body.Name, err = promptProjectName(ctx); err != nil {
return err
}
} else {
fmt.Fprintln(os.Stderr, printKeyValue("Creating project", body.Name))
}
if len(body.OrganizationSlug) == 0 {
if body.OrganizationSlug, err = promptOrgId(ctx); err != nil {
return err
}
fmt.Fprintln(os.Stderr, printKeyValue("Selected org-id", body.OrganizationSlug))
}
if body.Region == nil || len(*body.Region) == 0 {
region, err := promptProjectRegion(ctx)
if err != nil {
return err
}
body.Region = ®ion
fmt.Fprintln(os.Stderr, printKeyValue("Selected region", string(region)))
}
if len(body.DbPass) == 0 {
body.DbPass = flags.PromptPassword(os.Stdin)
}
return nil
}
func promptProjectName(ctx context.Context) (string, error) {
title := "Enter your project name: "
if name, err := utils.NewConsole().PromptText(ctx, title); err != nil {
return "", err
} else if len(name) > 0 {
return name, nil
}
return "", errors.New("project name cannot be empty")
}
func promptOrgId(ctx context.Context) (string, error) {
title := "Which organisation do you want to create the project for?"
resp, err := utils.GetSupabase().V1ListAllOrganizationsWithResponse(ctx)
if err != nil {
return "", err
}
if resp.JSON200 == nil {
return "", errors.New("Unexpected error retrieving organizations: " + string(resp.Body))
}
items := make([]utils.PromptItem, len(*resp.JSON200))
for i, org := range *resp.JSON200 {
items[i] = utils.PromptItem{Summary: org.Name, Details: org.Id}
}
choice, err := utils.PromptChoice(ctx, title, items)
if err != nil {
return "", err
}
return choice.Details, nil
}
func promptProjectRegion(ctx context.Context) (api.V1CreateProjectBodyRegion, error) {
title := "Which region do you want to host the project in?"
items := make([]utils.PromptItem, len(utils.CurrentProfile.ProjectRegions))
for i, region := range utils.CurrentProfile.ProjectRegions {
items[i] = utils.PromptItem{
Summary: string(region),
Details: utils.FormatRegion(string(region)),
}
}
choice, err := utils.PromptChoice(ctx, title, items)
if err != nil {
return "", err
}
return api.V1CreateProjectBodyRegion(choice.Summary), nil
}