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

     1  package create
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/go-errors/errors"
     9  	"github.com/spf13/afero"
    10  	"github.com/supabase/cli/internal/hostnames"
    11  	"github.com/supabase/cli/internal/utils"
    12  	"github.com/supabase/cli/pkg/api"
    13  )
    14  
    15  func Run(ctx context.Context, projectRef string, customHostname string, includeRawOutput bool, fsys afero.Fs) error {
    16  	// 1. Sanity checks.
    17  	hostname := strings.TrimSpace(customHostname)
    18  	{
    19  		if len(hostname) == 0 {
    20  			return errors.New("non-empty custom hostname expected")
    21  		}
    22  		// we verify that a CNAME is set as it simplifies the checks used for verifying ownership
    23  		err := hostnames.VerifyCNAME(ctx, projectRef, hostname)
    24  		if err != nil {
    25  			return err
    26  		}
    27  	}
    28  
    29  	// 2. create custom hostname
    30  	{
    31  		resp, err := utils.GetSupabase().CreateCustomHostnameConfigWithResponse(ctx, projectRef, api.CreateCustomHostnameConfigJSONRequestBody{
    32  			CustomHostname: hostname,
    33  		})
    34  		if err != nil {
    35  			return errors.Errorf("failed to create custom hostname: %w", err)
    36  		}
    37  		if resp.JSON201 == nil {
    38  			return errors.New("failed to create custom hostname config: " + string(resp.Body))
    39  		}
    40  		status, err := hostnames.TranslateStatus(resp.JSON201, includeRawOutput)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		fmt.Println(status)
    45  		return nil
    46  	}
    47  }