github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/aclentry/delete.go (about) 1 package aclentry 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 c := DeleteCommand{ 16 Base: argparser.Base{ 17 Globals: g, 18 }, 19 } 20 c.CmdClause = parent.Command("delete", "Delete an ACL entry from a specified ACL").Alias("remove") 21 22 // Required. 23 c.CmdClause.Flag("acl-id", "Alphanumeric string identifying a ACL").Required().StringVar(&c.aclID) 24 c.CmdClause.Flag("id", "Alphanumeric string identifying an ACL Entry").Required().StringVar(&c.id) 25 26 // Optional. 27 c.RegisterFlag(argparser.StringFlagOpts{ 28 Name: argparser.FlagServiceIDName, 29 Description: argparser.FlagServiceIDDesc, 30 Dst: &g.Manifest.Flag.ServiceID, 31 Short: 's', 32 }) 33 c.RegisterFlag(argparser.StringFlagOpts{ 34 Action: c.serviceName.Set, 35 Name: argparser.FlagServiceName, 36 Description: argparser.FlagServiceDesc, 37 Dst: &c.serviceName.Value, 38 }) 39 40 return &c 41 } 42 43 // DeleteCommand calls the Fastly API to delete an appropriate resource. 44 type DeleteCommand struct { 45 argparser.Base 46 47 aclID string 48 id string 49 serviceName argparser.OptionalServiceNameID 50 } 51 52 // Exec invokes the application logic for the command. 53 func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { 54 serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) 55 if err != nil { 56 return err 57 } 58 if c.Globals.Verbose() { 59 argparser.DisplayServiceID(serviceID, flag, source, out) 60 } 61 62 input := c.constructInput(serviceID) 63 err = c.Globals.APIClient.DeleteACLEntry(input) 64 if err != nil { 65 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 66 "Service ID": serviceID, 67 }) 68 return err 69 } 70 71 text.Success(out, "Deleted ACL entry '%s' (service: %s)", input.EntryID, serviceID) 72 return nil 73 } 74 75 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 76 func (c *DeleteCommand) constructInput(serviceID string) *fastly.DeleteACLEntryInput { 77 var input fastly.DeleteACLEntryInput 78 79 input.ACLID = c.aclID 80 input.EntryID = c.id 81 input.ServiceID = serviceID 82 83 return &input 84 }