github.com/supabase/cli@v1.168.1/internal/branches/create/create.go (about)

     1  package create
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/go-errors/errors"
     8  	"github.com/spf13/afero"
     9  	"github.com/supabase/cli/internal/gen/keys"
    10  	"github.com/supabase/cli/internal/utils"
    11  	"github.com/supabase/cli/internal/utils/flags"
    12  	"github.com/supabase/cli/pkg/api"
    13  )
    14  
    15  func Run(ctx context.Context, name, region string, fsys afero.Fs) error {
    16  	gitBranch := keys.GetGitBranchOrDefault("", fsys)
    17  	if len(name) == 0 && len(gitBranch) > 0 {
    18  		title := fmt.Sprintf("Do you want to create a branch named %s?", utils.Aqua(gitBranch))
    19  		if shouldCreate := utils.NewConsole().PromptYesNo(title, true); !shouldCreate {
    20  			return context.Canceled
    21  		}
    22  		name = gitBranch
    23  	}
    24  
    25  	resp, err := utils.GetSupabase().CreateBranchWithResponse(ctx, flags.ProjectRef, api.CreateBranchJSONRequestBody{
    26  		BranchName: name,
    27  		GitBranch:  &gitBranch,
    28  		Region:     &region,
    29  	})
    30  	if err != nil {
    31  		return errors.Errorf("failed to create preview branch: %w", err)
    32  	}
    33  
    34  	if resp.JSON201 == nil {
    35  		return errors.New("Unexpected error creating preview branch: " + string(resp.Body))
    36  	}
    37  
    38  	fmt.Println("Created preview branch:", resp.JSON201.Id)
    39  	return nil
    40  }