|
| 1 | +package botmanagement |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + |
| 6 | + "github.com/fastly/cli/pkg/argparser" |
| 7 | + "github.com/fastly/cli/pkg/global" |
| 8 | + "github.com/fastly/go-fastly/v9/fastly" |
| 9 | + product "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" |
| 10 | + |
| 11 | + "github.com/fastly/cli/internal/productcore" |
| 12 | + "github.com/fastly/cli/pkg/api" |
| 13 | +) |
| 14 | + |
| 15 | +// EnablementHooks is a structure of dependency-injection points used |
| 16 | +// by unit tests to provide mock behaviors |
| 17 | +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ |
| 18 | + DisableFunc: func(client api.Interface, serviceID string) error { |
| 19 | + return product.Disable(client.(*fastly.Client), serviceID) |
| 20 | + }, |
| 21 | + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { |
| 22 | + return product.Enable(client.(*fastly.Client), serviceID) |
| 23 | + }, |
| 24 | + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { |
| 25 | + return product.Get(client.(*fastly.Client), serviceID) |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +// RootCommand is the parent command for all subcommands in this package. |
| 30 | +// It should be installed under the primary root command. |
| 31 | +type RootCommand struct { |
| 32 | + argparser.Base |
| 33 | + // no flags |
| 34 | +} |
| 35 | + |
| 36 | +// CommandName is the string to be used to invoke this command |
| 37 | +const CommandName = "bot_management" |
| 38 | + |
| 39 | +// NewRootCommand returns a new command registered in the parent. |
| 40 | +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { |
| 41 | + var c RootCommand |
| 42 | + c.Globals = g |
| 43 | + c.CmdClause = parent.Command(CommandName, "Enable and disable the Bot Management product") |
| 44 | + return &c |
| 45 | +} |
| 46 | + |
| 47 | +// Exec implements the command interface. |
| 48 | +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { |
| 49 | + panic("unreachable") |
| 50 | +} |
| 51 | + |
| 52 | +// EnableCommand calls the Fastly API to disable the product. |
| 53 | +type EnableCommand struct { |
| 54 | + productcore.Enable[*product.EnableOutput] |
| 55 | +} |
| 56 | + |
| 57 | +// NewEnableCommand returns a usable command registered under the parent. |
| 58 | +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { |
| 59 | + c := EnableCommand{} |
| 60 | + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) |
| 61 | + return &c |
| 62 | +} |
| 63 | + |
| 64 | +// Exec invokes the application logic for the command. |
| 65 | +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { |
| 66 | + return cmd.Enable.Exec(out) |
| 67 | +} |
| 68 | + |
| 69 | +// DisableCommand calls the Fastly API to disable the product. |
| 70 | +type DisableCommand struct { |
| 71 | + productcore.Disable[*product.EnableOutput] |
| 72 | +} |
| 73 | + |
| 74 | +// NewDisableCommand returns a usable command registered under the parent. |
| 75 | +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { |
| 76 | + c := DisableCommand{} |
| 77 | + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) |
| 78 | + return &c |
| 79 | +} |
| 80 | + |
| 81 | +// Exec invokes the application logic for the command. |
| 82 | +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { |
| 83 | + return cmd.Disable.Exec(out) |
| 84 | +} |
| 85 | + |
| 86 | +// StatusCommand calls the Fastly API to get the enablement status of the product. |
| 87 | +type StatusCommand struct { |
| 88 | + productcore.Status[*product.EnableOutput] |
| 89 | +} |
| 90 | + |
| 91 | +// NewStatusCommand returns a usable command registered under the parent. |
| 92 | +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { |
| 93 | + c := StatusCommand{} |
| 94 | + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) |
| 95 | + return &c |
| 96 | +} |
| 97 | + |
| 98 | +// Exec invokes the application logic for the command. |
| 99 | +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { |
| 100 | + return cmd.Status.Exec(out) |
| 101 | +} |
0 commit comments