github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/prompts/domain/helpers.go (about)

     1  package domainPrompts
     2  
     3  import (
     4  	structureSpec "github.com/taubyte/go-specs/structure"
     5  	domainFlags "github.com/taubyte/tau-cli/flags/domain"
     6  	domainLib "github.com/taubyte/tau-cli/lib/domain"
     7  	"github.com/taubyte/tau-cli/prompts"
     8  	"github.com/urfave/cli/v2"
     9  )
    10  
    11  func certificate(ctx *cli.Context, domain *structureSpec.Domain, new bool) (err error) {
    12  	defaultCertType := domainFlags.CertTypeAuto
    13  	if !new {
    14  		defaultCertType = domain.CertType
    15  	}
    16  
    17  	domain.CertType, err = getCertType(ctx, defaultCertType)
    18  	if err != nil {
    19  		return
    20  	}
    21  
    22  	if domain.CertType == domainFlags.CertTypeInline {
    23  		if new {
    24  			domain.CertFile = GetOrRequireACertificate(ctx, CertificateFilePrompt)
    25  			domain.KeyFile = GetOrRequireAKey(ctx, KeyFilePrompt)
    26  		} else {
    27  			domain.CertFile = GetOrRequireACertificate(ctx, CertificateFilePrompt, domain.CertFile)
    28  			domain.KeyFile = GetOrRequireAKey(ctx, KeyFilePrompt, domain.KeyFile)
    29  		}
    30  
    31  		var (
    32  			cert []byte
    33  			key  []byte
    34  		)
    35  		cert, key, err = domainLib.ValidateCertificateKeyPairAndHostname(domain)
    36  		if err != nil {
    37  			// TODO verbose
    38  			return
    39  		}
    40  
    41  		domain.CertFile = string(cert)
    42  		domain.KeyFile = string(key)
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func getCertType(ctx *cli.Context, defaultCertType string) (certType string, err error) {
    49  	certType, isSet, err := domainFlags.GetCertType(ctx)
    50  	if err != nil {
    51  		return
    52  	}
    53  
    54  	if !isSet {
    55  		certType, err = prompts.SelectInterface(domainFlags.CertTypeOptions, CertificateTypePrompt, defaultCertType)
    56  		if err != nil {
    57  			return
    58  		}
    59  	}
    60  
    61  	return
    62  }