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

     1  package activate
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/spf13/afero"
    10  	"github.com/Redstoneguy129/cli/internal/utils"
    11  	"github.com/Redstoneguy129/cli/pkg/api"
    12  )
    13  
    14  func Run(ctx context.Context, projectRefArg string, desiredSubdomain string, fsys afero.Fs) error {
    15  	// 1. Sanity checks.
    16  	projectRef := projectRefArg
    17  	subdomain := strings.TrimSpace(desiredSubdomain)
    18  	{
    19  		if len(projectRefArg) == 0 {
    20  			ref, err := utils.LoadProjectRef(fsys)
    21  			if err != nil {
    22  				return err
    23  			}
    24  			projectRef = ref
    25  		} else if !utils.ProjectRefPattern.MatchString(projectRef) {
    26  			return errors.New("Invalid project ref format. Must be like `abcdefghijklmnopqrst`.")
    27  		}
    28  		if len(subdomain) == 0 {
    29  			return errors.New("non-empty vanity subdomain expected")
    30  		}
    31  	}
    32  
    33  	// 2. create vanity subdomain
    34  	{
    35  		resp, err := utils.GetSupabase().ActivateVanitySubdomainPleaseWithResponse(ctx, projectRef, api.ActivateVanitySubdomainPleaseJSONRequestBody{
    36  			VanitySubdomain: subdomain,
    37  		})
    38  		if err != nil {
    39  			return err
    40  		}
    41  		if resp.JSON201 == nil {
    42  			return errors.New("failed to create vanity subdomain config: " + string(resp.Body))
    43  		}
    44  		fmt.Printf("Activated vanity subdomain at %s\n", resp.JSON201.CustomDomain)
    45  		return nil
    46  	}
    47  }