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

     1  package subscription
     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 subscription. A subscription cannot be destroyed if there are domains in the TLS enabled state").Alias("remove")
    17  	c.Globals = g
    18  
    19  	// Required.
    20  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS subscription").Required().StringVar(&c.id)
    21  
    22  	// Optional.
    23  	c.CmdClause.Flag("force", "A flag that allows you to edit and delete a subscription with active domains").Action(c.force.Set).BoolVar(&c.force.Value)
    24  
    25  	return &c
    26  }
    27  
    28  // DeleteCommand calls the Fastly API to delete an appropriate resource.
    29  type DeleteCommand struct {
    30  	argparser.Base
    31  
    32  	force argparser.OptionalBool
    33  	id    string
    34  }
    35  
    36  // Exec invokes the application logic for the command.
    37  func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
    38  	input := c.constructInput()
    39  
    40  	err := c.Globals.APIClient.DeleteTLSSubscription(input)
    41  	if err != nil {
    42  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    43  			"TLS Subscription ID": c.id,
    44  			"Force":               c.force.Value,
    45  		})
    46  		return err
    47  	}
    48  
    49  	text.Success(out, "Deleted TLS Subscription '%s' (force: %t)", c.id, c.force.Value)
    50  	return nil
    51  }
    52  
    53  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    54  func (c *DeleteCommand) constructInput() *fastly.DeleteTLSSubscriptionInput {
    55  	var input fastly.DeleteTLSSubscriptionInput
    56  
    57  	input.ID = c.id
    58  
    59  	if c.force.WasSet {
    60  		input.Force = c.force.Value
    61  	}
    62  
    63  	return &input
    64  }