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

     1  package secretstore
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/fastly/go-fastly/v9/fastly"
     7  
     8  	"github.com/fastly/cli/pkg/argparser"
     9  	fsterr "github.com/fastly/cli/pkg/errors"
    10  	"github.com/fastly/cli/pkg/global"
    11  	"github.com/fastly/cli/pkg/text"
    12  )
    13  
    14  // NewDeleteCommand returns a usable command registered under the parent.
    15  func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand {
    16  	c := DeleteCommand{
    17  		Base: argparser.Base{
    18  			Globals: g,
    19  		},
    20  	}
    21  
    22  	c.CmdClause = parent.Command("delete", "Delete a secret store")
    23  
    24  	// Required.
    25  	c.RegisterFlag(argparser.StoreIDFlag(&c.Input.StoreID)) // --store-id
    26  
    27  	// Optional.
    28  	c.RegisterFlagBool(c.JSONFlag()) // --json
    29  
    30  	return &c
    31  }
    32  
    33  // DeleteCommand calls the Fastly API to delete an appropriate resource.
    34  type DeleteCommand struct {
    35  	argparser.Base
    36  	argparser.JSONOutput
    37  
    38  	Input fastly.DeleteSecretStoreInput
    39  }
    40  
    41  // Exec invokes the application logic for the command.
    42  func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
    43  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    44  		return fsterr.ErrInvalidVerboseJSONCombo
    45  	}
    46  
    47  	err := c.Globals.APIClient.DeleteSecretStore(&c.Input)
    48  	if err != nil {
    49  		c.Globals.ErrLog.Add(err)
    50  		return err
    51  	}
    52  
    53  	if c.JSONOutput.Enabled {
    54  		o := struct {
    55  			ID      string `json:"id"`
    56  			Deleted bool   `json:"deleted"`
    57  		}{
    58  			c.Input.StoreID,
    59  			true,
    60  		}
    61  		_, err := c.WriteJSON(out, o)
    62  		return err
    63  	}
    64  
    65  	text.Success(out, "Deleted Secret Store '%s'", c.Input.StoreID)
    66  	return nil
    67  }