• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 12001912209

25 Nov 2024 02:09AM UTC coverage: 59.524% (-0.03%) from 59.552%
12001912209

Pull #2906

github

sweatybridge
chore: revert to single quote
Pull Request #2906: chore: update api/beta.yaml from staging v1-yaml

8 of 14 new or added lines in 7 files covered. (57.14%)

5 existing lines in 1 file now uncovered.

6381 of 10720 relevant lines covered (59.52%)

6.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.96
/internal/projects/create/create.go
1
package create
2

3
import (
4
        "context"
5
        "fmt"
6
        "os"
7
        "strings"
8

9
        "github.com/go-errors/errors"
10
        "github.com/spf13/afero"
11
        "github.com/spf13/viper"
12
        "github.com/supabase/cli/internal/utils"
13
        "github.com/supabase/cli/internal/utils/credentials"
14
        "github.com/supabase/cli/internal/utils/flags"
15
        "github.com/supabase/cli/pkg/api"
16
)
17

18
func Run(ctx context.Context, params api.V1CreateProjectBodyDto, fsys afero.Fs) error {
5✔
19
        if err := promptMissingParams(ctx, &params); err != nil {
5✔
20
                return err
×
21
        }
×
22

23
        resp, err := utils.GetSupabase().V1CreateAProjectWithResponse(ctx, params)
5✔
24
        if err != nil {
6✔
25
                return errors.Errorf("failed to create project: %w", err)
1✔
26
        }
1✔
27
        if resp.JSON201 == nil {
7✔
28
                return errors.New("Unexpected error creating project: " + string(resp.Body))
3✔
29
        }
3✔
30

31
        flags.ProjectRef = resp.JSON201.Id
1✔
32
        viper.Set("DB_PASSWORD", params.DbPass)
1✔
33
        if err := credentials.StoreProvider.Set(flags.ProjectRef, params.DbPass); err != nil {
1✔
34
                fmt.Fprintln(os.Stderr, "Failed to save database password:", err)
×
35
        }
×
36

37
        projectUrl := fmt.Sprintf("%s/project/%s", utils.GetSupabaseDashboardURL(), resp.JSON201.Id)
1✔
38
        fmt.Fprintf(os.Stderr, "Created a new project %s at %s\n", utils.Aqua(resp.JSON201.Name), utils.Bold(projectUrl))
1✔
39
        if utils.OutputFormat.Value == utils.OutputPretty {
2✔
40
                return nil
1✔
41
        }
1✔
42

43
        return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, resp.JSON201)
×
44
}
45

46
func printKeyValue(key, value string) string {
5✔
47
        indent := 20 - len(key)
5✔
48
        spaces := strings.Repeat(" ", indent)
5✔
49
        return key + ":" + spaces + value
5✔
50
}
5✔
51

52
func promptMissingParams(ctx context.Context, body *api.V1CreateProjectBodyDto) error {
5✔
53
        var err error
5✔
54
        if len(body.Name) == 0 {
5✔
55
                if body.Name, err = promptProjectName(ctx); err != nil {
×
56
                        return err
×
57
                }
×
58
        } else {
5✔
59
                fmt.Fprintln(os.Stderr, printKeyValue("Creating project", body.Name))
5✔
60
        }
5✔
61
        if len(body.OrganizationId) == 0 {
5✔
62
                if body.OrganizationId, err = promptOrgId(ctx); err != nil {
×
63
                        return err
×
64
                }
×
65
                fmt.Fprintln(os.Stderr, printKeyValue("Selected org-id", body.OrganizationId))
×
66
        }
67
        if len(body.Region) == 0 {
5✔
68
                if body.Region, err = promptProjectRegion(ctx); err != nil {
×
69
                        return err
×
70
                }
×
71
                fmt.Fprintln(os.Stderr, printKeyValue("Selected region", string(body.Region)))
×
72
        }
73
        if len(body.DbPass) == 0 {
5✔
74
                body.DbPass = flags.PromptPassword(os.Stdin)
×
75
        }
×
76
        return nil
5✔
77
}
78

79
func promptProjectName(ctx context.Context) (string, error) {
×
80
        title := "Enter your project name: "
×
81
        if name, err := utils.NewConsole().PromptText(ctx, title); err != nil {
×
82
                return "", err
×
83
        } else if len(name) > 0 {
×
84
                return name, nil
×
85
        }
×
86
        return "", errors.New("project name cannot be empty")
×
87
}
88

89
func promptOrgId(ctx context.Context) (string, error) {
×
90
        title := "Which organisation do you want to create the project for?"
×
91
        resp, err := utils.GetSupabase().V1ListAllOrganizationsWithResponse(ctx)
×
92
        if err != nil {
×
93
                return "", err
×
94
        }
×
95
        if resp.JSON200 == nil {
×
96
                return "", errors.New("Unexpected error retrieving organizations: " + string(resp.Body))
×
97
        }
×
98
        items := make([]utils.PromptItem, len(*resp.JSON200))
×
99
        for i, org := range *resp.JSON200 {
×
100
                items[i] = utils.PromptItem{Summary: org.Name, Details: org.Id}
×
101
        }
×
102
        choice, err := utils.PromptChoice(ctx, title, items)
×
103
        if err != nil {
×
104
                return "", err
×
105
        }
×
106
        return choice.Details, nil
×
107
}
108

NEW
109
func promptProjectRegion(ctx context.Context) (api.V1CreateProjectBodyDtoRegion, error) {
×
110
        title := "Which region do you want to host the project in?"
×
111
        items := make([]utils.PromptItem, len(utils.RegionMap))
×
112
        i := 0
×
113
        for k, v := range utils.RegionMap {
×
114
                items[i] = utils.PromptItem{Summary: k, Details: v}
×
115
                i++
×
116
        }
×
117
        choice, err := utils.PromptChoice(ctx, title, items)
×
118
        if err != nil {
×
119
                return "", err
×
120
        }
×
NEW
121
        return api.V1CreateProjectBodyDtoRegion(choice.Summary), nil
×
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc