github.com/section/sectionctl@v1.12.3/commands/certs.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog/log"
     7  
     8  	"github.com/alecthomas/kong"
     9  	"github.com/section/sectionctl/api"
    10  )
    11  
    12  // CertsCmd manages certificates on Section
    13  type CertsCmd struct {
    14  	Renew CertsRenewCmd `cmd help:"Renew a certificate for a domain."`
    15  }
    16  
    17  // CertsRenewCmd handles renewing a certificate
    18  type CertsRenewCmd struct {
    19  	Hostname string `arg help:"The domain name to renew the cert for"`
    20  }
    21  
    22  // Run executes the command
    23  func (c *CertsRenewCmd) Run(ctx *kong.Context,logWriters *LogWriters) (err error) {
    24  	var aid int
    25  	s := NewSpinner("Looking up accounts",logWriters)
    26  	s.Start()
    27  
    28  	as, err := api.Accounts()
    29  	if err != nil {
    30  		return fmt.Errorf("unable to look up accounts: %w", err)
    31  	}
    32  
    33  	for _, a := range as {
    34  		ds, err := api.Domains(a.ID)
    35  		if err != nil {
    36  			return fmt.Errorf("unable to look up domains under account ID %d: %w", a.ID, err)
    37  		}
    38  		for _, d := range ds {
    39  			if d.DomainName == c.Hostname {
    40  				aid = a.ID
    41  				s.Stop()
    42  				break
    43  			}
    44  		}
    45  	}
    46  	s.Stop()
    47  
    48  	if aid == 0 {
    49  		return fmt.Errorf("unable to find the domain '%s' under any of your accounts.\n\nTry running `sectionctl domains` to see all your domains", c.Hostname)
    50  	}
    51  
    52  	s = NewSpinner(fmt.Sprintf("Renewing cert for %s", c.Hostname),logWriters)
    53  	s.Start()
    54  
    55  	resp, err := api.DomainsRenewCert(aid, c.Hostname)
    56  	s.Stop()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	log.Info().Msg(fmt.Sprintf("\nSuccess: %s\n", resp.Message))
    62  
    63  	return err
    64  }