github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/hostnames/create/create.go (about) 1 package create 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "strings" 8 9 "github.com/Redstoneguy129/cli/internal/hostnames" 10 "github.com/Redstoneguy129/cli/internal/utils" 11 "github.com/Redstoneguy129/cli/pkg/api" 12 "github.com/spf13/afero" 13 ) 14 15 func Run(ctx context.Context, projectRefArg string, customHostname string, includeRawOutput bool, fsys afero.Fs) error { 16 // 1. Sanity checks. 17 projectRef := projectRefArg 18 hostname := strings.TrimSpace(customHostname) 19 { 20 if len(projectRefArg) == 0 { 21 ref, err := utils.LoadProjectRef(fsys) 22 if err != nil { 23 return err 24 } 25 projectRef = ref 26 } else if !utils.ProjectRefPattern.MatchString(projectRef) { 27 return errors.New("Invalid project ref format. Must be like `abcdefghijklmnopqrst`.") 28 } 29 if len(hostname) == 0 { 30 return errors.New("non-empty custom hostname expected") 31 } 32 // we verify that a CNAME is set as it simplifies the checks used for verifying ownership 33 err := hostnames.VerifyCNAME(ctx, projectRef, hostname) 34 if err != nil { 35 return err 36 } 37 } 38 39 // 2. create custom hostname 40 { 41 resp, err := utils.GetSupabase().CreateCustomHostnameConfigWithResponse(ctx, projectRef, api.CreateCustomHostnameConfigJSONRequestBody{ 42 CustomHostname: hostname, 43 }) 44 if err != nil { 45 return err 46 } 47 if resp.JSON201 == nil { 48 return errors.New("failed to create custom hostname config: " + string(resp.Body)) 49 } 50 status, err := hostnames.TranslateStatus(resp.JSON201, includeRawOutput) 51 if err != nil { 52 return err 53 } 54 fmt.Println(status) 55 return nil 56 } 57 }