|
| 1 | +package redaction |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/fastly/go-fastly/v12/fastly" |
| 9 | + |
| 10 | + "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions" |
| 11 | + |
| 12 | + "github.com/fastly/cli/pkg/argparser" |
| 13 | + fsterr "github.com/fastly/cli/pkg/errors" |
| 14 | + "github.com/fastly/cli/pkg/global" |
| 15 | + "github.com/fastly/cli/pkg/text" |
| 16 | +) |
| 17 | + |
| 18 | +// DeleteCommand calls the Fastly API to delete a workspace. |
| 19 | +type DeleteCommand struct { |
| 20 | + argparser.Base |
| 21 | + argparser.JSONOutput |
| 22 | + |
| 23 | + // Required. |
| 24 | + workspaceID argparser.OptionalWorkspaceID |
| 25 | + redactionID string |
| 26 | +} |
| 27 | + |
| 28 | +// NewDeleteCommand returns a usable command registered under the parent. |
| 29 | +func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand { |
| 30 | + c := DeleteCommand{ |
| 31 | + Base: argparser.Base{ |
| 32 | + Globals: g, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + c.CmdClause = parent.Command("delete", "Delete a redaction") |
| 37 | + |
| 38 | + // Required. |
| 39 | + c.CmdClause.Flag("redaction-id", "Workspace ID").Required().StringVar(&c.redactionID) |
| 40 | + c.RegisterFlag(argparser.StringFlagOpts{ |
| 41 | + Name: argparser.FlagNGWAFWorkspaceID, |
| 42 | + Description: argparser.FlagNGWAFWorkspaceIDDesc, |
| 43 | + Dst: &c.workspaceID.Value, |
| 44 | + Action: c.workspaceID.Set, |
| 45 | + }) |
| 46 | + |
| 47 | + // Optional. |
| 48 | + c.RegisterFlagBool(c.JSONFlag()) |
| 49 | + |
| 50 | + return &c |
| 51 | +} |
| 52 | + |
| 53 | +// Exec invokes the application logic for the command. |
| 54 | +func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { |
| 55 | + // Call Parse() to ensure that we check if workspaceID |
| 56 | + // is set or to throw the appropriate error. |
| 57 | + if err := c.workspaceID.Parse(); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + if c.Globals.Verbose() && c.JSONOutput.Enabled { |
| 62 | + return fsterr.ErrInvalidVerboseJSONCombo |
| 63 | + } |
| 64 | + |
| 65 | + fc, ok := c.Globals.APIClient.(*fastly.Client) |
| 66 | + if !ok { |
| 67 | + return errors.New("failed to convert interface to a fastly client") |
| 68 | + } |
| 69 | + |
| 70 | + err := redactions.Delete(context.TODO(), fc, &redactions.DeleteInput{ |
| 71 | + RedactionID: &c.redactionID, |
| 72 | + WorkspaceID: &c.workspaceID.Value, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + c.Globals.ErrLog.Add(err) |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + if c.JSONOutput.Enabled { |
| 80 | + o := struct { |
| 81 | + ID string `json:"id"` |
| 82 | + Deleted bool `json:"deleted"` |
| 83 | + }{ |
| 84 | + c.redactionID, |
| 85 | + true, |
| 86 | + } |
| 87 | + _, err := c.WriteJSON(out, o) |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + text.Success(out, "Deleted redaction (id: %s)", c.redactionID) |
| 92 | + return nil |
| 93 | +} |
0 commit comments