github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/hostnames/activate/activate.go (about)

     1  package activate
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/Redstoneguy129/cli/internal/hostnames"
     9  	"github.com/Redstoneguy129/cli/internal/utils"
    10  	"github.com/spf13/afero"
    11  )
    12  
    13  func Run(ctx context.Context, projectRefArg string, includeRawOutput bool, fsys afero.Fs) error {
    14  	// 1. Sanity checks.
    15  	projectRef := projectRefArg
    16  	{
    17  		if len(projectRefArg) == 0 {
    18  			ref, err := utils.LoadProjectRef(fsys)
    19  			if err != nil {
    20  				return err
    21  			}
    22  			projectRef = ref
    23  		} else if !utils.ProjectRefPattern.MatchString(projectRef) {
    24  			return errors.New("Invalid project ref format. Must be like `abcdefghijklmnopqrst`.")
    25  		}
    26  		resp, err := hostnames.GetCustomHostnameConfig(ctx, projectRef)
    27  		if err != nil {
    28  			return err
    29  		}
    30  		err = hostnames.VerifyCNAME(ctx, projectRef, resp.JSON200.CustomHostname)
    31  		if err != nil {
    32  			return err
    33  		}
    34  	}
    35  
    36  	// 2. activate custom hostname config
    37  	{
    38  		resp, err := utils.GetSupabase().ActivateWithResponse(ctx, projectRef)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		if resp.JSON201 == nil {
    43  			return errors.New("failed to activate custom hostname config: " + string(resp.Body))
    44  		}
    45  		status, err := hostnames.TranslateStatus(resp.JSON201, includeRawOutput)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		fmt.Println(status)
    50  		return nil
    51  	}
    52  }