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

     1  package check
     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  	}
    29  
    30  	// 2. check if the subdomain is available
    31  	{
    32  		resp, err := utils.GetSupabase().CheckVanitySubdomainAvailabilityWithResponse(ctx, projectRef, api.CheckVanitySubdomainAvailabilityJSONRequestBody{
    33  			VanitySubdomain: subdomain,
    34  		})
    35  		if err != nil {
    36  			return err
    37  		}
    38  		if resp.JSON201 == nil {
    39  			return errors.New("failed to check subdomain availability: " + string(resp.Body))
    40  		}
    41  		fmt.Printf("Subdomain %s available: %+v\n", subdomain, resp.JSON201.Available)
    42  		return nil
    43  	}
    44  }