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

     1  package dictionaryentry
     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  // DeleteCommand calls the Fastly API to delete a service.
    14  type DeleteCommand struct {
    15  	argparser.Base
    16  	Input       fastly.DeleteDictionaryItemInput
    17  	serviceName argparser.OptionalServiceNameID
    18  }
    19  
    20  // NewDeleteCommand returns a usable command registered under the parent.
    21  func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand {
    22  	c := DeleteCommand{
    23  		Base: argparser.Base{
    24  			Globals: g,
    25  		},
    26  	}
    27  	c.CmdClause = parent.Command("delete", "Delete an item from a Fastly edge dictionary")
    28  
    29  	// Required.
    30  	c.CmdClause.Flag("dictionary-id", "Dictionary ID").Required().StringVar(&c.Input.DictionaryID)
    31  	c.CmdClause.Flag("key", "Dictionary item key").Required().StringVar(&c.Input.ItemKey)
    32  
    33  	// Optional.
    34  	c.RegisterFlag(argparser.StringFlagOpts{
    35  		Name:        argparser.FlagServiceIDName,
    36  		Description: argparser.FlagServiceIDDesc,
    37  		Dst:         &g.Manifest.Flag.ServiceID,
    38  		Short:       's',
    39  	})
    40  	c.RegisterFlag(argparser.StringFlagOpts{
    41  		Action:      c.serviceName.Set,
    42  		Name:        argparser.FlagServiceName,
    43  		Description: argparser.FlagServiceDesc,
    44  		Dst:         &c.serviceName.Value,
    45  	})
    46  	return &c
    47  }
    48  
    49  // Exec invokes the application logic for the command.
    50  func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
    51  	serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	if c.Globals.Verbose() {
    56  		argparser.DisplayServiceID(serviceID, flag, source, out)
    57  	}
    58  
    59  	c.Input.ServiceID = serviceID
    60  	err = c.Globals.APIClient.DeleteDictionaryItem(&c.Input)
    61  	if err != nil {
    62  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    63  			"Service ID": serviceID,
    64  		})
    65  		return err
    66  	}
    67  
    68  	text.Success(out, "Deleted dictionary item %s (service %s, dictionary %s)", c.Input.ItemKey, c.Input.ServiceID, c.Input.DictionaryID)
    69  	return nil
    70  }