Skip to content

Commit

Permalink
feat: support new branch params from cli flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Aug 19, 2024
1 parent 12fac6e commit 6f57e72
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
79 changes: 64 additions & 15 deletions cmd/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,45 @@ var (
}

branchRegion = utils.EnumFlag{
Allowed: make([]string, len(utils.FlyRegions)),
Allowed: flyRegions(),
}
branchSize = utils.EnumFlag{
Allowed: []string{
string(api.Micro),
string(api.Small),
string(api.Medium),
string(api.Large),
string(api.Xlarge),
string(api.N2xlarge),
string(api.N4xlarge),
string(api.N8xlarge),
string(api.N12xlarge),
string(api.N16xlarge),
},
}
persistent bool

branchCreateCmd = &cobra.Command{
Use: "create [name]",
Short: "Create a preview branch",
Long: "Create a preview branch for the linked project.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var name string
var body api.CreateBranchBody
if len(args) > 0 {
name = args[0]
body.BranchName = args[0]
}
cmdFlags := cmd.Flags()
if cmdFlags.Changed("region") {
body.Region = &branchRegion.Value
}
if cmdFlags.Changed("size") {
body.DesiredInstanceSize = (*api.DesiredInstanceSize)(&branchSize.Value)
}
if cmdFlags.Changed("persistent") {
body.Persistent = &persistent
}
return create.Run(cmd.Context(), name, branchRegion.Value, afero.NewOsFs())
return create.Run(cmd.Context(), body, afero.NewOsFs())
},
}

Expand Down Expand Up @@ -76,6 +101,15 @@ var (
},
}

branchStatus = utils.EnumFlag{
Allowed: []string{
string(api.BranchResponseStatusRUNNINGMIGRATIONS),
string(api.BranchResponseStatusMIGRATIONSPASSED),
string(api.BranchResponseStatusMIGRATIONSFAILED),
string(api.BranchResponseStatusFUNCTIONSDEPLOYED),
string(api.BranchResponseStatusFUNCTIONSFAILED),
},
}
branchName string
gitBranch string
resetOnPush bool
Expand All @@ -84,18 +118,25 @@ var (
Use: "update [branch-id]",
Short: "Update a preview branch",
Long: "Update a preview branch by its ID.",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmdFlags := cmd.Flags()
var body api.UpdateBranchBody
if cmd.Flags().Changed("name") {
if cmdFlags.Changed("name") {
body.BranchName = &branchName
}
if cmd.Flags().Changed("git-branch") {
if cmdFlags.Changed("git-branch") {
body.GitBranch = &gitBranch
}
if cmd.Flags().Changed("reset-on-push") {
if cmdFlags.Changed("reset-on-push") {
body.ResetOnPush = &resetOnPush
}
if cmdFlags.Changed("persistent") {
body.Persistent = &persistent
}
if cmdFlags.Changed("status") {
body.Status = (*api.UpdateBranchBodyStatus)(&branchStatus.Value)
}
ctx := cmd.Context()
if len(args) == 0 {
if err := promptBranchId(ctx, flags.ProjectRef); err != nil {
Expand Down Expand Up @@ -139,28 +180,36 @@ var (
func init() {
branchFlags := branchesCmd.PersistentFlags()
branchFlags.StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
// Setup enum flags
i := 0
for k := range utils.FlyRegions {
branchRegion.Allowed[i] = k
i++
}
sort.Strings(branchRegion.Allowed)
createFlags := branchCreateCmd.Flags()
createFlags.Var(&branchRegion, "region", "Select a region to deploy the branch database.")
createFlags.Var(&branchSize, "size", "Select a desired instance size for the branch database.")
createFlags.BoolVar(&persistent, "persistent", false, "Whether to create a persistent branch.")
branchesCmd.AddCommand(branchCreateCmd)
branchesCmd.AddCommand(branchListCmd)
branchesCmd.AddCommand(branchGetCmd)
updateFlags := branchUpdateCmd.Flags()
updateFlags.StringVar(&branchName, "name", "", "Rename the preview branch.")
updateFlags.StringVar(&gitBranch, "git-branch", "", "Change the associated git branch.")
updateFlags.BoolVar(&resetOnPush, "reset-on-push", false, "Reset the preview branch on git push.")
updateFlags.BoolVar(&persistent, "persistent", false, "Switch between ephemeral and persistent branch.")
updateFlags.Var(&branchStatus, "status", "Override the current branch status.")
branchesCmd.AddCommand(branchUpdateCmd)
branchesCmd.AddCommand(branchDeleteCmd)
branchesCmd.AddCommand(branchDisableCmd)
rootCmd.AddCommand(branchesCmd)
}

func flyRegions() []string {
result := make([]string, len(utils.FlyRegions))
i := 0
for k := range utils.FlyRegions {
result[i] = k
i++
}
sort.Strings(result)
return result
}

func promptBranchId(ctx context.Context, ref string) error {
resp, err := utils.GetSupabase().V1ListAllBranchesWithResponse(ctx, ref)
if err != nil {
Expand Down
13 changes: 5 additions & 8 deletions internal/branches/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@ import (
"github.com/supabase/cli/pkg/api"
)

func Run(ctx context.Context, name, region string, fsys afero.Fs) error {
func Run(ctx context.Context, body api.CreateBranchBody, fsys afero.Fs) error {
gitBranch := keys.GetGitBranchOrDefault("", fsys)
if len(name) == 0 && len(gitBranch) > 0 {
if len(body.BranchName) == 0 && len(gitBranch) > 0 {
title := fmt.Sprintf("Do you want to create a branch named %s?", utils.Aqua(gitBranch))
if shouldCreate, err := utils.NewConsole().PromptYesNo(ctx, title, true); err != nil {
return err
} else if !shouldCreate {
return errors.New(context.Canceled)
}
name = gitBranch
body.BranchName = gitBranch
}
body.GitBranch = &gitBranch

resp, err := utils.GetSupabase().V1CreateABranchWithResponse(ctx, flags.ProjectRef, api.V1CreateABranchJSONRequestBody{
BranchName: name,
GitBranch: &gitBranch,
Region: &region,
})
resp, err := utils.GetSupabase().V1CreateABranchWithResponse(ctx, flags.ProjectRef, body)
if err != nil {
return errors.Errorf("failed to create preview branch: %w", err)
}
Expand Down
12 changes: 9 additions & 3 deletions internal/branches/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func TestCreateCommand(t *testing.T) {
Id: "test-uuid",
})
// Run test
err := Run(context.Background(), "", "sin", fsys)
err := Run(context.Background(), api.CreateBranchBody{
Region: utils.Ptr("sin"),
}, fsys)
// Check error
assert.NoError(t, err)
})
Expand All @@ -50,7 +52,9 @@ func TestCreateCommand(t *testing.T) {
Post("/v1/projects/" + flags.ProjectRef + "/branches").
ReplyError(net.ErrClosed)
// Run test
err := Run(context.Background(), "", "sin", fsys)
err := Run(context.Background(), api.CreateBranchBody{
Region: utils.Ptr("sin"),
}, fsys)
// Check error
assert.ErrorIs(t, err, net.ErrClosed)
})
Expand All @@ -65,7 +69,9 @@ func TestCreateCommand(t *testing.T) {
Post("/v1/projects/" + flags.ProjectRef + "/branches").
Reply(http.StatusServiceUnavailable)
// Run test
err := Run(context.Background(), "", "sin", fsys)
err := Run(context.Background(), api.CreateBranchBody{
Region: utils.Ptr("sin"),
}, fsys)
// Check error
assert.ErrorContains(t, err, "Unexpected error creating preview branch:")
})
Expand Down

0 comments on commit 6f57e72

Please sign in to comment.