github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/certificate/delete.go (about)

     1  package certificate
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/fastly/go-fastly/v9/fastly"
     7  
     8  	"github.com/fastly/cli/pkg/argparser"
     9  	"github.com/fastly/cli/pkg/global"
    10  	"github.com/fastly/cli/pkg/text"
    11  )
    12  
    13  // NewDeleteCommand returns a usable command registered under the parent.
    14  func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand {
    15  	var c DeleteCommand
    16  	c.CmdClause = parent.Command("delete", "Destroy a TLS certificate. TLS certificates already enabled for a domain cannot be destroyed").Alias("remove")
    17  	c.Globals = g
    18  
    19  	// Required.
    20  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS certificate").Required().StringVar(&c.id)
    21  
    22  	return &c
    23  }
    24  
    25  // DeleteCommand calls the Fastly API to delete an appropriate resource.
    26  type DeleteCommand struct {
    27  	argparser.Base
    28  
    29  	id string
    30  }
    31  
    32  // Exec invokes the application logic for the command.
    33  func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
    34  	input := c.constructInput()
    35  
    36  	err := c.Globals.APIClient.DeleteCustomTLSCertificate(input)
    37  	if err != nil {
    38  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    39  			"TLS Certificate ID": c.id,
    40  		})
    41  		return err
    42  	}
    43  
    44  	text.Success(out, "Deleted TLS Certificate '%s'", c.id)
    45  	return nil
    46  }
    47  
    48  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    49  func (c *DeleteCommand) constructInput() *fastly.DeleteCustomTLSCertificateInput {
    50  	var input fastly.DeleteCustomTLSCertificateInput
    51  
    52  	input.ID = c.id
    53  
    54  	return &input
    55  }