github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/commands/resources/domain/edit.go (about)

     1  package domain
     2  
     3  import (
     4  	structureSpec "github.com/taubyte/go-specs/structure"
     5  	resources "github.com/taubyte/tau-cli/cli/commands/resources/common"
     6  	"github.com/taubyte/tau-cli/cli/common"
     7  	"github.com/taubyte/tau-cli/flags"
     8  	domainFlags "github.com/taubyte/tau-cli/flags/domain"
     9  	domainI18n "github.com/taubyte/tau-cli/i18n/domain"
    10  	domainLib "github.com/taubyte/tau-cli/lib/domain"
    11  	domainPrompts "github.com/taubyte/tau-cli/prompts/domain"
    12  	domainTable "github.com/taubyte/tau-cli/table/domain"
    13  	"github.com/urfave/cli/v2"
    14  )
    15  
    16  func (link) Edit() common.Command {
    17  	var previousFQDN string
    18  
    19  	return (&resources.Edit[*structureSpec.Domain]{
    20  		I18nEdited:      domainI18n.Edited,
    21  		PromptsEdit:     domainPrompts.Edit,
    22  		TableConfirm:    domainTable.Confirm,
    23  		PromptsEditThis: domainPrompts.EditThis,
    24  		UniqueFlags: flags.Combine(
    25  			domainFlags.FQDN,
    26  			domainFlags.CertType,
    27  			domainFlags.Certificate,
    28  			domainFlags.Key,
    29  		),
    30  
    31  		// Wrapping methods to handle registration
    32  		PromptsGetOrSelect: func(ctx *cli.Context) (*structureSpec.Domain, error) {
    33  			resource, err := domainPrompts.GetOrSelect(ctx)
    34  			if err != nil {
    35  				return nil, err
    36  			}
    37  
    38  			previousFQDN = resource.Fqdn
    39  			return resource, err
    40  		},
    41  
    42  		LibSet: func(resource *structureSpec.Domain) error {
    43  			validator, err := domainLib.Set(resource)
    44  			if err != nil {
    45  				return err
    46  			}
    47  
    48  			// Skipping registration check for generated FQDN
    49  
    50  			isGeneratedFqdn, err := domainLib.IsAGeneratedFQDN(resource.Fqdn)
    51  			if err != nil {
    52  				return err
    53  			}
    54  			if isGeneratedFqdn {
    55  				return nil
    56  			}
    57  
    58  			// Check if fqdn has changed and Validate the fqdn provided if it has
    59  			if previousFQDN != resource.Fqdn {
    60  				clientResponse, err := validator.ValidateFQDN(resource.Fqdn)
    61  				if err != nil {
    62  					return err
    63  				}
    64  				domainTable.Registered(resource.Fqdn, clientResponse)
    65  			}
    66  
    67  			return nil
    68  		},
    69  	}).Default()
    70  }