-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathbootstrap.go
96 lines (90 loc) · 2.49 KB
/
bootstrap.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
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/bootstrap"
"github.com/supabase/cli/internal/utils"
)
var (
starter = bootstrap.StarterTemplate{
Name: "scratch",
Description: "An empty project from scratch.",
Start: "supabase start",
}
bootstrapCmd = &cobra.Command{
GroupID: groupQuickStart,
Use: "bootstrap [template]",
Short: "Bootstrap a Supabase project from a starter template",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
if !viper.IsSet("WORKDIR") {
title := fmt.Sprintf("Enter a directory to bootstrap your project (or leave blank to use %s): ", utils.Bold(utils.CurrentDirAbs))
if workdir, err := utils.NewConsole().PromptText(ctx, title); err != nil {
return err
} else {
viper.Set("WORKDIR", workdir)
}
}
client := utils.GetGitHubClient(ctx)
templates, err := bootstrap.ListSamples(ctx, client)
if err != nil {
return err
}
if len(args) > 0 {
name := args[0]
for _, t := range templates {
if strings.EqualFold(t.Name, name) {
starter = t
break
}
}
if !strings.EqualFold(starter.Name, name) {
return errors.New("Invalid template: " + name)
}
} else {
if err := promptStarterTemplate(ctx, templates); err != nil {
return err
}
}
return bootstrap.Run(ctx, starter, afero.NewOsFs())
},
}
)
func init() {
bootstrapFlags := bootstrapCmd.Flags()
bootstrapFlags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.")
cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", bootstrapFlags.Lookup("password")))
rootCmd.AddCommand(bootstrapCmd)
}
func promptStarterTemplate(ctx context.Context, templates []bootstrap.StarterTemplate) error {
items := make([]utils.PromptItem, len(templates))
for i, t := range templates {
items[i] = utils.PromptItem{
Index: i,
Summary: t.Name,
Details: t.Description,
}
}
items = append(items, utils.PromptItem{
Index: len(items),
Summary: starter.Name,
Details: starter.Description,
})
title := "Which starter template do you want to use?"
choice, err := utils.PromptChoice(ctx, title, items)
if err != nil {
return err
}
if choice.Index < len(templates) {
starter = templates[choice.Index]
}
return nil
}