From c8fb9965da097d652176138ef0861725a07e6867 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 9 Dec 2024 17:20:54 -0500 Subject: [PATCH 1/9] feat(product): Add 'product' commands for each Fastly product. Adds 'enable', 'disable', and 'status' commands for each product previously supported by the 'products' command (which is now deprecated). Also adds 'configure' commands for products which support configuration (DDoS Protection and Next-Gen WAF). --- pkg/commands/commands.go | 12 ++ .../product/bot_management/disable.go | 77 +++++++++++ pkg/commands/product/bot_management/doc.go | 3 + pkg/commands/product/bot_management/enable.go | 77 +++++++++++ .../product/bot_management/product_test.go | 125 ++++++++++++++++++ pkg/commands/product/bot_management/root.go | 31 +++++ pkg/commands/product/bot_management/status.go | 112 ++++++++++++++++ pkg/commands/product/doc.go | 3 + pkg/commands/product/root.go | 31 +++++ 9 files changed, 471 insertions(+) create mode 100644 pkg/commands/product/bot_management/disable.go create mode 100644 pkg/commands/product/bot_management/doc.go create mode 100644 pkg/commands/product/bot_management/enable.go create mode 100644 pkg/commands/product/bot_management/product_test.go create mode 100644 pkg/commands/product/bot_management/root.go create mode 100644 pkg/commands/product/bot_management/status.go create mode 100644 pkg/commands/product/doc.go create mode 100644 pkg/commands/product/root.go diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index b6868ed8b..7f147d160 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -58,6 +58,8 @@ import ( "github.com/fastly/cli/pkg/commands/objectstorage" "github.com/fastly/cli/pkg/commands/objectstorage/accesskeys" "github.com/fastly/cli/pkg/commands/pop" + "github.com/fastly/cli/pkg/commands/product" + "github.com/fastly/cli/pkg/commands/product/bot_management" "github.com/fastly/cli/pkg/commands/products" "github.com/fastly/cli/pkg/commands/profile" "github.com/fastly/cli/pkg/commands/purge" @@ -399,6 +401,11 @@ func Define( // nolint:revive // function-length objectStorageAccesskeysGet := accesskeys.NewGetCommand(objectStorageAccesskeysRoot.CmdClause, data) objectStorageAccesskeysList := accesskeys.NewListCommand(objectStorageAccesskeysRoot.CmdClause, data) popCmdRoot := pop.NewRootCommand(app, data) + productCmdRoot := product.NewRootCommand(app, data) + productBotManagementCmdRoot := bot_management.NewRootCommand(productCmdRoot.CmdClause, data) + productBotManagementDisable := bot_management.NewDisableCommand(productBotManagementCmdRoot.CmdClause, data) + productBotManagementEnable := bot_management.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data) + productBotManagementStatus := bot_management.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data) productsCmdRoot := products.NewRootCommand(app, data) profileCmdRoot := profile.NewRootCommand(app, data) profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, data, ssoCmdRoot) @@ -817,6 +824,11 @@ func Define( // nolint:revive // function-length objectStorageAccesskeysGet, objectStorageAccesskeysList, popCmdRoot, + productCmdRoot, + productBotManagementCmdRoot, + productBotManagementDisable, + productBotManagementEnable, + productBotManagementStatus, productsCmdRoot, profileCmdRoot, profileCreate, diff --git a/pkg/commands/product/bot_management/disable.go b/pkg/commands/product/bot_management/disable.go new file mode 100644 index 000000000..e67dacc33 --- /dev/null +++ b/pkg/commands/product/bot_management/disable.go @@ -0,0 +1,77 @@ +package bot_management + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + + "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/manifest" + "github.com/fastly/cli/pkg/text" +) + +// DisableFn is a dependency-injection point for unit tests to provide +// a mock implementation of the API operation. +var DisableFn = func(client api.Interface, serviceID string) error { + return bot_management.Disable(client.(*fastly.Client), serviceID) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + argparser.Base + Manifest manifest.Data + + serviceName argparser.OptionalServiceNameID +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{ + Base: argparser.Base{ + Globals: g, + }, + } + c.CmdClause = parent.Command("disable", "Disable the "+bot_management.ProductName+" product") + + // Optional. + c.RegisterFlag(argparser.StringFlagOpts{ + Name: argparser.FlagServiceIDName, + Description: argparser.FlagServiceIDDesc, + Dst: &g.Manifest.Flag.ServiceID, + Short: 's', + }) + c.RegisterFlag(argparser.StringFlagOpts{ + Action: c.serviceName.Set, + Name: argparser.FlagServiceName, + Description: argparser.FlagServiceNameDesc, + Dst: &c.serviceName.Value, + }) + return &c +} + +// Exec invokes the application logic for the command. +func (c *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + if c.Globals.Verbose() { + argparser.DisplayServiceID(serviceID, flag, source, out) + } + + err = DisableFn(c.Globals.APIClient, serviceID) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + text.Success(out, + "Disabled "+bot_management.ProductName+" on service %s", serviceID) + + return nil +} diff --git a/pkg/commands/product/bot_management/doc.go b/pkg/commands/product/bot_management/doc.go new file mode 100644 index 000000000..90a800a0c --- /dev/null +++ b/pkg/commands/product/bot_management/doc.go @@ -0,0 +1,3 @@ +// Package bot_management contains commands to enable and disable the +// Fastly Bot Management product. +package bot_management diff --git a/pkg/commands/product/bot_management/enable.go b/pkg/commands/product/bot_management/enable.go new file mode 100644 index 000000000..b93a4a342 --- /dev/null +++ b/pkg/commands/product/bot_management/enable.go @@ -0,0 +1,77 @@ +package bot_management + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + + "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/manifest" + "github.com/fastly/cli/pkg/text" +) + +// EnableFn is a dependency-injection point for unit tests to provide +// a mock implementation of the API operation. +var EnableFn = func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { + return bot_management.Enable(client.(*fastly.Client), serviceID) +} + +// EnableCommand calls the Fastly API to enable the product. +type EnableCommand struct { + argparser.Base + Manifest manifest.Data + + serviceName argparser.OptionalServiceNameID +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{ + Base: argparser.Base{ + Globals: g, + }, + } + c.CmdClause = parent.Command("enable", "Enable the "+bot_management.ProductName+" product") + + // Optional. + c.RegisterFlag(argparser.StringFlagOpts{ + Name: argparser.FlagServiceIDName, + Description: argparser.FlagServiceIDDesc, + Dst: &g.Manifest.Flag.ServiceID, + Short: 's', + }) + c.RegisterFlag(argparser.StringFlagOpts{ + Action: c.serviceName.Set, + Name: argparser.FlagServiceName, + Description: argparser.FlagServiceNameDesc, + Dst: &c.serviceName.Value, + }) + return &c +} + +// Exec invokes the application logic for the command. +func (c *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + if c.Globals.Verbose() { + argparser.DisplayServiceID(serviceID, flag, source, out) + } + + _, err = EnableFn(c.Globals.APIClient, serviceID) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + text.Success(out, + "Enabled "+bot_management.ProductName+" on service %s", serviceID) + + return nil +} diff --git a/pkg/commands/product/bot_management/product_test.go b/pkg/commands/product/bot_management/product_test.go new file mode 100644 index 000000000..55f107c7c --- /dev/null +++ b/pkg/commands/product/bot_management/product_test.go @@ -0,0 +1,125 @@ +package bot_management_test + +import ( + "testing" + + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + + "github.com/fastly/cli/pkg/api" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/bot_management" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/testutil" +) + +func TestProductEnablement(t *testing.T) { + scenarios := []testutil.CLIScenario{ + { + Name: "validate missing Service ID: enable", + Args: "enable", + WantError: "error reading service: no service ID found", + }, + { + Name: "validate missing Service ID: disable", + Args: "enable", + WantError: "error reading service: no service ID found", + }, + { + Name: "validate missing Service ID: status", + Args: "enable", + WantError: "error reading service: no service ID found", + }, + { + Name: "validate invalid json/verbose flag combo: status", + Args: "status --service-id 123 --json --verbose", + WantError: "invalid flag combination, --verbose and --json", + }, + { + Name: "validate success for enabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + return nil, nil + } + }, + Args: "enable --service-id 123", + WantOutput: "SUCCESS: Enabled " + bot_management.ProductName + " on service 123", + }, + { + Name: "validate failure for enabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + return nil, testutil.Err + } + }, + Args: "enable --service-id 123", + WantError: "test error", + }, + { + Name: "validate success for disabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.DisableFn = func(_ api.Interface, _ string) error { + return nil + } + }, + Args: "disable --service-id 123", + WantOutput: "SUCCESS: Disabled " + bot_management.ProductName + " on service 123", + }, + { + Name: "validate failure for disabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.DisableFn = func(_ api.Interface, _ string) error { + return testutil.Err + } + }, + Args: "disable --service-id 123", + WantError: "test error", + }, + { + Name: "validate regular status output for enabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + return nil, nil + } + }, + Args: "status --service-id 123", + WantOutput: "INFO: " + bot_management.ProductName + " is enabled on service 123", + }, + { + Name: "validate JSON status output for enabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + return nil, nil + } + }, + Args: "status --service-id 123 --json", + WantOutput: "{\n \"enabled\": true\n}", + }, + { + Name: "validate regular status output for disabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service + return nil, &fastly.HTTPError{StatusCode: 400} + } + }, + Args: "status --service-id 123", + WantOutput: "INFO: " + bot_management.ProductName + " is disabled on service 123", + }, + { + Name: "validate JSON status output for disabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service + return nil, &fastly.HTTPError{StatusCode: 400} + } + }, + Args: "status --service-id 123 --json", + WantOutput: "{\n \"enabled\": false\n}", + }, + } + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName}, scenarios) +} diff --git a/pkg/commands/product/bot_management/root.go b/pkg/commands/product/bot_management/root.go new file mode 100644 index 000000000..20437c2ad --- /dev/null +++ b/pkg/commands/product/bot_management/root.go @@ -0,0 +1,31 @@ +package bot_management + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "bot_management" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Bot Management product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/bot_management/status.go b/pkg/commands/product/bot_management/status.go new file mode 100644 index 000000000..8202696d8 --- /dev/null +++ b/pkg/commands/product/bot_management/status.go @@ -0,0 +1,112 @@ +package bot_management + +import ( + "errors" + "io" + + fsterr "github.com/fastly/cli/pkg/errors" + + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + + "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/manifest" + "github.com/fastly/cli/pkg/text" +) + +// GetFn is a dependency-injection point for unit tests to provide +// a mock implementation of the API operation. +var GetFn = func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { + return bot_management.Get(client.(*fastly.Client), serviceID) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + argparser.Base + argparser.JSONOutput + Manifest manifest.Data + + serviceName argparser.OptionalServiceNameID +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{ + Base: argparser.Base{ + Globals: g, + }, + } + c.CmdClause = parent.Command("status", "Get the enablement status of the "+bot_management.ProductName+" product") + + // Optional. + c.RegisterFlagBool(c.JSONFlag()) // --json + c.RegisterFlag(argparser.StringFlagOpts{ + Name: argparser.FlagServiceIDName, + Description: argparser.FlagServiceIDDesc, + Dst: &g.Manifest.Flag.ServiceID, + Short: 's', + }) + c.RegisterFlag(argparser.StringFlagOpts{ + Action: c.serviceName.Set, + Name: argparser.FlagServiceName, + Description: argparser.FlagServiceNameDesc, + Dst: &c.serviceName.Value, + }) + return &c +} + +type status struct { + Enabled bool `json:"enabled"` +} + +// Exec invokes the application logic for the command. +func (c *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + if c.Globals.Verbose() && c.JSONOutput.Enabled { + return fsterr.ErrInvalidVerboseJSONCombo + } + + serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + if c.Globals.Verbose() { + argparser.DisplayServiceID(serviceID, flag, source, out) + } + + var s status + + _, err = GetFn(c.Globals.APIClient, serviceID) + if err != nil { + var herr *fastly.HTTPError + + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service; any + // other error should be reported + if !errors.As(err, &herr) || !herr.IsBadRequest() { + c.Globals.ErrLog.Add(err) + return err + } + } else { + s.Enabled = true + } + + if ok, err := c.WriteJSON(out, s); ok { + return err + } + + var msg string + if s.Enabled { + msg = "enabled" + } else { + msg = "disabled" + } + + text.Info(out, + bot_management.ProductName+" is %s on service %s", msg, serviceID) + + return nil +} diff --git a/pkg/commands/product/doc.go b/pkg/commands/product/doc.go new file mode 100644 index 000000000..e35f0ab32 --- /dev/null +++ b/pkg/commands/product/doc.go @@ -0,0 +1,3 @@ +// Package product contains commands to enable, disable, and configure +// Fastly products. +package product diff --git a/pkg/commands/product/root.go b/pkg/commands/product/root.go new file mode 100644 index 000000000..dcda72a2f --- /dev/null +++ b/pkg/commands/product/root.go @@ -0,0 +1,31 @@ +package product + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "product" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable, disable, and configure Fastly products") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} From f345e05534197604a600df731454cd05ef14f976 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Tue, 17 Dec 2024 13:43:08 -0500 Subject: [PATCH 2/9] Fix shell-completion test. --- pkg/app/run_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/app/run_test.go b/pkg/app/run_test.go index 75e1d7933..23b20e558 100644 --- a/pkg/app/run_test.go +++ b/pkg/app/run_test.go @@ -84,6 +84,7 @@ log-tail logging object-storage pops +product products profile purge From 157d0c7a65538b9ee8ed68cbd3773c3dc1597367 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Thu, 19 Dec 2024 13:02:24 -0500 Subject: [PATCH 3/9] WIP --- internal/productcore/base.go | 48 ++++++++++++++ internal/productcore/disable.go | 49 +++++++++++++++ internal/productcore/enable.go | 46 ++++++++++++++ pkg/commands/product/bot_management/common.go | 21 +++++++ .../product/bot_management/disable.go | 53 ++-------------- pkg/commands/product/bot_management/enable.go | 63 +++---------------- .../product/bot_management/product_test.go | 4 +- 7 files changed, 179 insertions(+), 105 deletions(-) create mode 100644 internal/productcore/base.go create mode 100644 internal/productcore/disable.go create mode 100644 internal/productcore/enable.go create mode 100644 pkg/commands/product/bot_management/common.go diff --git a/internal/productcore/base.go b/internal/productcore/base.go new file mode 100644 index 000000000..6eda4ecdd --- /dev/null +++ b/internal/productcore/base.go @@ -0,0 +1,48 @@ +package productcore + +import ( + "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/manifest" +) + +// Base is a base type for all product commands. +type Base struct { + argparser.Base + Manifest manifest.Data + + ServiceName argparser.OptionalServiceNameID + ProductName string +} + +// Init prepares the structure for use by the CLI core. +func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productName string) { + cmd.Globals = g + cmd.ProductName = productName + + // Optional flags. + cmd.RegisterFlag(argparser.StringFlagOpts{ + Name: argparser.FlagServiceIDName, + Description: argparser.FlagServiceIDDesc, + Dst: &g.Manifest.Flag.ServiceID, + Short: 's', + }) + cmd.RegisterFlag(argparser.StringFlagOpts{ + Action: cmd.ServiceName.Set, + Name: argparser.FlagServiceName, + Description: argparser.FlagServiceNameDesc, + Dst: &cmd.ServiceName.Value, + }) +} + +type EnablementHookFns[O any] struct { + DisableFn func(api.Interface, string) error + EnableFn func(api.Interface, string) (O, error) + GetFn func(api.Interface, string) (O, error) +} + +type ConfigurationHookFns[O, I any] struct { + GetConfigurationFn func(api.Interface, string) (O, error) + UpdateConfigurationFn func(api.Interface, string, I) (O, error) +} diff --git a/internal/productcore/disable.go b/internal/productcore/disable.go new file mode 100644 index 000000000..9c77277e4 --- /dev/null +++ b/internal/productcore/disable.go @@ -0,0 +1,49 @@ +package productcore + +import ( + "io" + "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/text" +) + +// DisableFn is the type of the function that will be used to perform +// the disablement. +type DisableFn func(api.Interface, string) error + +// Disable is a base type for all 'disable' commands. +type Disable struct { + Base +} + +// Init prepares the structure for use by the CLI core. +func (cmd *Disable) Init(parent argparser.Registerer, g *global.Data, productName string) { + cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product") + + cmd.Base.Init(parent, g, productName) +} + +// Exec executes the disablement operation. +func (cmd *Disable) Exec(out io.Writer, op DisableFn) error { + serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) + if err != nil { + cmd.Globals.ErrLog.Add(err) + return err + } + + if cmd.Globals.Verbose() { + argparser.DisplayServiceID(serviceID, flag, source, out) + } + + err = op(cmd.Globals.APIClient, serviceID) + if err != nil { + cmd.Globals.ErrLog.Add(err) + return err + } + + text.Success(out, + "Disabled "+cmd.ProductName+" on service %s", serviceID) + + return nil +} diff --git a/internal/productcore/enable.go b/internal/productcore/enable.go new file mode 100644 index 000000000..0e8a9a40a --- /dev/null +++ b/internal/productcore/enable.go @@ -0,0 +1,46 @@ +package productcore + +import ( + "io" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/text" +) + +// Enable is a base type for all 'enable' commands. +type Enable[O any] struct { + Base + hooks *HookFns[O] +} + +// Init prepares the structure for use by the CLI core. +func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *HookFns[O]) { + cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product") + cmd.hooks = hooks + + cmd.Base.Init(parent, g, productName) +} + +// Exec executes the disablement operation. +func (cmd *Enable[O]) Exec(out io.Writer) error { + serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) + if err != nil { + cmd.Globals.ErrLog.Add(err) + return err + } + + if cmd.Globals.Verbose() { + argparser.DisplayServiceID(serviceID, flag, source, out) + } + + _, err = cmd.hooks.EnableFn(cmd.Globals.APIClient, serviceID) + if err != nil { + cmd.Globals.ErrLog.Add(err) + return err + } + + text.Success(out, + "Enabled "+cmd.ProductName+" on service %s", serviceID) + + return nil +} diff --git a/pkg/commands/product/bot_management/common.go b/pkg/commands/product/bot_management/common.go new file mode 100644 index 000000000..5f73a0534 --- /dev/null +++ b/pkg/commands/product/bot_management/common.go @@ -0,0 +1,21 @@ +package bot_management + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +var EnablementHooks = productcore.EnablementHookFns[*bot_management.EnableOutput]{ + DisableFn: func(client api.Interface, serviceID string) error { + return bot_management.Disable(client.(*fastly.Client), serviceID) + }, + EnableFn: func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { + return bot_management.Enable(client.(*fastly.Client), serviceID) + }, + GetFn: func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { + return bot_management.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/bot_management/disable.go b/pkg/commands/product/bot_management/disable.go index e67dacc33..b30c8bdf3 100644 --- a/pkg/commands/product/bot_management/disable.go +++ b/pkg/commands/product/bot_management/disable.go @@ -6,11 +6,10 @@ import ( "github.com/fastly/go-fastly/v9/fastly" "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + "github.com/fastly/cli/internal/productcore" "github.com/fastly/cli/pkg/api" "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" - "github.com/fastly/cli/pkg/manifest" - "github.com/fastly/cli/pkg/text" ) // DisableFn is a dependency-injection point for unit tests to provide @@ -21,57 +20,17 @@ var DisableFn = func(client api.Interface, serviceID string) error { // DisableCommand calls the Fastly API to disable the product. type DisableCommand struct { - argparser.Base - Manifest manifest.Data - - serviceName argparser.OptionalServiceNameID + productcore.Disable } // NewDisableCommand returns a usable command registered under the parent. func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{ - Base: argparser.Base{ - Globals: g, - }, - } - c.CmdClause = parent.Command("disable", "Disable the "+bot_management.ProductName+" product") - - // Optional. - c.RegisterFlag(argparser.StringFlagOpts{ - Name: argparser.FlagServiceIDName, - Description: argparser.FlagServiceIDDesc, - Dst: &g.Manifest.Flag.ServiceID, - Short: 's', - }) - c.RegisterFlag(argparser.StringFlagOpts{ - Action: c.serviceName.Set, - Name: argparser.FlagServiceName, - Description: argparser.FlagServiceNameDesc, - Dst: &c.serviceName.Value, - }) + c := DisableCommand{} + c.Init(parent, g, bot_management.ProductName) return &c } // Exec invokes the application logic for the command. -func (c *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - if c.Globals.Verbose() { - argparser.DisplayServiceID(serviceID, flag, source, out) - } - - err = DisableFn(c.Globals.APIClient, serviceID) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - text.Success(out, - "Disabled "+bot_management.ProductName+" on service %s", serviceID) - - return nil +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out, DisableFn) } diff --git a/pkg/commands/product/bot_management/enable.go b/pkg/commands/product/bot_management/enable.go index b93a4a342..988ff5b96 100644 --- a/pkg/commands/product/bot_management/enable.go +++ b/pkg/commands/product/bot_management/enable.go @@ -3,75 +3,26 @@ package bot_management import ( "io" - "github.com/fastly/go-fastly/v9/fastly" "github.com/fastly/go-fastly/v9/fastly/products/bot_management" - "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/internal/productcore" "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" - "github.com/fastly/cli/pkg/manifest" - "github.com/fastly/cli/pkg/text" ) -// EnableFn is a dependency-injection point for unit tests to provide -// a mock implementation of the API operation. -var EnableFn = func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { - return bot_management.Enable(client.(*fastly.Client), serviceID) -} - -// EnableCommand calls the Fastly API to enable the product. +// EnableCommand calls the Fastly API to disable the product. type EnableCommand struct { - argparser.Base - Manifest manifest.Data - - serviceName argparser.OptionalServiceNameID + productcore.Enable[*bot_management.EnableOutput] } // NewEnableCommand returns a usable command registered under the parent. func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{ - Base: argparser.Base{ - Globals: g, - }, - } - c.CmdClause = parent.Command("enable", "Enable the "+bot_management.ProductName+" product") - - // Optional. - c.RegisterFlag(argparser.StringFlagOpts{ - Name: argparser.FlagServiceIDName, - Description: argparser.FlagServiceIDDesc, - Dst: &g.Manifest.Flag.ServiceID, - Short: 's', - }) - c.RegisterFlag(argparser.StringFlagOpts{ - Action: c.serviceName.Set, - Name: argparser.FlagServiceName, - Description: argparser.FlagServiceNameDesc, - Dst: &c.serviceName.Value, - }) + c := EnableCommand{} + c.Init(parent, g, bot_management.ProductName, &EnablementHooks) return &c } // Exec invokes the application logic for the command. -func (c *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - if c.Globals.Verbose() { - argparser.DisplayServiceID(serviceID, flag, source, out) - } - - _, err = EnableFn(c.Globals.APIClient, serviceID) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - text.Success(out, - "Enabled "+bot_management.ProductName+" on service %s", serviceID) - - return nil +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) } diff --git a/pkg/commands/product/bot_management/product_test.go b/pkg/commands/product/bot_management/product_test.go index 55f107c7c..7c7c23394 100644 --- a/pkg/commands/product/bot_management/product_test.go +++ b/pkg/commands/product/bot_management/product_test.go @@ -38,7 +38,7 @@ func TestProductEnablement(t *testing.T) { { Name: "validate success for enabling product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { return nil, nil } }, @@ -48,7 +48,7 @@ func TestProductEnablement(t *testing.T) { { Name: "validate failure for enabling product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { return nil, testutil.Err } }, From 26fbd4d1617af4d23157664c571d636c711e457a Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 13 Jan 2025 10:39:49 -0500 Subject: [PATCH 4/9] More WIP --- internal/productcore/base.go | 26 ++-- internal/productcore/disable.go | 27 +++-- internal/productcore/enable.go | 19 ++- internal/productcore/status.go | 70 +++++++++++ pkg/commands/commands.go | 10 +- pkg/commands/product/bot_management/common.go | 21 ---- pkg/commands/product/bot_management/doc.go | 3 - pkg/commands/product/bot_management/status.go | 112 ------------------ pkg/commands/product/botmanagement/common.go | 23 ++++ .../disable.go | 18 +-- pkg/commands/product/botmanagement/doc.go | 3 + .../enable.go | 8 +- .../product_test.go | 30 ++--- .../{bot_management => botmanagement}/root.go | 2 +- pkg/commands/product/botmanagement/status.go | 28 +++++ 15 files changed, 204 insertions(+), 196 deletions(-) create mode 100644 internal/productcore/status.go delete mode 100644 pkg/commands/product/bot_management/common.go delete mode 100644 pkg/commands/product/bot_management/doc.go delete mode 100644 pkg/commands/product/bot_management/status.go create mode 100644 pkg/commands/product/botmanagement/common.go rename pkg/commands/product/{bot_management => botmanagement}/disable.go (51%) create mode 100644 pkg/commands/product/botmanagement/doc.go rename pkg/commands/product/{bot_management => botmanagement}/enable.go (74%) rename pkg/commands/product/{bot_management => botmanagement}/product_test.go (72%) rename pkg/commands/product/{bot_management => botmanagement}/root.go (97%) create mode 100644 pkg/commands/product/botmanagement/status.go diff --git a/internal/productcore/base.go b/internal/productcore/base.go index 6eda4ecdd..e701d0017 100644 --- a/internal/productcore/base.go +++ b/internal/productcore/base.go @@ -10,6 +10,7 @@ import ( // Base is a base type for all product commands. type Base struct { argparser.Base + argparser.JSONOutput Manifest manifest.Data ServiceName argparser.OptionalServiceNameID @@ -34,15 +35,26 @@ func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productName s Description: argparser.FlagServiceNameDesc, Dst: &cmd.ServiceName.Value, }) + cmd.RegisterFlagBool(cmd.JSONFlag()) // --json } -type EnablementHookFns[O any] struct { - DisableFn func(api.Interface, string) error - EnableFn func(api.Interface, string) (O, error) - GetFn func(api.Interface, string) (O, error) +// EnablementStatus is a structure used to generate JSON output from +// the enablement-related commands +type EnablementStatus struct { + Enabled bool `json:"enabled"` } -type ConfigurationHookFns[O, I any] struct { - GetConfigurationFn func(api.Interface, string) (O, error) - UpdateConfigurationFn func(api.Interface, string, I) (O, error) +// EnablementHookFuncs is a structure of dependency-injection points +// used by unit tests to provide mock behaviors +type EnablementHookFuncs[O any] struct { + DisableFunc func(api.Interface, string) error + EnableFunc func(api.Interface, string) (O, error) + GetFunc func(api.Interface, string) (O, error) +} + +// ConfigurationHookFuncs is a structure of dependency-injection +// points by unit tests to provide mock behaviors +type ConfigurationHookFuncs[O, I any] struct { + GetConfigurationFunc func(api.Interface, string) (O, error) + UpdateConfigurationFunc func(api.Interface, string, I) (O, error) } diff --git a/internal/productcore/disable.go b/internal/productcore/disable.go index 9c77277e4..945e7f13d 100644 --- a/internal/productcore/disable.go +++ b/internal/productcore/disable.go @@ -2,30 +2,33 @@ package productcore import ( "io" - "github.com/fastly/cli/pkg/api" + fsterr "github.com/fastly/cli/pkg/errors" + "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" ) -// DisableFn is the type of the function that will be used to perform -// the disablement. -type DisableFn func(api.Interface, string) error - // Disable is a base type for all 'disable' commands. -type Disable struct { +type Disable[O any] struct { Base + hooks *EnablementHookFuncs[O] } // Init prepares the structure for use by the CLI core. -func (cmd *Disable) Init(parent argparser.Registerer, g *global.Data, productName string) { +func (cmd *Disable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product") + cmd.hooks = hooks cmd.Base.Init(parent, g, productName) } // Exec executes the disablement operation. -func (cmd *Disable) Exec(out io.Writer, op DisableFn) error { +func (cmd *Disable[O]) Exec(out io.Writer) error { + if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { + return fsterr.ErrInvalidVerboseJSONCombo + } + serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) if err != nil { cmd.Globals.ErrLog.Add(err) @@ -36,14 +39,18 @@ func (cmd *Disable) Exec(out io.Writer, op DisableFn) error { argparser.DisplayServiceID(serviceID, flag, source, out) } - err = op(cmd.Globals.APIClient, serviceID) + err = cmd.hooks.DisableFunc(cmd.Globals.APIClient, serviceID) if err != nil { cmd.Globals.ErrLog.Add(err) return err } + if ok, err := cmd.WriteJSON(out, EnablementStatus{Enabled: false}); ok { + return err + } + text.Success(out, - "Disabled "+cmd.ProductName+" on service %s", serviceID) + "Disabled %s on service %s", cmd.ProductName, serviceID) return nil } diff --git a/internal/productcore/enable.go b/internal/productcore/enable.go index 0e8a9a40a..153064be4 100644 --- a/internal/productcore/enable.go +++ b/internal/productcore/enable.go @@ -2,6 +2,7 @@ package productcore import ( "io" + fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" @@ -10,19 +11,23 @@ import ( // Enable is a base type for all 'enable' commands. type Enable[O any] struct { Base - hooks *HookFns[O] + hooks *EnablementHookFuncs[O] } // Init prepares the structure for use by the CLI core. -func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *HookFns[O]) { +func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product") cmd.hooks = hooks cmd.Base.Init(parent, g, productName) } -// Exec executes the disablement operation. +// Exec executes the enablement operation. func (cmd *Enable[O]) Exec(out io.Writer) error { + if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { + return fsterr.ErrInvalidVerboseJSONCombo + } + serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) if err != nil { cmd.Globals.ErrLog.Add(err) @@ -33,14 +38,18 @@ func (cmd *Enable[O]) Exec(out io.Writer) error { argparser.DisplayServiceID(serviceID, flag, source, out) } - _, err = cmd.hooks.EnableFn(cmd.Globals.APIClient, serviceID) + _, err = cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID) if err != nil { cmd.Globals.ErrLog.Add(err) return err } + if ok, err := cmd.WriteJSON(out, EnablementStatus{Enabled: true}); ok { + return err + } + text.Success(out, - "Enabled "+cmd.ProductName+" on service %s", serviceID) + "Enabled %s on service %s", cmd.ProductName, serviceID) return nil } diff --git a/internal/productcore/status.go b/internal/productcore/status.go new file mode 100644 index 000000000..7c7cea27e --- /dev/null +++ b/internal/productcore/status.go @@ -0,0 +1,70 @@ +package productcore + +import ( + "io" + "errors" + "github.com/fastly/go-fastly/v9/fastly" + fsterr "github.com/fastly/cli/pkg/errors" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/text" +) + +// Status is a base type for all 'status' commands. +type Status[O any] struct { + Base + hooks *EnablementHookFuncs[O] +} + +// Init prepares the structure for use by the CLI core. +func (cmd *Status[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { + cmd.CmdClause = parent.Command("status", "Get the enablement status of the "+productName+" product") + cmd.hooks = hooks + + cmd.Base.Init(parent, g, productName) +} + +// Exec executes the status operation. +func (cmd *Status[O]) Exec(out io.Writer) error { + if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { + return fsterr.ErrInvalidVerboseJSONCombo + } + + serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) + if err != nil { + cmd.Globals.ErrLog.Add(err) + return err + } + + if cmd.Globals.Verbose() { + argparser.DisplayServiceID(serviceID, flag, source, out) + } + + s := EnablementStatus{} + state := "disabled" + + _, err = cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID) + if err != nil { + var herr *fastly.HTTPError + + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service; any + // other error should be reported + if !errors.As(err, &herr) || !herr.IsBadRequest() { + cmd.Globals.ErrLog.Add(err) + return err + } + } else { + s.Enabled = true + state = "enabled" + } + + if ok, err := cmd.WriteJSON(out, s); ok { + return err + } + + text.Info(out, + "%s is %s on service %s", cmd.ProductName, state, serviceID) + + return nil +} diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 7f147d160..aa93a388c 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -59,7 +59,7 @@ import ( "github.com/fastly/cli/pkg/commands/objectstorage/accesskeys" "github.com/fastly/cli/pkg/commands/pop" "github.com/fastly/cli/pkg/commands/product" - "github.com/fastly/cli/pkg/commands/product/bot_management" + "github.com/fastly/cli/pkg/commands/product/botmanagement" "github.com/fastly/cli/pkg/commands/products" "github.com/fastly/cli/pkg/commands/profile" "github.com/fastly/cli/pkg/commands/purge" @@ -402,10 +402,10 @@ func Define( // nolint:revive // function-length objectStorageAccesskeysList := accesskeys.NewListCommand(objectStorageAccesskeysRoot.CmdClause, data) popCmdRoot := pop.NewRootCommand(app, data) productCmdRoot := product.NewRootCommand(app, data) - productBotManagementCmdRoot := bot_management.NewRootCommand(productCmdRoot.CmdClause, data) - productBotManagementDisable := bot_management.NewDisableCommand(productBotManagementCmdRoot.CmdClause, data) - productBotManagementEnable := bot_management.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data) - productBotManagementStatus := bot_management.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data) + productBotManagementCmdRoot := botmanagement.NewRootCommand(productCmdRoot.CmdClause, data) + productBotManagementDisable := botmanagement.NewDisableCommand(productBotManagementCmdRoot.CmdClause, data) + productBotManagementEnable := botmanagement.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data) + productBotManagementStatus := botmanagement.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data) productsCmdRoot := products.NewRootCommand(app, data) profileCmdRoot := profile.NewRootCommand(app, data) profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, data, ssoCmdRoot) diff --git a/pkg/commands/product/bot_management/common.go b/pkg/commands/product/bot_management/common.go deleted file mode 100644 index 5f73a0534..000000000 --- a/pkg/commands/product/bot_management/common.go +++ /dev/null @@ -1,21 +0,0 @@ -package bot_management - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/bot_management" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -var EnablementHooks = productcore.EnablementHookFns[*bot_management.EnableOutput]{ - DisableFn: func(client api.Interface, serviceID string) error { - return bot_management.Disable(client.(*fastly.Client), serviceID) - }, - EnableFn: func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { - return bot_management.Enable(client.(*fastly.Client), serviceID) - }, - GetFn: func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { - return bot_management.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/bot_management/doc.go b/pkg/commands/product/bot_management/doc.go deleted file mode 100644 index 90a800a0c..000000000 --- a/pkg/commands/product/bot_management/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package bot_management contains commands to enable and disable the -// Fastly Bot Management product. -package bot_management diff --git a/pkg/commands/product/bot_management/status.go b/pkg/commands/product/bot_management/status.go deleted file mode 100644 index 8202696d8..000000000 --- a/pkg/commands/product/bot_management/status.go +++ /dev/null @@ -1,112 +0,0 @@ -package bot_management - -import ( - "errors" - "io" - - fsterr "github.com/fastly/cli/pkg/errors" - - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/bot_management" - - "github.com/fastly/cli/pkg/api" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/cli/pkg/manifest" - "github.com/fastly/cli/pkg/text" -) - -// GetFn is a dependency-injection point for unit tests to provide -// a mock implementation of the API operation. -var GetFn = func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) { - return bot_management.Get(client.(*fastly.Client), serviceID) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - argparser.Base - argparser.JSONOutput - Manifest manifest.Data - - serviceName argparser.OptionalServiceNameID -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{ - Base: argparser.Base{ - Globals: g, - }, - } - c.CmdClause = parent.Command("status", "Get the enablement status of the "+bot_management.ProductName+" product") - - // Optional. - c.RegisterFlagBool(c.JSONFlag()) // --json - c.RegisterFlag(argparser.StringFlagOpts{ - Name: argparser.FlagServiceIDName, - Description: argparser.FlagServiceIDDesc, - Dst: &g.Manifest.Flag.ServiceID, - Short: 's', - }) - c.RegisterFlag(argparser.StringFlagOpts{ - Action: c.serviceName.Set, - Name: argparser.FlagServiceName, - Description: argparser.FlagServiceNameDesc, - Dst: &c.serviceName.Value, - }) - return &c -} - -type status struct { - Enabled bool `json:"enabled"` -} - -// Exec invokes the application logic for the command. -func (c *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - if c.Globals.Verbose() && c.JSONOutput.Enabled { - return fsterr.ErrInvalidVerboseJSONCombo - } - - serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - if c.Globals.Verbose() { - argparser.DisplayServiceID(serviceID, flag, source, out) - } - - var s status - - _, err = GetFn(c.Globals.APIClient, serviceID) - if err != nil { - var herr *fastly.HTTPError - - // The API returns a 'Bad Request' error when the - // product has not been enabled on the service; any - // other error should be reported - if !errors.As(err, &herr) || !herr.IsBadRequest() { - c.Globals.ErrLog.Add(err) - return err - } - } else { - s.Enabled = true - } - - if ok, err := c.WriteJSON(out, s); ok { - return err - } - - var msg string - if s.Enabled { - msg = "enabled" - } else { - msg = "disabled" - } - - text.Info(out, - bot_management.ProductName+" is %s on service %s", msg, serviceID) - - return nil -} diff --git a/pkg/commands/product/botmanagement/common.go b/pkg/commands/product/botmanagement/common.go new file mode 100644 index 000000000..53eb554a7 --- /dev/null +++ b/pkg/commands/product/botmanagement/common.go @@ -0,0 +1,23 @@ +package botmanagement + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*botmanagement.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return botmanagement.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*botmanagement.EnableOutput, error) { + return botmanagement.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*botmanagement.EnableOutput, error) { + return botmanagement.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/bot_management/disable.go b/pkg/commands/product/botmanagement/disable.go similarity index 51% rename from pkg/commands/product/bot_management/disable.go rename to pkg/commands/product/botmanagement/disable.go index b30c8bdf3..7c004a459 100644 --- a/pkg/commands/product/bot_management/disable.go +++ b/pkg/commands/product/botmanagement/disable.go @@ -1,36 +1,28 @@ -package bot_management +package botmanagement import ( "io" - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" ) -// DisableFn is a dependency-injection point for unit tests to provide -// a mock implementation of the API operation. -var DisableFn = func(client api.Interface, serviceID string) error { - return bot_management.Disable(client.(*fastly.Client), serviceID) -} - // DisableCommand calls the Fastly API to disable the product. type DisableCommand struct { - productcore.Disable + productcore.Disable[*botmanagement.EnableOutput] } // NewDisableCommand returns a usable command registered under the parent. func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { c := DisableCommand{} - c.Init(parent, g, bot_management.ProductName) + c.Init(parent, g, botmanagement.ProductName, &EnablementHooks) return &c } // Exec invokes the application logic for the command. func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out, DisableFn) + return cmd.Disable.Exec(out) } diff --git a/pkg/commands/product/botmanagement/doc.go b/pkg/commands/product/botmanagement/doc.go new file mode 100644 index 000000000..e735a6455 --- /dev/null +++ b/pkg/commands/product/botmanagement/doc.go @@ -0,0 +1,3 @@ +// Package botmanagement contains commands to enable and disable the +// Fastly Bot Management product. +package botmanagement diff --git a/pkg/commands/product/bot_management/enable.go b/pkg/commands/product/botmanagement/enable.go similarity index 74% rename from pkg/commands/product/bot_management/enable.go rename to pkg/commands/product/botmanagement/enable.go index 988ff5b96..3cb147c61 100644 --- a/pkg/commands/product/bot_management/enable.go +++ b/pkg/commands/product/botmanagement/enable.go @@ -1,9 +1,9 @@ -package bot_management +package botmanagement import ( "io" - "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" "github.com/fastly/cli/internal/productcore" "github.com/fastly/cli/pkg/argparser" @@ -12,13 +12,13 @@ import ( // EnableCommand calls the Fastly API to disable the product. type EnableCommand struct { - productcore.Enable[*bot_management.EnableOutput] + productcore.Enable[*botmanagement.EnableOutput] } // NewEnableCommand returns a usable command registered under the parent. func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { c := EnableCommand{} - c.Init(parent, g, bot_management.ProductName, &EnablementHooks) + c.Init(parent, g, botmanagement.ProductName, &EnablementHooks) return &c } diff --git a/pkg/commands/product/bot_management/product_test.go b/pkg/commands/product/botmanagement/product_test.go similarity index 72% rename from pkg/commands/product/bot_management/product_test.go rename to pkg/commands/product/botmanagement/product_test.go index 7c7c23394..1e8768633 100644 --- a/pkg/commands/product/bot_management/product_test.go +++ b/pkg/commands/product/botmanagement/product_test.go @@ -1,14 +1,14 @@ -package bot_management_test +package botmanagement_test import ( "testing" "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/bot_management" + "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" "github.com/fastly/cli/pkg/api" root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/bot_management" + sub "github.com/fastly/cli/pkg/commands/product/botmanagement" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/testutil" ) @@ -38,17 +38,17 @@ func TestProductEnablement(t *testing.T) { { Name: "validate success for enabling product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.EnableFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { return nil, nil } }, Args: "enable --service-id 123", - WantOutput: "SUCCESS: Enabled " + bot_management.ProductName + " on service 123", + WantOutput: "SUCCESS: Enabled " + botmanagement.ProductName + " on service 123", }, { Name: "validate failure for enabling product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.EnableFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { return nil, testutil.Err } }, @@ -58,17 +58,17 @@ func TestProductEnablement(t *testing.T) { { Name: "validate success for disabling product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.DisableFn = func(_ api.Interface, _ string) error { + sub.EnablementHooks.DisableFunc = func(_ api.Interface, _ string) error { return nil } }, Args: "disable --service-id 123", - WantOutput: "SUCCESS: Disabled " + bot_management.ProductName + " on service 123", + WantOutput: "SUCCESS: Disabled " + botmanagement.ProductName + " on service 123", }, { Name: "validate failure for disabling product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.DisableFn = func(_ api.Interface, _ string) error { + sub.EnablementHooks.DisableFunc = func(_ api.Interface, _ string) error { return testutil.Err } }, @@ -78,17 +78,17 @@ func TestProductEnablement(t *testing.T) { { Name: "validate regular status output for enabled product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { return nil, nil } }, Args: "status --service-id 123", - WantOutput: "INFO: " + bot_management.ProductName + " is enabled on service 123", + WantOutput: "INFO: " + botmanagement.ProductName + " is enabled on service 123", }, { Name: "validate JSON status output for enabled product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { return nil, nil } }, @@ -98,19 +98,19 @@ func TestProductEnablement(t *testing.T) { { Name: "validate regular status output for disabled product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { // The API returns a 'Bad Request' error when the // product has not been enabled on the service return nil, &fastly.HTTPError{StatusCode: 400} } }, Args: "status --service-id 123", - WantOutput: "INFO: " + bot_management.ProductName + " is disabled on service 123", + WantOutput: "INFO: " + botmanagement.ProductName + " is disabled on service 123", }, { Name: "validate JSON status output for disabled product", Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.GetFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) { + sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { // The API returns a 'Bad Request' error when the // product has not been enabled on the service return nil, &fastly.HTTPError{StatusCode: 400} diff --git a/pkg/commands/product/bot_management/root.go b/pkg/commands/product/botmanagement/root.go similarity index 97% rename from pkg/commands/product/bot_management/root.go rename to pkg/commands/product/botmanagement/root.go index 20437c2ad..c0cbae4a2 100644 --- a/pkg/commands/product/bot_management/root.go +++ b/pkg/commands/product/botmanagement/root.go @@ -1,4 +1,4 @@ -package bot_management +package botmanagement import ( "io" diff --git a/pkg/commands/product/botmanagement/status.go b/pkg/commands/product/botmanagement/status.go new file mode 100644 index 000000000..4dbb81162 --- /dev/null +++ b/pkg/commands/product/botmanagement/status.go @@ -0,0 +1,28 @@ +package botmanagement + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*botmanagement.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, botmanagement.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} From b63a9ddaa920a48832e8d9c0b6620f7882f89170 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 13 Jan 2025 12:12:30 -0500 Subject: [PATCH 5/9] MOAR GENERICS --- internal/productcore/base.go | 2 +- internal/productcore_test/enablement.go | 163 ++++++++++++++++++ .../product/botmanagement/product_test.go | 120 +------------ 3 files changed, 171 insertions(+), 114 deletions(-) create mode 100644 internal/productcore_test/enablement.go diff --git a/internal/productcore/base.go b/internal/productcore/base.go index e701d0017..033868a21 100644 --- a/internal/productcore/base.go +++ b/internal/productcore/base.go @@ -53,7 +53,7 @@ type EnablementHookFuncs[O any] struct { } // ConfigurationHookFuncs is a structure of dependency-injection -// points by unit tests to provide mock behaviors +// points used by unit tests to provide mock behaviors type ConfigurationHookFuncs[O, I any] struct { GetConfigurationFunc func(api.Interface, string) (O, error) UpdateConfigurationFunc func(api.Interface, string, I) (O, error) diff --git a/internal/productcore_test/enablement.go b/internal/productcore_test/enablement.go new file mode 100644 index 000000000..deabf515b --- /dev/null +++ b/internal/productcore_test/enablement.go @@ -0,0 +1,163 @@ +package productcore_test + +import ( + "testing" + + "github.com/fastly/go-fastly/v9/fastly" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/cli/pkg/testutil" +) + +type TestEnablementInput[O any] struct { + T *testing.T + Commands []string + ProductName string + Hooks *productcore.EnablementHookFuncs[O] +} + +func TestEnablement[O any](i TestEnablementInput[O]) { + scenarios := []testutil.CLIScenario{ + { + Name: "validate missing Service ID: enable", + Args: "enable", + WantError: "error reading service: no service ID found", + }, + { + Name: "validate missing Service ID: disable", + Args: "enable", + WantError: "error reading service: no service ID found", + }, + { + Name: "validate missing Service ID: status", + Args: "enable", + WantError: "error reading service: no service ID found", + }, + { + Name: "validate invalid json/verbose flag combo: enable", + Args: "enable --service-id 123 --json --verbose", + WantError: "invalid flag combination, --verbose and --json", + }, + { + Name: "validate invalid json/verbose flag combo: disable", + Args: "disable --service-id 123 --json --verbose", + WantError: "invalid flag combination, --verbose and --json", + }, + { + Name: "validate invalid json/verbose flag combo: status", + Args: "status --service-id 123 --json --verbose", + WantError: "invalid flag combination, --verbose and --json", + }, + { + Name: "validate text output success for enabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { + return + } + }, + Args: "enable --service-id 123", + WantOutput: "SUCCESS: Enabled " + i.ProductName + " on service 123", + }, + { + Name: "validate JSON output success for enabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { + return + } + }, + Args: "enable --service-id 123 --json", + WantOutput: "{\n \"enabled\": true\n}", + }, + { + Name: "validate failure for enabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { + err = testutil.Err + return + } + }, + Args: "enable --service-id 123", + WantError: "test error", + }, + { + Name: "validate text output success for disabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { + return nil + } + }, + Args: "disable --service-id 123", + WantOutput: "SUCCESS: Disabled " + i.ProductName + " on service 123", + }, + { + Name: "validate JSON output success for disabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { + return nil + } + }, + Args: "disable --service-id 123 --json", + WantOutput: "{\n \"enabled\": false\n}", + }, + { + Name: "validate failure for disabling product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { + return testutil.Err + } + }, + Args: "disable --service-id 123", + WantError: "test error", + }, + { + Name: "validate text status output for enabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { + return + } + }, + Args: "status --service-id 123", + WantOutput: "INFO: " + i.ProductName + " is enabled on service 123", + }, + { + Name: "validate JSON status output for enabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { + return + } + }, + Args: "status --service-id 123 --json", + WantOutput: "{\n \"enabled\": true\n}", + }, + { + Name: "validate text status output for disabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service + err = &fastly.HTTPError{StatusCode: 400} + return + } + }, + Args: "status --service-id 123", + WantOutput: "INFO: " + i.ProductName + " is disabled on service 123", + }, + { + Name: "validate JSON status output for disabled product", + Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service + err = &fastly.HTTPError{StatusCode: 400} + return + } + }, + Args: "status --service-id 123 --json", + WantOutput: "{\n \"enabled\": false\n}", + }, + } + + testutil.RunCLIScenarios(i.T, i.Commands, scenarios) +} diff --git a/pkg/commands/product/botmanagement/product_test.go b/pkg/commands/product/botmanagement/product_test.go index 1e8768633..cb113472d 100644 --- a/pkg/commands/product/botmanagement/product_test.go +++ b/pkg/commands/product/botmanagement/product_test.go @@ -3,123 +3,17 @@ package botmanagement_test import ( "testing" - "github.com/fastly/go-fastly/v9/fastly" "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" - - "github.com/fastly/cli/pkg/api" + "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/botmanagement" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/cli/pkg/testutil" ) func TestProductEnablement(t *testing.T) { - scenarios := []testutil.CLIScenario{ - { - Name: "validate missing Service ID: enable", - Args: "enable", - WantError: "error reading service: no service ID found", - }, - { - Name: "validate missing Service ID: disable", - Args: "enable", - WantError: "error reading service: no service ID found", - }, - { - Name: "validate missing Service ID: status", - Args: "enable", - WantError: "error reading service: no service ID found", - }, - { - Name: "validate invalid json/verbose flag combo: status", - Args: "status --service-id 123 --json --verbose", - WantError: "invalid flag combination, --verbose and --json", - }, - { - Name: "validate success for enabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.EnableFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { - return nil, nil - } - }, - Args: "enable --service-id 123", - WantOutput: "SUCCESS: Enabled " + botmanagement.ProductName + " on service 123", - }, - { - Name: "validate failure for enabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.EnableFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { - return nil, testutil.Err - } - }, - Args: "enable --service-id 123", - WantError: "test error", - }, - { - Name: "validate success for disabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.DisableFunc = func(_ api.Interface, _ string) error { - return nil - } - }, - Args: "disable --service-id 123", - WantOutput: "SUCCESS: Disabled " + botmanagement.ProductName + " on service 123", - }, - { - Name: "validate failure for disabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.DisableFunc = func(_ api.Interface, _ string) error { - return testutil.Err - } - }, - Args: "disable --service-id 123", - WantError: "test error", - }, - { - Name: "validate regular status output for enabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { - return nil, nil - } - }, - Args: "status --service-id 123", - WantOutput: "INFO: " + botmanagement.ProductName + " is enabled on service 123", - }, - { - Name: "validate JSON status output for enabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { - return nil, nil - } - }, - Args: "status --service-id 123 --json", - WantOutput: "{\n \"enabled\": true\n}", - }, - { - Name: "validate regular status output for disabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { - // The API returns a 'Bad Request' error when the - // product has not been enabled on the service - return nil, &fastly.HTTPError{StatusCode: 400} - } - }, - Args: "status --service-id 123", - WantOutput: "INFO: " + botmanagement.ProductName + " is disabled on service 123", - }, - { - Name: "validate JSON status output for disabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { - sub.EnablementHooks.GetFunc = func(_ api.Interface, _ string) (*botmanagement.EnableOutput, error) { - // The API returns a 'Bad Request' error when the - // product has not been enabled on the service - return nil, &fastly.HTTPError{StatusCode: 400} - } - }, - Args: "status --service-id 123 --json", - WantOutput: "{\n \"enabled\": false\n}", - }, - } - - testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName}, scenarios) + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*botmanagement.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductName: botmanagement.ProductName, + Hooks: &sub.EnablementHooks, + }) } From f0d3db52251ae145ca34fea98f8be136d3f1adb7 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 13 Jan 2025 12:23:54 -0500 Subject: [PATCH 6/9] Fixes and Makefile --- internal/productcore/base.go | 6 +++--- internal/productcore/disable.go | 1 + internal/productcore/enable.go | 3 ++- internal/productcore/status.go | 7 ++++--- internal/productcore_test/enablement.go | 10 +++++----- pkg/commands/product/botmanagement/product_test.go | 8 ++++---- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/internal/productcore/base.go b/internal/productcore/base.go index 033868a21..aec548866 100644 --- a/internal/productcore/base.go +++ b/internal/productcore/base.go @@ -48,13 +48,13 @@ type EnablementStatus struct { // used by unit tests to provide mock behaviors type EnablementHookFuncs[O any] struct { DisableFunc func(api.Interface, string) error - EnableFunc func(api.Interface, string) (O, error) - GetFunc func(api.Interface, string) (O, error) + EnableFunc func(api.Interface, string) (O, error) + GetFunc func(api.Interface, string) (O, error) } // ConfigurationHookFuncs is a structure of dependency-injection // points used by unit tests to provide mock behaviors type ConfigurationHookFuncs[O, I any] struct { - GetConfigurationFunc func(api.Interface, string) (O, error) + GetConfigurationFunc func(api.Interface, string) (O, error) UpdateConfigurationFunc func(api.Interface, string, I) (O, error) } diff --git a/internal/productcore/disable.go b/internal/productcore/disable.go index 945e7f13d..b7b308b30 100644 --- a/internal/productcore/disable.go +++ b/internal/productcore/disable.go @@ -2,6 +2,7 @@ package productcore import ( "io" + fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/argparser" diff --git a/internal/productcore/enable.go b/internal/productcore/enable.go index 153064be4..bfe0bc081 100644 --- a/internal/productcore/enable.go +++ b/internal/productcore/enable.go @@ -2,8 +2,9 @@ package productcore import ( "io" - fsterr "github.com/fastly/cli/pkg/errors" + "github.com/fastly/cli/pkg/argparser" + fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" ) diff --git a/internal/productcore/status.go b/internal/productcore/status.go index 7c7cea27e..d7c87d608 100644 --- a/internal/productcore/status.go +++ b/internal/productcore/status.go @@ -1,13 +1,14 @@ package productcore import ( - "io" "errors" - "github.com/fastly/go-fastly/v9/fastly" - fsterr "github.com/fastly/cli/pkg/errors" + "io" + "github.com/fastly/cli/pkg/argparser" + fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" + "github.com/fastly/go-fastly/v9/fastly" ) // Status is a base type for all 'status' commands. diff --git a/internal/productcore_test/enablement.go b/internal/productcore_test/enablement.go index deabf515b..21fc8cd07 100644 --- a/internal/productcore_test/enablement.go +++ b/internal/productcore_test/enablement.go @@ -12,10 +12,10 @@ import ( ) type TestEnablementInput[O any] struct { - T *testing.T - Commands []string + T *testing.T + Commands []string ProductName string - Hooks *productcore.EnablementHookFuncs[O] + Hooks *productcore.EnablementHookFuncs[O] } func TestEnablement[O any](i TestEnablementInput[O]) { @@ -137,7 +137,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { // The API returns a 'Bad Request' error when the // product has not been enabled on the service - err = &fastly.HTTPError{StatusCode: 400} + err = &fastly.HTTPError{StatusCode: 400} return } }, @@ -150,7 +150,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { // The API returns a 'Bad Request' error when the // product has not been enabled on the service - err = &fastly.HTTPError{StatusCode: 400} + err = &fastly.HTTPError{StatusCode: 400} return } }, diff --git a/pkg/commands/product/botmanagement/product_test.go b/pkg/commands/product/botmanagement/product_test.go index cb113472d..679a5eb7a 100644 --- a/pkg/commands/product/botmanagement/product_test.go +++ b/pkg/commands/product/botmanagement/product_test.go @@ -3,17 +3,17 @@ package botmanagement_test import ( "testing" - "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/botmanagement" + "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" ) func TestProductEnablement(t *testing.T) { productcore_test.TestEnablement(productcore_test.TestEnablementInput[*botmanagement.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, + T: t, + Commands: []string{root.CommandName, sub.CommandName}, ProductName: botmanagement.ProductName, - Hooks: &sub.EnablementHooks, + Hooks: &sub.EnablementHooks, }) } From b8eeffa7e71de2ba628ff2785b245c3d2e8f8c1d Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 13 Jan 2025 15:23:29 -0500 Subject: [PATCH 7/9] Remaining enableable products except NGWAF. --- internal/productcore/base.go | 7 +- internal/productcore/disable.go | 6 +- internal/productcore/enable.go | 6 +- internal/productcore/status.go | 6 +- internal/productcore_test/enablement.go | 32 ++++---- pkg/commands/commands.go | 82 +++++++++++++++++++ pkg/commands/product/botmanagement/disable.go | 2 +- pkg/commands/product/botmanagement/enable.go | 2 +- .../product/botmanagement/product_test.go | 3 +- pkg/commands/product/botmanagement/status.go | 2 +- .../product/brotlicompression/common.go | 23 ++++++ .../product/brotlicompression/disable.go | 28 +++++++ pkg/commands/product/brotlicompression/doc.go | 3 + .../product/brotlicompression/enable.go | 28 +++++++ .../product/brotlicompression/product_test.go | 20 +++++ .../product/brotlicompression/root.go | 31 +++++++ .../product/brotlicompression/status.go | 28 +++++++ pkg/commands/product/ddosprotection/common.go | 23 ++++++ .../product/ddosprotection/disable.go | 28 +++++++ pkg/commands/product/ddosprotection/doc.go | 3 + pkg/commands/product/ddosprotection/enable.go | 28 +++++++ .../product/ddosprotection/product_test.go | 20 +++++ pkg/commands/product/ddosprotection/root.go | 31 +++++++ pkg/commands/product/ddosprotection/status.go | 28 +++++++ .../product/domaininspector/common.go | 23 ++++++ .../product/domaininspector/disable.go | 28 +++++++ pkg/commands/product/domaininspector/doc.go | 3 + .../product/domaininspector/enable.go | 28 +++++++ .../product/domaininspector/product_test.go | 20 +++++ pkg/commands/product/domaininspector/root.go | 31 +++++++ .../product/domaininspector/status.go | 28 +++++++ pkg/commands/product/fanout/common.go | 23 ++++++ pkg/commands/product/fanout/disable.go | 28 +++++++ pkg/commands/product/fanout/doc.go | 3 + pkg/commands/product/fanout/enable.go | 28 +++++++ pkg/commands/product/fanout/product_test.go | 20 +++++ pkg/commands/product/fanout/root.go | 31 +++++++ pkg/commands/product/fanout/status.go | 28 +++++++ pkg/commands/product/imageoptimizer/common.go | 23 ++++++ .../product/imageoptimizer/disable.go | 28 +++++++ pkg/commands/product/imageoptimizer/doc.go | 3 + pkg/commands/product/imageoptimizer/enable.go | 28 +++++++ .../product/imageoptimizer/product_test.go | 20 +++++ pkg/commands/product/imageoptimizer/root.go | 31 +++++++ pkg/commands/product/imageoptimizer/status.go | 28 +++++++ .../product/logexplorerinsights/common.go | 23 ++++++ .../product/logexplorerinsights/disable.go | 28 +++++++ .../product/logexplorerinsights/doc.go | 3 + .../product/logexplorerinsights/enable.go | 28 +++++++ .../logexplorerinsights/product_test.go | 20 +++++ .../product/logexplorerinsights/root.go | 31 +++++++ .../product/logexplorerinsights/status.go | 28 +++++++ .../product/origininspector/common.go | 23 ++++++ .../product/origininspector/disable.go | 28 +++++++ pkg/commands/product/origininspector/doc.go | 3 + .../product/origininspector/enable.go | 28 +++++++ .../product/origininspector/product_test.go | 20 +++++ pkg/commands/product/origininspector/root.go | 31 +++++++ .../product/origininspector/status.go | 28 +++++++ pkg/commands/product/websockets/common.go | 23 ++++++ pkg/commands/product/websockets/disable.go | 28 +++++++ pkg/commands/product/websockets/doc.go | 3 + pkg/commands/product/websockets/enable.go | 28 +++++++ .../product/websockets/product_test.go | 20 +++++ pkg/commands/product/websockets/root.go | 31 +++++++ pkg/commands/product/websockets/status.go | 28 +++++++ 66 files changed, 1407 insertions(+), 29 deletions(-) create mode 100644 pkg/commands/product/brotlicompression/common.go create mode 100644 pkg/commands/product/brotlicompression/disable.go create mode 100644 pkg/commands/product/brotlicompression/doc.go create mode 100644 pkg/commands/product/brotlicompression/enable.go create mode 100644 pkg/commands/product/brotlicompression/product_test.go create mode 100644 pkg/commands/product/brotlicompression/root.go create mode 100644 pkg/commands/product/brotlicompression/status.go create mode 100644 pkg/commands/product/ddosprotection/common.go create mode 100644 pkg/commands/product/ddosprotection/disable.go create mode 100644 pkg/commands/product/ddosprotection/doc.go create mode 100644 pkg/commands/product/ddosprotection/enable.go create mode 100644 pkg/commands/product/ddosprotection/product_test.go create mode 100644 pkg/commands/product/ddosprotection/root.go create mode 100644 pkg/commands/product/ddosprotection/status.go create mode 100644 pkg/commands/product/domaininspector/common.go create mode 100644 pkg/commands/product/domaininspector/disable.go create mode 100644 pkg/commands/product/domaininspector/doc.go create mode 100644 pkg/commands/product/domaininspector/enable.go create mode 100644 pkg/commands/product/domaininspector/product_test.go create mode 100644 pkg/commands/product/domaininspector/root.go create mode 100644 pkg/commands/product/domaininspector/status.go create mode 100644 pkg/commands/product/fanout/common.go create mode 100644 pkg/commands/product/fanout/disable.go create mode 100644 pkg/commands/product/fanout/doc.go create mode 100644 pkg/commands/product/fanout/enable.go create mode 100644 pkg/commands/product/fanout/product_test.go create mode 100644 pkg/commands/product/fanout/root.go create mode 100644 pkg/commands/product/fanout/status.go create mode 100644 pkg/commands/product/imageoptimizer/common.go create mode 100644 pkg/commands/product/imageoptimizer/disable.go create mode 100644 pkg/commands/product/imageoptimizer/doc.go create mode 100644 pkg/commands/product/imageoptimizer/enable.go create mode 100644 pkg/commands/product/imageoptimizer/product_test.go create mode 100644 pkg/commands/product/imageoptimizer/root.go create mode 100644 pkg/commands/product/imageoptimizer/status.go create mode 100644 pkg/commands/product/logexplorerinsights/common.go create mode 100644 pkg/commands/product/logexplorerinsights/disable.go create mode 100644 pkg/commands/product/logexplorerinsights/doc.go create mode 100644 pkg/commands/product/logexplorerinsights/enable.go create mode 100644 pkg/commands/product/logexplorerinsights/product_test.go create mode 100644 pkg/commands/product/logexplorerinsights/root.go create mode 100644 pkg/commands/product/logexplorerinsights/status.go create mode 100644 pkg/commands/product/origininspector/common.go create mode 100644 pkg/commands/product/origininspector/disable.go create mode 100644 pkg/commands/product/origininspector/doc.go create mode 100644 pkg/commands/product/origininspector/enable.go create mode 100644 pkg/commands/product/origininspector/product_test.go create mode 100644 pkg/commands/product/origininspector/root.go create mode 100644 pkg/commands/product/origininspector/status.go create mode 100644 pkg/commands/product/websockets/common.go create mode 100644 pkg/commands/product/websockets/disable.go create mode 100644 pkg/commands/product/websockets/doc.go create mode 100644 pkg/commands/product/websockets/enable.go create mode 100644 pkg/commands/product/websockets/product_test.go create mode 100644 pkg/commands/product/websockets/root.go create mode 100644 pkg/commands/product/websockets/status.go diff --git a/internal/productcore/base.go b/internal/productcore/base.go index aec548866..01b6233b3 100644 --- a/internal/productcore/base.go +++ b/internal/productcore/base.go @@ -14,12 +14,14 @@ type Base struct { Manifest manifest.Data ServiceName argparser.OptionalServiceNameID + ProductID string ProductName string } // Init prepares the structure for use by the CLI core. -func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productName string) { +func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productID, productName string) { cmd.Globals = g + cmd.ProductID = productID cmd.ProductName = productName // Optional flags. @@ -41,7 +43,8 @@ func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productName s // EnablementStatus is a structure used to generate JSON output from // the enablement-related commands type EnablementStatus struct { - Enabled bool `json:"enabled"` + ProductID string `json:"product_id"` + Enabled bool `json:"enabled"` } // EnablementHookFuncs is a structure of dependency-injection points diff --git a/internal/productcore/disable.go b/internal/productcore/disable.go index b7b308b30..ea211918b 100644 --- a/internal/productcore/disable.go +++ b/internal/productcore/disable.go @@ -17,11 +17,11 @@ type Disable[O any] struct { } // Init prepares the structure for use by the CLI core. -func (cmd *Disable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { +func (cmd *Disable[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product") cmd.hooks = hooks - cmd.Base.Init(parent, g, productName) + cmd.Base.Init(parent, g, productID, productName) } // Exec executes the disablement operation. @@ -46,7 +46,7 @@ func (cmd *Disable[O]) Exec(out io.Writer) error { return err } - if ok, err := cmd.WriteJSON(out, EnablementStatus{Enabled: false}); ok { + if ok, err := cmd.WriteJSON(out, EnablementStatus{ProductID: cmd.ProductID, Enabled: false}); ok { return err } diff --git a/internal/productcore/enable.go b/internal/productcore/enable.go index bfe0bc081..a26327307 100644 --- a/internal/productcore/enable.go +++ b/internal/productcore/enable.go @@ -16,11 +16,11 @@ type Enable[O any] struct { } // Init prepares the structure for use by the CLI core. -func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { +func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product") cmd.hooks = hooks - cmd.Base.Init(parent, g, productName) + cmd.Base.Init(parent, g, productID, productName) } // Exec executes the enablement operation. @@ -45,7 +45,7 @@ func (cmd *Enable[O]) Exec(out io.Writer) error { return err } - if ok, err := cmd.WriteJSON(out, EnablementStatus{Enabled: true}); ok { + if ok, err := cmd.WriteJSON(out, EnablementStatus{ProductID: cmd.ProductID, Enabled: true}); ok { return err } diff --git a/internal/productcore/status.go b/internal/productcore/status.go index d7c87d608..60c10a486 100644 --- a/internal/productcore/status.go +++ b/internal/productcore/status.go @@ -18,11 +18,11 @@ type Status[O any] struct { } // Init prepares the structure for use by the CLI core. -func (cmd *Status[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { +func (cmd *Status[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("status", "Get the enablement status of the "+productName+" product") cmd.hooks = hooks - cmd.Base.Init(parent, g, productName) + cmd.Base.Init(parent, g, productID, productName) } // Exec executes the status operation. @@ -41,7 +41,7 @@ func (cmd *Status[O]) Exec(out io.Writer) error { argparser.DisplayServiceID(serviceID, flag, source, out) } - s := EnablementStatus{} + s := EnablementStatus{ProductID: cmd.ProductID} state := "disabled" _, err = cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID) diff --git a/internal/productcore_test/enablement.go b/internal/productcore_test/enablement.go index 21fc8cd07..9e823c633 100644 --- a/internal/productcore_test/enablement.go +++ b/internal/productcore_test/enablement.go @@ -11,13 +11,17 @@ import ( "github.com/fastly/cli/pkg/testutil" ) +// TestEnablementInput supplies the details required for +// TestEnablement to execute a series of test scenarios. type TestEnablementInput[O any] struct { T *testing.T Commands []string + ProductID string ProductName string Hooks *productcore.EnablementHookFuncs[O] } +// TestEnablement executes the test scenarios common to all products. func TestEnablement[O any](i TestEnablementInput[O]) { scenarios := []testutil.CLIScenario{ { @@ -52,7 +56,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate text output success for enabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { return } @@ -62,17 +66,17 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate JSON output success for enabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { return } }, Args: "enable --service-id 123 --json", - WantOutput: "{\n \"enabled\": true\n}", + WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": true\n}", }, { Name: "validate failure for enabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { err = testutil.Err return @@ -83,7 +87,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate text output success for disabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { return nil } @@ -93,17 +97,17 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate JSON output success for disabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { return nil } }, Args: "disable --service-id 123 --json", - WantOutput: "{\n \"enabled\": false\n}", + WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": false\n}", }, { Name: "validate failure for disabling product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { return testutil.Err } @@ -113,7 +117,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate text status output for enabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { return } @@ -123,17 +127,17 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate JSON status output for enabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { return } }, Args: "status --service-id 123 --json", - WantOutput: "{\n \"enabled\": true\n}", + WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": true\n}", }, { Name: "validate text status output for disabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { // The API returns a 'Bad Request' error when the // product has not been enabled on the service @@ -146,7 +150,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { }, { Name: "validate JSON status output for disabled product", - Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) { + Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { // The API returns a 'Bad Request' error when the // product has not been enabled on the service @@ -155,7 +159,7 @@ func TestEnablement[O any](i TestEnablementInput[O]) { } }, Args: "status --service-id 123 --json", - WantOutput: "{\n \"enabled\": false\n}", + WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": false\n}", }, } diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index aa93a388c..8cd65ef7c 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -60,6 +60,14 @@ import ( "github.com/fastly/cli/pkg/commands/pop" "github.com/fastly/cli/pkg/commands/product" "github.com/fastly/cli/pkg/commands/product/botmanagement" + "github.com/fastly/cli/pkg/commands/product/brotlicompression" + "github.com/fastly/cli/pkg/commands/product/ddosprotection" + "github.com/fastly/cli/pkg/commands/product/domaininspector" + "github.com/fastly/cli/pkg/commands/product/fanout" + "github.com/fastly/cli/pkg/commands/product/imageoptimizer" + "github.com/fastly/cli/pkg/commands/product/logexplorerinsights" + "github.com/fastly/cli/pkg/commands/product/origininspector" + "github.com/fastly/cli/pkg/commands/product/websockets" "github.com/fastly/cli/pkg/commands/products" "github.com/fastly/cli/pkg/commands/profile" "github.com/fastly/cli/pkg/commands/purge" @@ -401,11 +409,53 @@ func Define( // nolint:revive // function-length objectStorageAccesskeysGet := accesskeys.NewGetCommand(objectStorageAccesskeysRoot.CmdClause, data) objectStorageAccesskeysList := accesskeys.NewListCommand(objectStorageAccesskeysRoot.CmdClause, data) popCmdRoot := pop.NewRootCommand(app, data) + productCmdRoot := product.NewRootCommand(app, data) productBotManagementCmdRoot := botmanagement.NewRootCommand(productCmdRoot.CmdClause, data) productBotManagementDisable := botmanagement.NewDisableCommand(productBotManagementCmdRoot.CmdClause, data) productBotManagementEnable := botmanagement.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data) productBotManagementStatus := botmanagement.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data) + + productBrotliCompressionCmdRoot := brotlicompression.NewRootCommand(productCmdRoot.CmdClause, data) + productBrotliCompressionDisable := brotlicompression.NewDisableCommand(productBrotliCompressionCmdRoot.CmdClause, data) + productBrotliCompressionEnable := brotlicompression.NewEnableCommand(productBrotliCompressionCmdRoot.CmdClause, data) + productBrotliCompressionStatus := brotlicompression.NewStatusCommand(productBrotliCompressionCmdRoot.CmdClause, data) + + productDDoSProtectionCmdRoot := ddosprotection.NewRootCommand(productCmdRoot.CmdClause, data) + productDDoSProtectionDisable := ddosprotection.NewDisableCommand(productDDoSProtectionCmdRoot.CmdClause, data) + productDDoSProtectionEnable := ddosprotection.NewEnableCommand(productDDoSProtectionCmdRoot.CmdClause, data) + productDDoSProtectionStatus := ddosprotection.NewStatusCommand(productDDoSProtectionCmdRoot.CmdClause, data) + + productDomainInspectorCmdRoot := domaininspector.NewRootCommand(productCmdRoot.CmdClause, data) + productDomainInspectorDisable := domaininspector.NewDisableCommand(productDomainInspectorCmdRoot.CmdClause, data) + productDomainInspectorEnable := domaininspector.NewEnableCommand(productDomainInspectorCmdRoot.CmdClause, data) + productDomainInspectorStatus := domaininspector.NewStatusCommand(productDomainInspectorCmdRoot.CmdClause, data) + + productFanoutCmdRoot := fanout.NewRootCommand(productCmdRoot.CmdClause, data) + productFanoutDisable := fanout.NewDisableCommand(productFanoutCmdRoot.CmdClause, data) + productFanoutEnable := fanout.NewEnableCommand(productFanoutCmdRoot.CmdClause, data) + productFanoutStatus := fanout.NewStatusCommand(productFanoutCmdRoot.CmdClause, data) + + productImageOptimizerCmdRoot := imageoptimizer.NewRootCommand(productCmdRoot.CmdClause, data) + productImageOptimizerDisable := imageoptimizer.NewDisableCommand(productImageOptimizerCmdRoot.CmdClause, data) + productImageOptimizerEnable := imageoptimizer.NewEnableCommand(productImageOptimizerCmdRoot.CmdClause, data) + productImageOptimizerStatus := imageoptimizer.NewStatusCommand(productImageOptimizerCmdRoot.CmdClause, data) + + productLogExplorerInsightsCmdRoot := logexplorerinsights.NewRootCommand(productCmdRoot.CmdClause, data) + productLogExplorerInsightsDisable := logexplorerinsights.NewDisableCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) + productLogExplorerInsightsEnable := logexplorerinsights.NewEnableCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) + productLogExplorerInsightsStatus := logexplorerinsights.NewStatusCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) + + productOriginInspectorCmdRoot := origininspector.NewRootCommand(productCmdRoot.CmdClause, data) + productOriginInspectorDisable := origininspector.NewDisableCommand(productOriginInspectorCmdRoot.CmdClause, data) + productOriginInspectorEnable := origininspector.NewEnableCommand(productOriginInspectorCmdRoot.CmdClause, data) + productOriginInspectorStatus := origininspector.NewStatusCommand(productOriginInspectorCmdRoot.CmdClause, data) + + productWebSocketsCmdRoot := websockets.NewRootCommand(productCmdRoot.CmdClause, data) + productWebSocketsDisable := websockets.NewDisableCommand(productWebSocketsCmdRoot.CmdClause, data) + productWebSocketsEnable := websockets.NewEnableCommand(productWebSocketsCmdRoot.CmdClause, data) + productWebSocketsStatus := websockets.NewStatusCommand(productWebSocketsCmdRoot.CmdClause, data) + productsCmdRoot := products.NewRootCommand(app, data) profileCmdRoot := profile.NewRootCommand(app, data) profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, data, ssoCmdRoot) @@ -829,6 +879,38 @@ func Define( // nolint:revive // function-length productBotManagementDisable, productBotManagementEnable, productBotManagementStatus, + productBrotliCompressionCmdRoot, + productBrotliCompressionDisable, + productBrotliCompressionEnable, + productBrotliCompressionStatus, + productDDoSProtectionCmdRoot, + productDDoSProtectionDisable, + productDDoSProtectionEnable, + productDDoSProtectionStatus, + productDomainInspectorCmdRoot, + productDomainInspectorDisable, + productDomainInspectorEnable, + productDomainInspectorStatus, + productFanoutCmdRoot, + productFanoutDisable, + productFanoutEnable, + productFanoutStatus, + productImageOptimizerCmdRoot, + productImageOptimizerDisable, + productImageOptimizerEnable, + productImageOptimizerStatus, + productLogExplorerInsightsCmdRoot, + productLogExplorerInsightsDisable, + productLogExplorerInsightsEnable, + productLogExplorerInsightsStatus, + productOriginInspectorCmdRoot, + productOriginInspectorDisable, + productOriginInspectorEnable, + productOriginInspectorStatus, + productWebSocketsCmdRoot, + productWebSocketsDisable, + productWebSocketsEnable, + productWebSocketsStatus, productsCmdRoot, profileCmdRoot, profileCreate, diff --git a/pkg/commands/product/botmanagement/disable.go b/pkg/commands/product/botmanagement/disable.go index 7c004a459..983d6812e 100644 --- a/pkg/commands/product/botmanagement/disable.go +++ b/pkg/commands/product/botmanagement/disable.go @@ -18,7 +18,7 @@ type DisableCommand struct { // NewDisableCommand returns a usable command registered under the parent. func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { c := DisableCommand{} - c.Init(parent, g, botmanagement.ProductName, &EnablementHooks) + c.Init(parent, g, botmanagement.ProductID, botmanagement.ProductName, &EnablementHooks) return &c } diff --git a/pkg/commands/product/botmanagement/enable.go b/pkg/commands/product/botmanagement/enable.go index 3cb147c61..613affe4d 100644 --- a/pkg/commands/product/botmanagement/enable.go +++ b/pkg/commands/product/botmanagement/enable.go @@ -18,7 +18,7 @@ type EnableCommand struct { // NewEnableCommand returns a usable command registered under the parent. func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { c := EnableCommand{} - c.Init(parent, g, botmanagement.ProductName, &EnablementHooks) + c.Init(parent, g, botmanagement.ProductID, botmanagement.ProductName, &EnablementHooks) return &c } diff --git a/pkg/commands/product/botmanagement/product_test.go b/pkg/commands/product/botmanagement/product_test.go index 679a5eb7a..ec403c77b 100644 --- a/pkg/commands/product/botmanagement/product_test.go +++ b/pkg/commands/product/botmanagement/product_test.go @@ -9,10 +9,11 @@ import ( "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" ) -func TestProductEnablement(t *testing.T) { +func TestBotManagementEnablement(t *testing.T) { productcore_test.TestEnablement(productcore_test.TestEnablementInput[*botmanagement.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, + ProductID: botmanagement.ProductID, ProductName: botmanagement.ProductName, Hooks: &sub.EnablementHooks, }) diff --git a/pkg/commands/product/botmanagement/status.go b/pkg/commands/product/botmanagement/status.go index 4dbb81162..f9655b4cd 100644 --- a/pkg/commands/product/botmanagement/status.go +++ b/pkg/commands/product/botmanagement/status.go @@ -18,7 +18,7 @@ type StatusCommand struct { // NewStatusCommand returns a usable command registered under the parent. func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { c := StatusCommand{} - c.Init(parent, g, botmanagement.ProductName, &EnablementHooks) + c.Init(parent, g, botmanagement.ProductID, botmanagement.ProductName, &EnablementHooks) return &c } diff --git a/pkg/commands/product/brotlicompression/common.go b/pkg/commands/product/brotlicompression/common.go new file mode 100644 index 000000000..109103319 --- /dev/null +++ b/pkg/commands/product/brotlicompression/common.go @@ -0,0 +1,23 @@ +package brotlicompression + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*brotlicompression.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return brotlicompression.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*brotlicompression.EnableOutput, error) { + return brotlicompression.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*brotlicompression.EnableOutput, error) { + return brotlicompression.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/brotlicompression/disable.go b/pkg/commands/product/brotlicompression/disable.go new file mode 100644 index 000000000..175de0efa --- /dev/null +++ b/pkg/commands/product/brotlicompression/disable.go @@ -0,0 +1,28 @@ +package brotlicompression + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*brotlicompression.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, brotlicompression.ProductID, brotlicompression.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/brotlicompression/doc.go b/pkg/commands/product/brotlicompression/doc.go new file mode 100644 index 000000000..8c09f66e2 --- /dev/null +++ b/pkg/commands/product/brotlicompression/doc.go @@ -0,0 +1,3 @@ +// Package brotlicompression contains commands to enable and disable the +// Fastly Brotli Compression product. +package brotlicompression diff --git a/pkg/commands/product/brotlicompression/enable.go b/pkg/commands/product/brotlicompression/enable.go new file mode 100644 index 000000000..7d3748352 --- /dev/null +++ b/pkg/commands/product/brotlicompression/enable.go @@ -0,0 +1,28 @@ +package brotlicompression + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*brotlicompression.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, brotlicompression.ProductID, brotlicompression.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/brotlicompression/product_test.go b/pkg/commands/product/brotlicompression/product_test.go new file mode 100644 index 000000000..50a1f97da --- /dev/null +++ b/pkg/commands/product/brotlicompression/product_test.go @@ -0,0 +1,20 @@ +package brotlicompression_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/brotlicompression" + "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*brotlicompression.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: brotlicompression.ProductID, + ProductName: brotlicompression.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/brotlicompression/root.go b/pkg/commands/product/brotlicompression/root.go new file mode 100644 index 000000000..470fd054d --- /dev/null +++ b/pkg/commands/product/brotlicompression/root.go @@ -0,0 +1,31 @@ +package brotlicompression + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "brotli_compression" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Brotli Compression product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/brotlicompression/status.go b/pkg/commands/product/brotlicompression/status.go new file mode 100644 index 000000000..a81a216bf --- /dev/null +++ b/pkg/commands/product/brotlicompression/status.go @@ -0,0 +1,28 @@ +package brotlicompression + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*brotlicompression.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, brotlicompression.ProductID, brotlicompression.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/ddosprotection/common.go b/pkg/commands/product/ddosprotection/common.go new file mode 100644 index 000000000..c039d3b9b --- /dev/null +++ b/pkg/commands/product/ddosprotection/common.go @@ -0,0 +1,23 @@ +package ddosprotection + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*ddosprotection.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return ddosprotection.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*ddosprotection.EnableOutput, error) { + return ddosprotection.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*ddosprotection.EnableOutput, error) { + return ddosprotection.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/ddosprotection/disable.go b/pkg/commands/product/ddosprotection/disable.go new file mode 100644 index 000000000..e62667c63 --- /dev/null +++ b/pkg/commands/product/ddosprotection/disable.go @@ -0,0 +1,28 @@ +package ddosprotection + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*ddosprotection.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, ddosprotection.ProductID, ddosprotection.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/ddosprotection/doc.go b/pkg/commands/product/ddosprotection/doc.go new file mode 100644 index 000000000..4a490d690 --- /dev/null +++ b/pkg/commands/product/ddosprotection/doc.go @@ -0,0 +1,3 @@ +// Package ddosprotection contains commands to enable and disable the +// Fastly DDoS Protection product. +package ddosprotection diff --git a/pkg/commands/product/ddosprotection/enable.go b/pkg/commands/product/ddosprotection/enable.go new file mode 100644 index 000000000..fd70474a2 --- /dev/null +++ b/pkg/commands/product/ddosprotection/enable.go @@ -0,0 +1,28 @@ +package ddosprotection + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*ddosprotection.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, ddosprotection.ProductID, ddosprotection.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/ddosprotection/product_test.go b/pkg/commands/product/ddosprotection/product_test.go new file mode 100644 index 000000000..6899fee49 --- /dev/null +++ b/pkg/commands/product/ddosprotection/product_test.go @@ -0,0 +1,20 @@ +package ddosprotection_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/ddosprotection" + "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*ddosprotection.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: ddosprotection.ProductID, + ProductName: ddosprotection.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/ddosprotection/root.go b/pkg/commands/product/ddosprotection/root.go new file mode 100644 index 000000000..93003a6d4 --- /dev/null +++ b/pkg/commands/product/ddosprotection/root.go @@ -0,0 +1,31 @@ +package ddosprotection + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "ddos_protection" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly DDoS Protection product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/ddosprotection/status.go b/pkg/commands/product/ddosprotection/status.go new file mode 100644 index 000000000..5fa815ae6 --- /dev/null +++ b/pkg/commands/product/ddosprotection/status.go @@ -0,0 +1,28 @@ +package ddosprotection + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*ddosprotection.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, ddosprotection.ProductID, ddosprotection.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/domaininspector/common.go b/pkg/commands/product/domaininspector/common.go new file mode 100644 index 000000000..8b6d1c500 --- /dev/null +++ b/pkg/commands/product/domaininspector/common.go @@ -0,0 +1,23 @@ +package domaininspector + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*domaininspector.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return domaininspector.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*domaininspector.EnableOutput, error) { + return domaininspector.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*domaininspector.EnableOutput, error) { + return domaininspector.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/domaininspector/disable.go b/pkg/commands/product/domaininspector/disable.go new file mode 100644 index 000000000..68512c52c --- /dev/null +++ b/pkg/commands/product/domaininspector/disable.go @@ -0,0 +1,28 @@ +package domaininspector + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*domaininspector.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, domaininspector.ProductID, domaininspector.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/domaininspector/doc.go b/pkg/commands/product/domaininspector/doc.go new file mode 100644 index 000000000..69c34b29f --- /dev/null +++ b/pkg/commands/product/domaininspector/doc.go @@ -0,0 +1,3 @@ +// Package domaininspector contains commands to enable and disable the +// Fastly Domain Inspector product. +package domaininspector diff --git a/pkg/commands/product/domaininspector/enable.go b/pkg/commands/product/domaininspector/enable.go new file mode 100644 index 000000000..2667594a9 --- /dev/null +++ b/pkg/commands/product/domaininspector/enable.go @@ -0,0 +1,28 @@ +package domaininspector + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*domaininspector.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, domaininspector.ProductID, domaininspector.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/domaininspector/product_test.go b/pkg/commands/product/domaininspector/product_test.go new file mode 100644 index 000000000..f9ae0c86f --- /dev/null +++ b/pkg/commands/product/domaininspector/product_test.go @@ -0,0 +1,20 @@ +package domaininspector_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/domaininspector" + "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*domaininspector.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: domaininspector.ProductID, + ProductName: domaininspector.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/domaininspector/root.go b/pkg/commands/product/domaininspector/root.go new file mode 100644 index 000000000..48d5a7a90 --- /dev/null +++ b/pkg/commands/product/domaininspector/root.go @@ -0,0 +1,31 @@ +package domaininspector + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "domain_inspector" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Domain Inspector product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/domaininspector/status.go b/pkg/commands/product/domaininspector/status.go new file mode 100644 index 000000000..ef0de728e --- /dev/null +++ b/pkg/commands/product/domaininspector/status.go @@ -0,0 +1,28 @@ +package domaininspector + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*domaininspector.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, domaininspector.ProductID, domaininspector.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/fanout/common.go b/pkg/commands/product/fanout/common.go new file mode 100644 index 000000000..0ccf4a77c --- /dev/null +++ b/pkg/commands/product/fanout/common.go @@ -0,0 +1,23 @@ +package fanout + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/fanout" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*fanout.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return fanout.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*fanout.EnableOutput, error) { + return fanout.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*fanout.EnableOutput, error) { + return fanout.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/fanout/disable.go b/pkg/commands/product/fanout/disable.go new file mode 100644 index 000000000..3e31a8ecc --- /dev/null +++ b/pkg/commands/product/fanout/disable.go @@ -0,0 +1,28 @@ +package fanout + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/fanout" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*fanout.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, fanout.ProductID, fanout.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/fanout/doc.go b/pkg/commands/product/fanout/doc.go new file mode 100644 index 000000000..84f696f55 --- /dev/null +++ b/pkg/commands/product/fanout/doc.go @@ -0,0 +1,3 @@ +// Package fanout contains commands to enable and disable the +// Fastly Fanout product. +package fanout diff --git a/pkg/commands/product/fanout/enable.go b/pkg/commands/product/fanout/enable.go new file mode 100644 index 000000000..7fbcd662f --- /dev/null +++ b/pkg/commands/product/fanout/enable.go @@ -0,0 +1,28 @@ +package fanout + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/fanout" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*fanout.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, fanout.ProductID, fanout.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/fanout/product_test.go b/pkg/commands/product/fanout/product_test.go new file mode 100644 index 000000000..408edfc05 --- /dev/null +++ b/pkg/commands/product/fanout/product_test.go @@ -0,0 +1,20 @@ +package fanout_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/fanout" + "github.com/fastly/go-fastly/v9/fastly/products/fanout" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*fanout.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: fanout.ProductID, + ProductName: fanout.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/fanout/root.go b/pkg/commands/product/fanout/root.go new file mode 100644 index 000000000..f4b76d531 --- /dev/null +++ b/pkg/commands/product/fanout/root.go @@ -0,0 +1,31 @@ +package fanout + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "fanout" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Fanout product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/fanout/status.go b/pkg/commands/product/fanout/status.go new file mode 100644 index 000000000..b71ad24b4 --- /dev/null +++ b/pkg/commands/product/fanout/status.go @@ -0,0 +1,28 @@ +package fanout + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/fanout" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*fanout.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, fanout.ProductID, fanout.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/imageoptimizer/common.go b/pkg/commands/product/imageoptimizer/common.go new file mode 100644 index 000000000..79704110b --- /dev/null +++ b/pkg/commands/product/imageoptimizer/common.go @@ -0,0 +1,23 @@ +package imageoptimizer + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*imageoptimizer.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return imageoptimizer.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*imageoptimizer.EnableOutput, error) { + return imageoptimizer.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*imageoptimizer.EnableOutput, error) { + return imageoptimizer.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/imageoptimizer/disable.go b/pkg/commands/product/imageoptimizer/disable.go new file mode 100644 index 000000000..7b2a55473 --- /dev/null +++ b/pkg/commands/product/imageoptimizer/disable.go @@ -0,0 +1,28 @@ +package imageoptimizer + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*imageoptimizer.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, imageoptimizer.ProductID, imageoptimizer.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/imageoptimizer/doc.go b/pkg/commands/product/imageoptimizer/doc.go new file mode 100644 index 000000000..29fb55985 --- /dev/null +++ b/pkg/commands/product/imageoptimizer/doc.go @@ -0,0 +1,3 @@ +// Package imageoptimizer contains commands to enable and disable the +// Fastly Image Optimizer product. +package imageoptimizer diff --git a/pkg/commands/product/imageoptimizer/enable.go b/pkg/commands/product/imageoptimizer/enable.go new file mode 100644 index 000000000..44ec9d894 --- /dev/null +++ b/pkg/commands/product/imageoptimizer/enable.go @@ -0,0 +1,28 @@ +package imageoptimizer + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*imageoptimizer.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, imageoptimizer.ProductID, imageoptimizer.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/imageoptimizer/product_test.go b/pkg/commands/product/imageoptimizer/product_test.go new file mode 100644 index 000000000..9732acc7f --- /dev/null +++ b/pkg/commands/product/imageoptimizer/product_test.go @@ -0,0 +1,20 @@ +package imageoptimizer_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/imageoptimizer" + "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*imageoptimizer.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: imageoptimizer.ProductID, + ProductName: imageoptimizer.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/imageoptimizer/root.go b/pkg/commands/product/imageoptimizer/root.go new file mode 100644 index 000000000..7de221fc9 --- /dev/null +++ b/pkg/commands/product/imageoptimizer/root.go @@ -0,0 +1,31 @@ +package imageoptimizer + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "image_optimizer" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Image Optimizer product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/imageoptimizer/status.go b/pkg/commands/product/imageoptimizer/status.go new file mode 100644 index 000000000..e3d470bde --- /dev/null +++ b/pkg/commands/product/imageoptimizer/status.go @@ -0,0 +1,28 @@ +package imageoptimizer + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*imageoptimizer.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, imageoptimizer.ProductID, imageoptimizer.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/logexplorerinsights/common.go b/pkg/commands/product/logexplorerinsights/common.go new file mode 100644 index 000000000..4b13e044b --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/common.go @@ -0,0 +1,23 @@ +package logexplorerinsights + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*logexplorerinsights.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return logexplorerinsights.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*logexplorerinsights.EnableOutput, error) { + return logexplorerinsights.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*logexplorerinsights.EnableOutput, error) { + return logexplorerinsights.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/logexplorerinsights/disable.go b/pkg/commands/product/logexplorerinsights/disable.go new file mode 100644 index 000000000..a549661f4 --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/disable.go @@ -0,0 +1,28 @@ +package logexplorerinsights + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*logexplorerinsights.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, logexplorerinsights.ProductID, logexplorerinsights.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/logexplorerinsights/doc.go b/pkg/commands/product/logexplorerinsights/doc.go new file mode 100644 index 000000000..e22fc0b7f --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/doc.go @@ -0,0 +1,3 @@ +// Package logexplorerinsights contains commands to enable and disable the +// Fastly Log Explorer & Insights product. +package logexplorerinsights diff --git a/pkg/commands/product/logexplorerinsights/enable.go b/pkg/commands/product/logexplorerinsights/enable.go new file mode 100644 index 000000000..1f1a05ffb --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/enable.go @@ -0,0 +1,28 @@ +package logexplorerinsights + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*logexplorerinsights.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, logexplorerinsights.ProductID, logexplorerinsights.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/logexplorerinsights/product_test.go b/pkg/commands/product/logexplorerinsights/product_test.go new file mode 100644 index 000000000..7411c9e6e --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/product_test.go @@ -0,0 +1,20 @@ +package logexplorerinsights_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/logexplorerinsights" + "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*logexplorerinsights.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: logexplorerinsights.ProductID, + ProductName: logexplorerinsights.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/logexplorerinsights/root.go b/pkg/commands/product/logexplorerinsights/root.go new file mode 100644 index 000000000..6268be92b --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/root.go @@ -0,0 +1,31 @@ +package logexplorerinsights + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "log_explorer_insights" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Log Explorer & Insights product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/logexplorerinsights/status.go b/pkg/commands/product/logexplorerinsights/status.go new file mode 100644 index 000000000..6829c168c --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/status.go @@ -0,0 +1,28 @@ +package logexplorerinsights + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*logexplorerinsights.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, logexplorerinsights.ProductID, logexplorerinsights.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/origininspector/common.go b/pkg/commands/product/origininspector/common.go new file mode 100644 index 000000000..2f31a68ff --- /dev/null +++ b/pkg/commands/product/origininspector/common.go @@ -0,0 +1,23 @@ +package origininspector + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/origininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*origininspector.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return origininspector.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*origininspector.EnableOutput, error) { + return origininspector.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*origininspector.EnableOutput, error) { + return origininspector.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/origininspector/disable.go b/pkg/commands/product/origininspector/disable.go new file mode 100644 index 000000000..acc390d38 --- /dev/null +++ b/pkg/commands/product/origininspector/disable.go @@ -0,0 +1,28 @@ +package origininspector + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/origininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*origininspector.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, origininspector.ProductID, origininspector.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/origininspector/doc.go b/pkg/commands/product/origininspector/doc.go new file mode 100644 index 000000000..72d198a1b --- /dev/null +++ b/pkg/commands/product/origininspector/doc.go @@ -0,0 +1,3 @@ +// Package origininspector contains commands to enable and disable the +// Fastly Origin Inspector product. +package origininspector diff --git a/pkg/commands/product/origininspector/enable.go b/pkg/commands/product/origininspector/enable.go new file mode 100644 index 000000000..a28d16ae8 --- /dev/null +++ b/pkg/commands/product/origininspector/enable.go @@ -0,0 +1,28 @@ +package origininspector + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/origininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*origininspector.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, origininspector.ProductID, origininspector.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/origininspector/product_test.go b/pkg/commands/product/origininspector/product_test.go new file mode 100644 index 000000000..750047803 --- /dev/null +++ b/pkg/commands/product/origininspector/product_test.go @@ -0,0 +1,20 @@ +package origininspector_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/origininspector" + "github.com/fastly/go-fastly/v9/fastly/products/origininspector" +) + +func TestProductEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*origininspector.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: origininspector.ProductID, + ProductName: origininspector.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/origininspector/root.go b/pkg/commands/product/origininspector/root.go new file mode 100644 index 000000000..db8f3f24f --- /dev/null +++ b/pkg/commands/product/origininspector/root.go @@ -0,0 +1,31 @@ +package origininspector + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "origin_inspector" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Origin Inspector product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/origininspector/status.go b/pkg/commands/product/origininspector/status.go new file mode 100644 index 000000000..430459a6b --- /dev/null +++ b/pkg/commands/product/origininspector/status.go @@ -0,0 +1,28 @@ +package origininspector + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/origininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*origininspector.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, origininspector.ProductID, origininspector.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/websockets/common.go b/pkg/commands/product/websockets/common.go new file mode 100644 index 000000000..598dfed3e --- /dev/null +++ b/pkg/commands/product/websockets/common.go @@ -0,0 +1,23 @@ +package websockets + +import ( + "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products/websockets" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*websockets.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return websockets.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*websockets.EnableOutput, error) { + return websockets.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*websockets.EnableOutput, error) { + return websockets.Get(client.(*fastly.Client), serviceID) + }, +} diff --git a/pkg/commands/product/websockets/disable.go b/pkg/commands/product/websockets/disable.go new file mode 100644 index 000000000..d3b369483 --- /dev/null +++ b/pkg/commands/product/websockets/disable.go @@ -0,0 +1,28 @@ +package websockets + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/websockets" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*websockets.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, websockets.ProductID, websockets.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} diff --git a/pkg/commands/product/websockets/doc.go b/pkg/commands/product/websockets/doc.go new file mode 100644 index 000000000..69aa77cc4 --- /dev/null +++ b/pkg/commands/product/websockets/doc.go @@ -0,0 +1,3 @@ +// Package websockets contains commands to enable and disable the +// Fastly WebSockets product. +package websockets diff --git a/pkg/commands/product/websockets/enable.go b/pkg/commands/product/websockets/enable.go new file mode 100644 index 000000000..72c5a751a --- /dev/null +++ b/pkg/commands/product/websockets/enable.go @@ -0,0 +1,28 @@ +package websockets + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/websockets" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*websockets.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, websockets.ProductID, websockets.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} diff --git a/pkg/commands/product/websockets/product_test.go b/pkg/commands/product/websockets/product_test.go new file mode 100644 index 000000000..0158d3f15 --- /dev/null +++ b/pkg/commands/product/websockets/product_test.go @@ -0,0 +1,20 @@ +package websockets_test + +import ( + "testing" + + "github.com/fastly/cli/internal/productcore_test" + root "github.com/fastly/cli/pkg/commands/product" + sub "github.com/fastly/cli/pkg/commands/product/websockets" + "github.com/fastly/go-fastly/v9/fastly/products/websockets" +) + +func TestWebSocketsEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*websockets.EnableOutput]{ + T: t, + Commands: []string{root.CommandName, sub.CommandName}, + ProductID: websockets.ProductID, + ProductName: websockets.ProductName, + Hooks: &sub.EnablementHooks, + }) +} diff --git a/pkg/commands/product/websockets/root.go b/pkg/commands/product/websockets/root.go new file mode 100644 index 000000000..438ee61e5 --- /dev/null +++ b/pkg/commands/product/websockets/root.go @@ -0,0 +1,31 @@ +package websockets + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "websockets" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly WebSockets product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} diff --git a/pkg/commands/product/websockets/status.go b/pkg/commands/product/websockets/status.go new file mode 100644 index 000000000..8a5c63caf --- /dev/null +++ b/pkg/commands/product/websockets/status.go @@ -0,0 +1,28 @@ +package websockets + +import ( + "io" + + "github.com/fastly/go-fastly/v9/fastly/products/websockets" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" +) + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*websockets.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, websockets.ProductID, websockets.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} From cfb53fe1ba32f362ffebd314508468b71d53398b Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 13 Jan 2025 16:43:02 -0500 Subject: [PATCH 8/9] Review feedback... removing unnecessary source files. --- internal/productcore/disable.go | 3 + internal/productcore/doc.go | 4 + internal/productcore/enable.go | 3 + internal/productcore/status.go | 3 + internal/productcore_test/doc.go | 4 + .../product/botmanagement/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 8 +- pkg/commands/product/botmanagement/common.go | 23 ---- pkg/commands/product/botmanagement/disable.go | 28 ----- pkg/commands/product/botmanagement/enable.go | 28 ----- pkg/commands/product/botmanagement/root.go | 31 ------ pkg/commands/product/botmanagement/status.go | 28 ----- .../product/brotlicompression/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- .../product/brotlicompression/common.go | 23 ---- .../product/brotlicompression/disable.go | 28 ----- .../product/brotlicompression/enable.go | 28 ----- .../product/brotlicompression/root.go | 31 ------ .../product/brotlicompression/status.go | 28 ----- .../product/ddosprotection/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- pkg/commands/product/ddosprotection/common.go | 23 ---- .../product/ddosprotection/disable.go | 28 ----- pkg/commands/product/ddosprotection/enable.go | 28 ----- pkg/commands/product/ddosprotection/root.go | 31 ------ pkg/commands/product/ddosprotection/status.go | 28 ----- .../product/domaininspector/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- .../product/domaininspector/common.go | 23 ---- .../product/domaininspector/disable.go | 28 ----- .../product/domaininspector/enable.go | 28 ----- pkg/commands/product/domaininspector/root.go | 31 ------ .../product/domaininspector/status.go | 28 ----- pkg/commands/product/fanout/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- pkg/commands/product/fanout/common.go | 23 ---- pkg/commands/product/fanout/disable.go | 28 ----- pkg/commands/product/fanout/enable.go | 28 ----- pkg/commands/product/fanout/root.go | 31 ------ pkg/commands/product/fanout/status.go | 28 ----- .../product/imageoptimizer/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- pkg/commands/product/imageoptimizer/common.go | 23 ---- .../product/imageoptimizer/disable.go | 28 ----- pkg/commands/product/imageoptimizer/enable.go | 28 ----- pkg/commands/product/imageoptimizer/root.go | 31 ------ pkg/commands/product/imageoptimizer/status.go | 28 ----- .../product/logexplorerinsights/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- .../product/logexplorerinsights/common.go | 23 ---- .../product/logexplorerinsights/disable.go | 28 ----- .../product/logexplorerinsights/enable.go | 28 ----- .../product/logexplorerinsights/root.go | 31 ------ .../product/logexplorerinsights/status.go | 28 ----- .../product/origininspector/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 10 +- .../product/origininspector/common.go | 23 ---- .../product/origininspector/disable.go | 28 ----- .../product/origininspector/enable.go | 28 ----- pkg/commands/product/origininspector/root.go | 31 ------ .../product/origininspector/status.go | 28 ----- pkg/commands/product/websockets/commands.go | 101 ++++++++++++++++++ .../{product_test.go => commands_test.go} | 8 +- pkg/commands/product/websockets/common.go | 23 ---- pkg/commands/product/websockets/disable.go | 28 ----- pkg/commands/product/websockets/enable.go | 28 ----- pkg/commands/product/websockets/root.go | 31 ------ pkg/commands/product/websockets/status.go | 28 ----- 68 files changed, 969 insertions(+), 1285 deletions(-) create mode 100644 internal/productcore/doc.go create mode 100644 internal/productcore_test/doc.go create mode 100644 pkg/commands/product/botmanagement/commands.go rename pkg/commands/product/botmanagement/{product_test.go => commands_test.go} (71%) delete mode 100644 pkg/commands/product/botmanagement/common.go delete mode 100644 pkg/commands/product/botmanagement/disable.go delete mode 100644 pkg/commands/product/botmanagement/enable.go delete mode 100644 pkg/commands/product/botmanagement/root.go delete mode 100644 pkg/commands/product/botmanagement/status.go create mode 100644 pkg/commands/product/brotlicompression/commands.go rename pkg/commands/product/brotlicompression/{product_test.go => commands_test.go} (63%) delete mode 100644 pkg/commands/product/brotlicompression/common.go delete mode 100644 pkg/commands/product/brotlicompression/disable.go delete mode 100644 pkg/commands/product/brotlicompression/enable.go delete mode 100644 pkg/commands/product/brotlicompression/root.go delete mode 100644 pkg/commands/product/brotlicompression/status.go create mode 100644 pkg/commands/product/ddosprotection/commands.go rename pkg/commands/product/ddosprotection/{product_test.go => commands_test.go} (63%) delete mode 100644 pkg/commands/product/ddosprotection/common.go delete mode 100644 pkg/commands/product/ddosprotection/disable.go delete mode 100644 pkg/commands/product/ddosprotection/enable.go delete mode 100644 pkg/commands/product/ddosprotection/root.go delete mode 100644 pkg/commands/product/ddosprotection/status.go create mode 100644 pkg/commands/product/domaininspector/commands.go rename pkg/commands/product/domaininspector/{product_test.go => commands_test.go} (63%) delete mode 100644 pkg/commands/product/domaininspector/common.go delete mode 100644 pkg/commands/product/domaininspector/disable.go delete mode 100644 pkg/commands/product/domaininspector/enable.go delete mode 100644 pkg/commands/product/domaininspector/root.go delete mode 100644 pkg/commands/product/domaininspector/status.go create mode 100644 pkg/commands/product/fanout/commands.go rename pkg/commands/product/fanout/{product_test.go => commands_test.go} (65%) delete mode 100644 pkg/commands/product/fanout/common.go delete mode 100644 pkg/commands/product/fanout/disable.go delete mode 100644 pkg/commands/product/fanout/enable.go delete mode 100644 pkg/commands/product/fanout/root.go delete mode 100644 pkg/commands/product/fanout/status.go create mode 100644 pkg/commands/product/imageoptimizer/commands.go rename pkg/commands/product/imageoptimizer/{product_test.go => commands_test.go} (63%) delete mode 100644 pkg/commands/product/imageoptimizer/common.go delete mode 100644 pkg/commands/product/imageoptimizer/disable.go delete mode 100644 pkg/commands/product/imageoptimizer/enable.go delete mode 100644 pkg/commands/product/imageoptimizer/root.go delete mode 100644 pkg/commands/product/imageoptimizer/status.go create mode 100644 pkg/commands/product/logexplorerinsights/commands.go rename pkg/commands/product/logexplorerinsights/{product_test.go => commands_test.go} (62%) delete mode 100644 pkg/commands/product/logexplorerinsights/common.go delete mode 100644 pkg/commands/product/logexplorerinsights/disable.go delete mode 100644 pkg/commands/product/logexplorerinsights/enable.go delete mode 100644 pkg/commands/product/logexplorerinsights/root.go delete mode 100644 pkg/commands/product/logexplorerinsights/status.go create mode 100644 pkg/commands/product/origininspector/commands.go rename pkg/commands/product/origininspector/{product_test.go => commands_test.go} (63%) delete mode 100644 pkg/commands/product/origininspector/common.go delete mode 100644 pkg/commands/product/origininspector/disable.go delete mode 100644 pkg/commands/product/origininspector/enable.go delete mode 100644 pkg/commands/product/origininspector/root.go delete mode 100644 pkg/commands/product/origininspector/status.go create mode 100644 pkg/commands/product/websockets/commands.go rename pkg/commands/product/websockets/{product_test.go => commands_test.go} (72%) delete mode 100644 pkg/commands/product/websockets/common.go delete mode 100644 pkg/commands/product/websockets/disable.go delete mode 100644 pkg/commands/product/websockets/enable.go delete mode 100644 pkg/commands/product/websockets/root.go delete mode 100644 pkg/commands/product/websockets/status.go diff --git a/internal/productcore/disable.go b/internal/productcore/disable.go index ea211918b..53211423e 100644 --- a/internal/productcore/disable.go +++ b/internal/productcore/disable.go @@ -13,6 +13,9 @@ import ( // Disable is a base type for all 'disable' commands. type Disable[O any] struct { Base + // hooks is a pointer to an EnablementHookFuncs structure so + // that tests can modify the contents of the structure after + // this structure has been initialized hooks *EnablementHookFuncs[O] } diff --git a/internal/productcore/doc.go b/internal/productcore/doc.go new file mode 100644 index 000000000..11b453c3b --- /dev/null +++ b/internal/productcore/doc.go @@ -0,0 +1,4 @@ +// Package productcore contains a group of generic structures and +// functions which are used to implement common behaviors in product +// enablement/configuration commands +package productcore diff --git a/internal/productcore/enable.go b/internal/productcore/enable.go index a26327307..88907094a 100644 --- a/internal/productcore/enable.go +++ b/internal/productcore/enable.go @@ -12,6 +12,9 @@ import ( // Enable is a base type for all 'enable' commands. type Enable[O any] struct { Base + // hooks is a pointer to an EnablementHookFuncs structure so + // that tests can modify the contents of the structure after + // this structure has been initialized hooks *EnablementHookFuncs[O] } diff --git a/internal/productcore/status.go b/internal/productcore/status.go index 60c10a486..e1de419e1 100644 --- a/internal/productcore/status.go +++ b/internal/productcore/status.go @@ -14,6 +14,9 @@ import ( // Status is a base type for all 'status' commands. type Status[O any] struct { Base + // hooks is a pointer to an EnablementHookFuncs structure so + // that tests can modify the contents of the structure after + // this structure has been initialized hooks *EnablementHookFuncs[O] } diff --git a/internal/productcore_test/doc.go b/internal/productcore_test/doc.go new file mode 100644 index 000000000..ef5ae533f --- /dev/null +++ b/internal/productcore_test/doc.go @@ -0,0 +1,4 @@ +// Package productcore_test contains a group of generic structures and +// functions which are used to implement common behaviors in product +// enablement/configuration tests +package productcore_test diff --git a/pkg/commands/product/botmanagement/commands.go b/pkg/commands/product/botmanagement/commands.go new file mode 100644 index 000000000..84359c076 --- /dev/null +++ b/pkg/commands/product/botmanagement/commands.go @@ -0,0 +1,101 @@ +package botmanagement + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "bot_management" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Bot Management product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/botmanagement/product_test.go b/pkg/commands/product/botmanagement/commands_test.go similarity index 71% rename from pkg/commands/product/botmanagement/product_test.go rename to pkg/commands/product/botmanagement/commands_test.go index ec403c77b..b62b55274 100644 --- a/pkg/commands/product/botmanagement/product_test.go +++ b/pkg/commands/product/botmanagement/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/botmanagement" - "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" + product "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" ) func TestBotManagementEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*botmanagement.EnableOutput]{ + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: botmanagement.ProductID, - ProductName: botmanagement.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/botmanagement/common.go b/pkg/commands/product/botmanagement/common.go deleted file mode 100644 index 53eb554a7..000000000 --- a/pkg/commands/product/botmanagement/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package botmanagement - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*botmanagement.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return botmanagement.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*botmanagement.EnableOutput, error) { - return botmanagement.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*botmanagement.EnableOutput, error) { - return botmanagement.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/botmanagement/disable.go b/pkg/commands/product/botmanagement/disable.go deleted file mode 100644 index 983d6812e..000000000 --- a/pkg/commands/product/botmanagement/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package botmanagement - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*botmanagement.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, botmanagement.ProductID, botmanagement.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/botmanagement/enable.go b/pkg/commands/product/botmanagement/enable.go deleted file mode 100644 index 613affe4d..000000000 --- a/pkg/commands/product/botmanagement/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package botmanagement - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*botmanagement.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, botmanagement.ProductID, botmanagement.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/botmanagement/root.go b/pkg/commands/product/botmanagement/root.go deleted file mode 100644 index c0cbae4a2..000000000 --- a/pkg/commands/product/botmanagement/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package botmanagement - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "bot_management" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Bot Management product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/botmanagement/status.go b/pkg/commands/product/botmanagement/status.go deleted file mode 100644 index f9655b4cd..000000000 --- a/pkg/commands/product/botmanagement/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package botmanagement - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*botmanagement.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, botmanagement.ProductID, botmanagement.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/brotlicompression/commands.go b/pkg/commands/product/brotlicompression/commands.go new file mode 100644 index 000000000..a2dafda6a --- /dev/null +++ b/pkg/commands/product/brotlicompression/commands.go @@ -0,0 +1,101 @@ +package brotlicompression + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "brotli_compression" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Brotli Compression product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/brotlicompression/product_test.go b/pkg/commands/product/brotlicompression/commands_test.go similarity index 63% rename from pkg/commands/product/brotlicompression/product_test.go rename to pkg/commands/product/brotlicompression/commands_test.go index 50a1f97da..072b26d05 100644 --- a/pkg/commands/product/brotlicompression/product_test.go +++ b/pkg/commands/product/brotlicompression/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/brotlicompression" - "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" + product "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*brotlicompression.EnableOutput]{ +func TestBrotliCompressionEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: brotlicompression.ProductID, - ProductName: brotlicompression.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/brotlicompression/common.go b/pkg/commands/product/brotlicompression/common.go deleted file mode 100644 index 109103319..000000000 --- a/pkg/commands/product/brotlicompression/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package brotlicompression - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*brotlicompression.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return brotlicompression.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*brotlicompression.EnableOutput, error) { - return brotlicompression.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*brotlicompression.EnableOutput, error) { - return brotlicompression.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/brotlicompression/disable.go b/pkg/commands/product/brotlicompression/disable.go deleted file mode 100644 index 175de0efa..000000000 --- a/pkg/commands/product/brotlicompression/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package brotlicompression - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*brotlicompression.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, brotlicompression.ProductID, brotlicompression.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/brotlicompression/enable.go b/pkg/commands/product/brotlicompression/enable.go deleted file mode 100644 index 7d3748352..000000000 --- a/pkg/commands/product/brotlicompression/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package brotlicompression - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*brotlicompression.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, brotlicompression.ProductID, brotlicompression.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/brotlicompression/root.go b/pkg/commands/product/brotlicompression/root.go deleted file mode 100644 index 470fd054d..000000000 --- a/pkg/commands/product/brotlicompression/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package brotlicompression - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "brotli_compression" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Brotli Compression product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/brotlicompression/status.go b/pkg/commands/product/brotlicompression/status.go deleted file mode 100644 index a81a216bf..000000000 --- a/pkg/commands/product/brotlicompression/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package brotlicompression - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*brotlicompression.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, brotlicompression.ProductID, brotlicompression.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/ddosprotection/commands.go b/pkg/commands/product/ddosprotection/commands.go new file mode 100644 index 000000000..9fed94dc8 --- /dev/null +++ b/pkg/commands/product/ddosprotection/commands.go @@ -0,0 +1,101 @@ +package ddosprotection + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "ddos_protection" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the DDoS Protection product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/ddosprotection/product_test.go b/pkg/commands/product/ddosprotection/commands_test.go similarity index 63% rename from pkg/commands/product/ddosprotection/product_test.go rename to pkg/commands/product/ddosprotection/commands_test.go index 6899fee49..91f4f3c2f 100644 --- a/pkg/commands/product/ddosprotection/product_test.go +++ b/pkg/commands/product/ddosprotection/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/ddosprotection" - "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" + product "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*ddosprotection.EnableOutput]{ +func TestDDoSProtectionEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: ddosprotection.ProductID, - ProductName: ddosprotection.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/ddosprotection/common.go b/pkg/commands/product/ddosprotection/common.go deleted file mode 100644 index c039d3b9b..000000000 --- a/pkg/commands/product/ddosprotection/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package ddosprotection - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*ddosprotection.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return ddosprotection.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*ddosprotection.EnableOutput, error) { - return ddosprotection.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*ddosprotection.EnableOutput, error) { - return ddosprotection.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/ddosprotection/disable.go b/pkg/commands/product/ddosprotection/disable.go deleted file mode 100644 index e62667c63..000000000 --- a/pkg/commands/product/ddosprotection/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package ddosprotection - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*ddosprotection.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, ddosprotection.ProductID, ddosprotection.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/ddosprotection/enable.go b/pkg/commands/product/ddosprotection/enable.go deleted file mode 100644 index fd70474a2..000000000 --- a/pkg/commands/product/ddosprotection/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package ddosprotection - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*ddosprotection.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, ddosprotection.ProductID, ddosprotection.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/ddosprotection/root.go b/pkg/commands/product/ddosprotection/root.go deleted file mode 100644 index 93003a6d4..000000000 --- a/pkg/commands/product/ddosprotection/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package ddosprotection - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "ddos_protection" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly DDoS Protection product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/ddosprotection/status.go b/pkg/commands/product/ddosprotection/status.go deleted file mode 100644 index 5fa815ae6..000000000 --- a/pkg/commands/product/ddosprotection/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package ddosprotection - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*ddosprotection.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, ddosprotection.ProductID, ddosprotection.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/domaininspector/commands.go b/pkg/commands/product/domaininspector/commands.go new file mode 100644 index 000000000..967429028 --- /dev/null +++ b/pkg/commands/product/domaininspector/commands.go @@ -0,0 +1,101 @@ +package domaininspector + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "domain_inspector" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Domain Inspector product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/domaininspector/product_test.go b/pkg/commands/product/domaininspector/commands_test.go similarity index 63% rename from pkg/commands/product/domaininspector/product_test.go rename to pkg/commands/product/domaininspector/commands_test.go index f9ae0c86f..cda8337dc 100644 --- a/pkg/commands/product/domaininspector/product_test.go +++ b/pkg/commands/product/domaininspector/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/domaininspector" - "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" + product "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*domaininspector.EnableOutput]{ +func TestDomainInspectorEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: domaininspector.ProductID, - ProductName: domaininspector.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/domaininspector/common.go b/pkg/commands/product/domaininspector/common.go deleted file mode 100644 index 8b6d1c500..000000000 --- a/pkg/commands/product/domaininspector/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package domaininspector - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*domaininspector.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return domaininspector.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*domaininspector.EnableOutput, error) { - return domaininspector.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*domaininspector.EnableOutput, error) { - return domaininspector.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/domaininspector/disable.go b/pkg/commands/product/domaininspector/disable.go deleted file mode 100644 index 68512c52c..000000000 --- a/pkg/commands/product/domaininspector/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package domaininspector - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*domaininspector.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, domaininspector.ProductID, domaininspector.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/domaininspector/enable.go b/pkg/commands/product/domaininspector/enable.go deleted file mode 100644 index 2667594a9..000000000 --- a/pkg/commands/product/domaininspector/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package domaininspector - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*domaininspector.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, domaininspector.ProductID, domaininspector.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/domaininspector/root.go b/pkg/commands/product/domaininspector/root.go deleted file mode 100644 index 48d5a7a90..000000000 --- a/pkg/commands/product/domaininspector/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package domaininspector - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "domain_inspector" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Domain Inspector product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/domaininspector/status.go b/pkg/commands/product/domaininspector/status.go deleted file mode 100644 index ef0de728e..000000000 --- a/pkg/commands/product/domaininspector/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package domaininspector - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*domaininspector.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, domaininspector.ProductID, domaininspector.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/fanout/commands.go b/pkg/commands/product/fanout/commands.go new file mode 100644 index 000000000..445e50083 --- /dev/null +++ b/pkg/commands/product/fanout/commands.go @@ -0,0 +1,101 @@ +package fanout + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/fanout" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "fanout" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Fanout product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/fanout/product_test.go b/pkg/commands/product/fanout/commands_test.go similarity index 65% rename from pkg/commands/product/fanout/product_test.go rename to pkg/commands/product/fanout/commands_test.go index 408edfc05..67963e1c8 100644 --- a/pkg/commands/product/fanout/product_test.go +++ b/pkg/commands/product/fanout/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/fanout" - "github.com/fastly/go-fastly/v9/fastly/products/fanout" + product "github.com/fastly/go-fastly/v9/fastly/products/fanout" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*fanout.EnableOutput]{ +func TestFanoutEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: fanout.ProductID, - ProductName: fanout.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/fanout/common.go b/pkg/commands/product/fanout/common.go deleted file mode 100644 index 0ccf4a77c..000000000 --- a/pkg/commands/product/fanout/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package fanout - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/fanout" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*fanout.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return fanout.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*fanout.EnableOutput, error) { - return fanout.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*fanout.EnableOutput, error) { - return fanout.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/fanout/disable.go b/pkg/commands/product/fanout/disable.go deleted file mode 100644 index 3e31a8ecc..000000000 --- a/pkg/commands/product/fanout/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package fanout - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/fanout" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*fanout.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, fanout.ProductID, fanout.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/fanout/enable.go b/pkg/commands/product/fanout/enable.go deleted file mode 100644 index 7fbcd662f..000000000 --- a/pkg/commands/product/fanout/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package fanout - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/fanout" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*fanout.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, fanout.ProductID, fanout.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/fanout/root.go b/pkg/commands/product/fanout/root.go deleted file mode 100644 index f4b76d531..000000000 --- a/pkg/commands/product/fanout/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package fanout - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "fanout" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Fanout product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/fanout/status.go b/pkg/commands/product/fanout/status.go deleted file mode 100644 index b71ad24b4..000000000 --- a/pkg/commands/product/fanout/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package fanout - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/fanout" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*fanout.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, fanout.ProductID, fanout.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/imageoptimizer/commands.go b/pkg/commands/product/imageoptimizer/commands.go new file mode 100644 index 000000000..46554f48d --- /dev/null +++ b/pkg/commands/product/imageoptimizer/commands.go @@ -0,0 +1,101 @@ +package imageoptimizer + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "image_optimizer" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Image Optimizer product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/imageoptimizer/product_test.go b/pkg/commands/product/imageoptimizer/commands_test.go similarity index 63% rename from pkg/commands/product/imageoptimizer/product_test.go rename to pkg/commands/product/imageoptimizer/commands_test.go index 9732acc7f..0b7ee2e83 100644 --- a/pkg/commands/product/imageoptimizer/product_test.go +++ b/pkg/commands/product/imageoptimizer/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/imageoptimizer" - "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" + product "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*imageoptimizer.EnableOutput]{ +func TestImageoptimizerEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: imageoptimizer.ProductID, - ProductName: imageoptimizer.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/imageoptimizer/common.go b/pkg/commands/product/imageoptimizer/common.go deleted file mode 100644 index 79704110b..000000000 --- a/pkg/commands/product/imageoptimizer/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package imageoptimizer - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*imageoptimizer.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return imageoptimizer.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*imageoptimizer.EnableOutput, error) { - return imageoptimizer.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*imageoptimizer.EnableOutput, error) { - return imageoptimizer.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/imageoptimizer/disable.go b/pkg/commands/product/imageoptimizer/disable.go deleted file mode 100644 index 7b2a55473..000000000 --- a/pkg/commands/product/imageoptimizer/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package imageoptimizer - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*imageoptimizer.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, imageoptimizer.ProductID, imageoptimizer.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/imageoptimizer/enable.go b/pkg/commands/product/imageoptimizer/enable.go deleted file mode 100644 index 44ec9d894..000000000 --- a/pkg/commands/product/imageoptimizer/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package imageoptimizer - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*imageoptimizer.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, imageoptimizer.ProductID, imageoptimizer.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/imageoptimizer/root.go b/pkg/commands/product/imageoptimizer/root.go deleted file mode 100644 index 7de221fc9..000000000 --- a/pkg/commands/product/imageoptimizer/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package imageoptimizer - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "image_optimizer" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Image Optimizer product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/imageoptimizer/status.go b/pkg/commands/product/imageoptimizer/status.go deleted file mode 100644 index e3d470bde..000000000 --- a/pkg/commands/product/imageoptimizer/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package imageoptimizer - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*imageoptimizer.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, imageoptimizer.ProductID, imageoptimizer.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/logexplorerinsights/commands.go b/pkg/commands/product/logexplorerinsights/commands.go new file mode 100644 index 000000000..0dca17c8b --- /dev/null +++ b/pkg/commands/product/logexplorerinsights/commands.go @@ -0,0 +1,101 @@ +package logexplorerinsights + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "log_explorer_insights" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Log Explorer & Insights product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/logexplorerinsights/product_test.go b/pkg/commands/product/logexplorerinsights/commands_test.go similarity index 62% rename from pkg/commands/product/logexplorerinsights/product_test.go rename to pkg/commands/product/logexplorerinsights/commands_test.go index 7411c9e6e..67ccf3930 100644 --- a/pkg/commands/product/logexplorerinsights/product_test.go +++ b/pkg/commands/product/logexplorerinsights/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/logexplorerinsights" - "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" + product "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*logexplorerinsights.EnableOutput]{ +func TestLogexplorerinsightsEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: logexplorerinsights.ProductID, - ProductName: logexplorerinsights.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/logexplorerinsights/common.go b/pkg/commands/product/logexplorerinsights/common.go deleted file mode 100644 index 4b13e044b..000000000 --- a/pkg/commands/product/logexplorerinsights/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package logexplorerinsights - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*logexplorerinsights.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return logexplorerinsights.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*logexplorerinsights.EnableOutput, error) { - return logexplorerinsights.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*logexplorerinsights.EnableOutput, error) { - return logexplorerinsights.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/logexplorerinsights/disable.go b/pkg/commands/product/logexplorerinsights/disable.go deleted file mode 100644 index a549661f4..000000000 --- a/pkg/commands/product/logexplorerinsights/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package logexplorerinsights - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*logexplorerinsights.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, logexplorerinsights.ProductID, logexplorerinsights.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/logexplorerinsights/enable.go b/pkg/commands/product/logexplorerinsights/enable.go deleted file mode 100644 index 1f1a05ffb..000000000 --- a/pkg/commands/product/logexplorerinsights/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package logexplorerinsights - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*logexplorerinsights.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, logexplorerinsights.ProductID, logexplorerinsights.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/logexplorerinsights/root.go b/pkg/commands/product/logexplorerinsights/root.go deleted file mode 100644 index 6268be92b..000000000 --- a/pkg/commands/product/logexplorerinsights/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package logexplorerinsights - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "log_explorer_insights" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Log Explorer & Insights product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/logexplorerinsights/status.go b/pkg/commands/product/logexplorerinsights/status.go deleted file mode 100644 index 6829c168c..000000000 --- a/pkg/commands/product/logexplorerinsights/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package logexplorerinsights - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*logexplorerinsights.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, logexplorerinsights.ProductID, logexplorerinsights.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/origininspector/commands.go b/pkg/commands/product/origininspector/commands.go new file mode 100644 index 000000000..aad1f2add --- /dev/null +++ b/pkg/commands/product/origininspector/commands.go @@ -0,0 +1,101 @@ +package origininspector + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/origininspector" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "origin_inspector" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the Origin Inspector product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/origininspector/product_test.go b/pkg/commands/product/origininspector/commands_test.go similarity index 63% rename from pkg/commands/product/origininspector/product_test.go rename to pkg/commands/product/origininspector/commands_test.go index 750047803..49e3cb1c0 100644 --- a/pkg/commands/product/origininspector/product_test.go +++ b/pkg/commands/product/origininspector/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/origininspector" - "github.com/fastly/go-fastly/v9/fastly/products/origininspector" + product "github.com/fastly/go-fastly/v9/fastly/products/origininspector" ) -func TestProductEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*origininspector.EnableOutput]{ +func TestOrigininspectorEnablement(t *testing.T) { + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: origininspector.ProductID, - ProductName: origininspector.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/origininspector/common.go b/pkg/commands/product/origininspector/common.go deleted file mode 100644 index 2f31a68ff..000000000 --- a/pkg/commands/product/origininspector/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package origininspector - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/origininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*origininspector.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return origininspector.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*origininspector.EnableOutput, error) { - return origininspector.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*origininspector.EnableOutput, error) { - return origininspector.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/origininspector/disable.go b/pkg/commands/product/origininspector/disable.go deleted file mode 100644 index acc390d38..000000000 --- a/pkg/commands/product/origininspector/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package origininspector - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/origininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*origininspector.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, origininspector.ProductID, origininspector.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/origininspector/enable.go b/pkg/commands/product/origininspector/enable.go deleted file mode 100644 index a28d16ae8..000000000 --- a/pkg/commands/product/origininspector/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package origininspector - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/origininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*origininspector.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, origininspector.ProductID, origininspector.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/origininspector/root.go b/pkg/commands/product/origininspector/root.go deleted file mode 100644 index db8f3f24f..000000000 --- a/pkg/commands/product/origininspector/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package origininspector - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "origin_inspector" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly Origin Inspector product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/origininspector/status.go b/pkg/commands/product/origininspector/status.go deleted file mode 100644 index 430459a6b..000000000 --- a/pkg/commands/product/origininspector/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package origininspector - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/origininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*origininspector.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, origininspector.ProductID, origininspector.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/websockets/commands.go b/pkg/commands/product/websockets/commands.go new file mode 100644 index 000000000..2b981ec2b --- /dev/null +++ b/pkg/commands/product/websockets/commands.go @@ -0,0 +1,101 @@ +package websockets + +import ( + "io" + + "github.com/fastly/cli/pkg/argparser" + "github.com/fastly/cli/pkg/global" + "github.com/fastly/go-fastly/v9/fastly" + product "github.com/fastly/go-fastly/v9/fastly/products/websockets" + + "github.com/fastly/cli/internal/productcore" + "github.com/fastly/cli/pkg/api" +) + +// EnablementHooks is a structure of dependency-injection points used +// by unit tests to provide mock behaviors +var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ + DisableFunc: func(client api.Interface, serviceID string) error { + return product.Disable(client.(*fastly.Client), serviceID) + }, + EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Enable(client.(*fastly.Client), serviceID) + }, + GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + return product.Get(client.(*fastly.Client), serviceID) + }, +} + +// RootCommand is the parent command for all subcommands in this package. +// It should be installed under the primary root command. +type RootCommand struct { + argparser.Base + // no flags +} + +// CommandName is the string to be used to invoke this command +const CommandName = "websockets" + +// NewRootCommand returns a new command registered in the parent. +func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { + var c RootCommand + c.Globals = g + c.CmdClause = parent.Command(CommandName, "Enable and disable the WebSockets product") + return &c +} + +// Exec implements the command interface. +func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { + panic("unreachable") +} + +// EnableCommand calls the Fastly API to disable the product. +type EnableCommand struct { + productcore.Enable[*product.EnableOutput] +} + +// NewEnableCommand returns a usable command registered under the parent. +func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { + c := EnableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Enable.Exec(out) +} + +// DisableCommand calls the Fastly API to disable the product. +type DisableCommand struct { + productcore.Disable[*product.EnableOutput] +} + +// NewDisableCommand returns a usable command registered under the parent. +func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { + c := DisableCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Disable.Exec(out) +} + +// StatusCommand calls the Fastly API to get the enablement status of the product. +type StatusCommand struct { + productcore.Status[*product.EnableOutput] +} + +// NewStatusCommand returns a usable command registered under the parent. +func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { + c := StatusCommand{} + c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + return &c +} + +// Exec invokes the application logic for the command. +func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { + return cmd.Status.Exec(out) +} diff --git a/pkg/commands/product/websockets/product_test.go b/pkg/commands/product/websockets/commands_test.go similarity index 72% rename from pkg/commands/product/websockets/product_test.go rename to pkg/commands/product/websockets/commands_test.go index 0158d3f15..900d4fdaf 100644 --- a/pkg/commands/product/websockets/product_test.go +++ b/pkg/commands/product/websockets/commands_test.go @@ -6,15 +6,15 @@ import ( "github.com/fastly/cli/internal/productcore_test" root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/websockets" - "github.com/fastly/go-fastly/v9/fastly/products/websockets" + product "github.com/fastly/go-fastly/v9/fastly/products/websockets" ) func TestWebSocketsEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*websockets.EnableOutput]{ + productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ T: t, Commands: []string{root.CommandName, sub.CommandName}, - ProductID: websockets.ProductID, - ProductName: websockets.ProductName, + ProductID: product.ProductID, + ProductName: product.ProductName, Hooks: &sub.EnablementHooks, }) } diff --git a/pkg/commands/product/websockets/common.go b/pkg/commands/product/websockets/common.go deleted file mode 100644 index 598dfed3e..000000000 --- a/pkg/commands/product/websockets/common.go +++ /dev/null @@ -1,23 +0,0 @@ -package websockets - -import ( - "github.com/fastly/go-fastly/v9/fastly" - "github.com/fastly/go-fastly/v9/fastly/products/websockets" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*websockets.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return websockets.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*websockets.EnableOutput, error) { - return websockets.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*websockets.EnableOutput, error) { - return websockets.Get(client.(*fastly.Client), serviceID) - }, -} diff --git a/pkg/commands/product/websockets/disable.go b/pkg/commands/product/websockets/disable.go deleted file mode 100644 index d3b369483..000000000 --- a/pkg/commands/product/websockets/disable.go +++ /dev/null @@ -1,28 +0,0 @@ -package websockets - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/websockets" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*websockets.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, websockets.ProductID, websockets.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} diff --git a/pkg/commands/product/websockets/enable.go b/pkg/commands/product/websockets/enable.go deleted file mode 100644 index 72c5a751a..000000000 --- a/pkg/commands/product/websockets/enable.go +++ /dev/null @@ -1,28 +0,0 @@ -package websockets - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/websockets" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*websockets.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, websockets.ProductID, websockets.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} diff --git a/pkg/commands/product/websockets/root.go b/pkg/commands/product/websockets/root.go deleted file mode 100644 index 438ee61e5..000000000 --- a/pkg/commands/product/websockets/root.go +++ /dev/null @@ -1,31 +0,0 @@ -package websockets - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "websockets" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fastly WebSockets product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} diff --git a/pkg/commands/product/websockets/status.go b/pkg/commands/product/websockets/status.go deleted file mode 100644 index 8a5c63caf..000000000 --- a/pkg/commands/product/websockets/status.go +++ /dev/null @@ -1,28 +0,0 @@ -package websockets - -import ( - "io" - - "github.com/fastly/go-fastly/v9/fastly/products/websockets" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" -) - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*websockets.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, websockets.ProductID, websockets.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} From 386f765ab0a7a2c475010309acc008320a7ca564 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Thu, 20 Feb 2025 09:52:27 -0500 Subject: [PATCH 9/9] More WIP --- internal/productcore/base.go | 63 +++- internal/productcore/disable.go | 23 +- internal/productcore/enable.go | 19 +- internal/productcore/status.go | 23 +- internal/productcore_test/enablement.go | 319 ++++++++++-------- pkg/commands/commands.go | 145 ++++---- .../product/botmanagement/commands.go | 25 +- .../product/botmanagement/commands_test.go | 34 +- .../product/brotlicompression/commands.go | 101 ------ .../brotlicompression/commands_test.go | 20 -- pkg/commands/product/brotlicompression/doc.go | 3 - .../product/ddosprotection/commands.go | 101 ------ .../product/ddosprotection/commands_test.go | 20 -- pkg/commands/product/ddosprotection/doc.go | 3 - .../product/domaininspector/commands.go | 101 ------ .../product/domaininspector/commands_test.go | 20 -- pkg/commands/product/domaininspector/doc.go | 3 - pkg/commands/product/fanout/commands.go | 101 ------ pkg/commands/product/fanout/commands_test.go | 20 -- pkg/commands/product/fanout/doc.go | 3 - .../product/imageoptimizer/commands.go | 101 ------ .../product/imageoptimizer/commands_test.go | 20 -- pkg/commands/product/imageoptimizer/doc.go | 3 - .../product/logexplorerinsights/commands.go | 101 ------ .../logexplorerinsights/commands_test.go | 20 -- .../product/logexplorerinsights/doc.go | 3 - .../product/origininspector/commands.go | 101 ------ .../product/origininspector/commands_test.go | 20 -- pkg/commands/product/origininspector/doc.go | 3 - pkg/commands/product/websockets/commands.go | 101 ------ .../product/websockets/commands_test.go | 20 -- pkg/commands/product/websockets/doc.go | 3 - 32 files changed, 374 insertions(+), 1269 deletions(-) delete mode 100644 pkg/commands/product/brotlicompression/commands.go delete mode 100644 pkg/commands/product/brotlicompression/commands_test.go delete mode 100644 pkg/commands/product/brotlicompression/doc.go delete mode 100644 pkg/commands/product/ddosprotection/commands.go delete mode 100644 pkg/commands/product/ddosprotection/commands_test.go delete mode 100644 pkg/commands/product/ddosprotection/doc.go delete mode 100644 pkg/commands/product/domaininspector/commands.go delete mode 100644 pkg/commands/product/domaininspector/commands_test.go delete mode 100644 pkg/commands/product/domaininspector/doc.go delete mode 100644 pkg/commands/product/fanout/commands.go delete mode 100644 pkg/commands/product/fanout/commands_test.go delete mode 100644 pkg/commands/product/fanout/doc.go delete mode 100644 pkg/commands/product/imageoptimizer/commands.go delete mode 100644 pkg/commands/product/imageoptimizer/commands_test.go delete mode 100644 pkg/commands/product/imageoptimizer/doc.go delete mode 100644 pkg/commands/product/logexplorerinsights/commands.go delete mode 100644 pkg/commands/product/logexplorerinsights/commands_test.go delete mode 100644 pkg/commands/product/logexplorerinsights/doc.go delete mode 100644 pkg/commands/product/origininspector/commands.go delete mode 100644 pkg/commands/product/origininspector/commands_test.go delete mode 100644 pkg/commands/product/origininspector/doc.go delete mode 100644 pkg/commands/product/websockets/commands.go delete mode 100644 pkg/commands/product/websockets/commands_test.go delete mode 100644 pkg/commands/product/websockets/doc.go diff --git a/internal/productcore/base.go b/internal/productcore/base.go index 01b6233b3..871f26125 100644 --- a/internal/productcore/base.go +++ b/internal/productcore/base.go @@ -1,10 +1,13 @@ package productcore import ( + "fmt" + "github.com/fastly/cli/pkg/api" "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/manifest" + "github.com/fastly/go-fastly/v9/fastly/products" ) // Base is a base type for all product commands. @@ -14,15 +17,11 @@ type Base struct { Manifest manifest.Data ServiceName argparser.OptionalServiceNameID - ProductID string - ProductName string } // Init prepares the structure for use by the CLI core. -func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productID, productName string) { +func (cmd *Base) Init(parent argparser.Registerer, g *global.Data) { cmd.Globals = g - cmd.ProductID = productID - cmd.ProductName = productName // Optional flags. cmd.RegisterFlag(argparser.StringFlagOpts{ @@ -40,16 +39,60 @@ func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productID, pr cmd.RegisterFlagBool(cmd.JSONFlag()) // --json } -// EnablementStatus is a structure used to generate JSON output from +// EnablementStatus is a structure used to generate output from // the enablement-related commands -type EnablementStatus struct { - ProductID string `json:"product_id"` - Enabled bool `json:"enabled"` +type EnablementStatus[_ products.ProductOutput] struct { + ProductName string `json:"-"` + ProductID string `json:"product_id"` + ServiceID string `json:"service_id"` + Enabled bool `json:"enabled"` +} + +type StatusManager[O products.ProductOutput] interface { + SetEnabled(bool) + GetEnabled() string + GetProductName() string + SetProductID(string) + SetServiceID(string) + TransformOutput(O) + GetTextResult() string +} + +func (s *EnablementStatus[_]) SetEnabled(e bool) { + s.Enabled = e +} + +func (s *EnablementStatus[_]) GetEnabled() string { + if s.Enabled { + return "enabled" + } + return "disabled" +} + +func (s *EnablementStatus[_]) GetProductName() string { + return s.ProductName +} + +func (s *EnablementStatus[_]) SetProductID(id string) { + s.ProductID = id +} + +func (s *EnablementStatus[_]) SetServiceID(id string) { + s.ServiceID = id +} + +func (s *EnablementStatus[O]) TransformOutput(o O) { + s.ProductID = o.ProductID() + s.ServiceID = o.ServiceID() +} + +func (s *EnablementStatus[O]) GetTextResult() string { + return fmt.Sprintf("%s is %s on service %s", s.ProductName, s.GetEnabled(), s.ServiceID) } // EnablementHookFuncs is a structure of dependency-injection points // used by unit tests to provide mock behaviors -type EnablementHookFuncs[O any] struct { +type EnablementHookFuncs[O products.ProductOutput] struct { DisableFunc func(api.Interface, string) error EnableFunc func(api.Interface, string) (O, error) GetFunc func(api.Interface, string) (O, error) diff --git a/internal/productcore/disable.go b/internal/productcore/disable.go index 53211423e..3659b050c 100644 --- a/internal/productcore/disable.go +++ b/internal/productcore/disable.go @@ -8,11 +8,13 @@ import ( "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" + "github.com/fastly/go-fastly/v9/fastly/products" ) // Disable is a base type for all 'disable' commands. -type Disable[O any] struct { +type Disable[O products.ProductOutput, _ StatusManager[O]] struct { Base + ProductID string // hooks is a pointer to an EnablementHookFuncs structure so // that tests can modify the contents of the structure after // this structure has been initialized @@ -20,15 +22,16 @@ type Disable[O any] struct { } // Init prepares the structure for use by the CLI core. -func (cmd *Disable[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { +func (cmd *Disable[O, _]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product") cmd.hooks = hooks - cmd.Base.Init(parent, g, productID, productName) + cmd.Base.Init(parent, g) + cmd.ProductID = productID } // Exec executes the disablement operation. -func (cmd *Disable[O]) Exec(out io.Writer) error { +func (cmd *Disable[O, S]) Exec(out io.Writer, status S) error { if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { return fsterr.ErrInvalidVerboseJSONCombo } @@ -49,12 +52,18 @@ func (cmd *Disable[O]) Exec(out io.Writer) error { return err } - if ok, err := cmd.WriteJSON(out, EnablementStatus{ProductID: cmd.ProductID, Enabled: false}); ok { + status.SetEnabled(false) + // The API does not return details of the service and product + // which were disabled, so they have to be inserted into + // 'status' directly + status.SetProductID(cmd.ProductID) + status.SetServiceID(serviceID) + + if ok, err := cmd.WriteJSON(out, status); ok { return err } - text.Success(out, - "Disabled %s on service %s", cmd.ProductName, serviceID) + text.Success(out, status.GetTextResult()) return nil } diff --git a/internal/productcore/enable.go b/internal/productcore/enable.go index 88907094a..6e47c4f39 100644 --- a/internal/productcore/enable.go +++ b/internal/productcore/enable.go @@ -7,10 +7,11 @@ import ( fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" + "github.com/fastly/go-fastly/v9/fastly/products" ) // Enable is a base type for all 'enable' commands. -type Enable[O any] struct { +type Enable[O products.ProductOutput, _ StatusManager[O]] struct { Base // hooks is a pointer to an EnablementHookFuncs structure so // that tests can modify the contents of the structure after @@ -19,15 +20,15 @@ type Enable[O any] struct { } // Init prepares the structure for use by the CLI core. -func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { +func (cmd *Enable[O, _]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product") cmd.hooks = hooks - cmd.Base.Init(parent, g, productID, productName) + cmd.Base.Init(parent, g) } // Exec executes the enablement operation. -func (cmd *Enable[O]) Exec(out io.Writer) error { +func (cmd *Enable[O, S]) Exec(out io.Writer, status S) error { if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { return fsterr.ErrInvalidVerboseJSONCombo } @@ -42,18 +43,20 @@ func (cmd *Enable[O]) Exec(out io.Writer) error { argparser.DisplayServiceID(serviceID, flag, source, out) } - _, err = cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID) + o, err := cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID) if err != nil { cmd.Globals.ErrLog.Add(err) return err } - if ok, err := cmd.WriteJSON(out, EnablementStatus{ProductID: cmd.ProductID, Enabled: true}); ok { + status.SetEnabled(true) + status.TransformOutput(o) + + if ok, err := cmd.WriteJSON(out, status); ok { return err } - text.Success(out, - "Enabled %s on service %s", cmd.ProductName, serviceID) + text.Success(out, status.GetTextResult()) return nil } diff --git a/internal/productcore/status.go b/internal/productcore/status.go index e1de419e1..268e8d117 100644 --- a/internal/productcore/status.go +++ b/internal/productcore/status.go @@ -9,10 +9,11 @@ import ( "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products" ) // Status is a base type for all 'status' commands. -type Status[O any] struct { +type Status[O products.ProductOutput, _ StatusManager[O]] struct { Base // hooks is a pointer to an EnablementHookFuncs structure so // that tests can modify the contents of the structure after @@ -21,15 +22,15 @@ type Status[O any] struct { } // Init prepares the structure for use by the CLI core. -func (cmd *Status[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { +func (cmd *Status[O, _]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { cmd.CmdClause = parent.Command("status", "Get the enablement status of the "+productName+" product") cmd.hooks = hooks - cmd.Base.Init(parent, g, productID, productName) + cmd.Base.Init(parent, g) } // Exec executes the status operation. -func (cmd *Status[O]) Exec(out io.Writer) error { +func (cmd *Status[O, S]) Exec(out io.Writer, status S) error { if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { return fsterr.ErrInvalidVerboseJSONCombo } @@ -44,10 +45,7 @@ func (cmd *Status[O]) Exec(out io.Writer) error { argparser.DisplayServiceID(serviceID, flag, source, out) } - s := EnablementStatus{ProductID: cmd.ProductID} - state := "disabled" - - _, err = cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID) + o, err := cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID) if err != nil { var herr *fastly.HTTPError @@ -59,16 +57,15 @@ func (cmd *Status[O]) Exec(out io.Writer) error { return err } } else { - s.Enabled = true - state = "enabled" + status.SetEnabled(true) + status.TransformOutput(o) } - if ok, err := cmd.WriteJSON(out, s); ok { + if ok, err := cmd.WriteJSON(out, status); ok { return err } - text.Info(out, - "%s is %s on service %s", cmd.ProductName, state, serviceID) + text.Info(out, status.GetTextResult()) return nil } diff --git a/internal/productcore_test/enablement.go b/internal/productcore_test/enablement.go index 9e823c633..c0d84d709 100644 --- a/internal/productcore_test/enablement.go +++ b/internal/productcore_test/enablement.go @@ -1,9 +1,11 @@ package productcore_test import ( + "strings" "testing" "github.com/fastly/go-fastly/v9/fastly" + "github.com/fastly/go-fastly/v9/fastly/products" "github.com/fastly/cli/internal/productcore" "github.com/fastly/cli/pkg/api" @@ -11,157 +13,176 @@ import ( "github.com/fastly/cli/pkg/testutil" ) -// TestEnablementInput supplies the details required for -// TestEnablement to execute a series of test scenarios. -type TestEnablementInput[O any] struct { - T *testing.T - Commands []string - ProductID string - ProductName string - Hooks *productcore.EnablementHookFuncs[O] +const TestServiceID = "123" + +type CommandRequiredArguments struct { + Command string + Arguments []string +} + +func MissingServiceIDScenarios(cra []CommandRequiredArguments) (scenarios []testutil.CLIScenario) { + for _, v := range cra { + scenario := testutil.CLIScenario{} + + scenario.Name = "validate missing Service ID: " + v.Command + scenario.Args = v.Command + for _, a := range v.Arguments { + if (!strings.HasPrefix(a, "--service-id")) { + scenario.Args += " " + a + } + } + scenario.WantError = "error reading service: no service ID found" + + scenarios = append(scenarios, scenario) + } + + return +} + +func InvalidJSONVerboseScenarios(cra []CommandRequiredArguments) (scenarios []testutil.CLIScenario) { + for _, v := range cra { + scenario := testutil.CLIScenario{} + + scenario.Name = "validate invalid json/verbose flag combo: " + v.Command + scenario.Args = v.Command + for _, a := range v.Arguments { + scenario.Args += " " + a + } + scenario.Args += " --json --verbose" + scenario.WantError = "invalid flag combination, --verbose and --json" + + scenarios = append(scenarios, scenario) + } + + return +} + +func EnableScenarios[O products.ProductOutput](cra []CommandRequiredArguments, productID, productName string, hooks *productcore.EnablementHookFuncs[O], mocker func(string) O) (scenarios []testutil.CLIScenario) { + for _, v := range cra { + if v.Command != "enable" { + continue + } + + scenario := testutil.CLIScenario{} + + scenario.Name = "validate text output success for enabling product" + scenario.Args = v.Command + for _, a := range v.Arguments { + scenario.Args += " " + a + } + scenario.Setup = func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { + hooks.EnableFunc = func(_ api.Interface, serviceID string) (O, error) { + return mocker(serviceID), nil + } + } + scenario.WantOutput = "SUCCESS: " + productName + " is enabled on service " + TestServiceID + scenarios = append(scenarios, scenario) + + scenario.Name = "validate JSON output success for enabling product" + scenario.Args += " --json" + scenario.WantOutput = "{\n \"product_id\": \"" + productID + "\",\n \"service_id\": \"" + TestServiceID + "\",\n \"enabled\": true\n}" + scenarios = append(scenarios, scenario) + + scenario.Name = "validate failure for enabling product" + scenario.Setup = func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { + hooks.EnableFunc = func(_ api.Interface, serviceID string) (O, error) { + return mocker(serviceID), testutil.Err + } + } + scenario.WantOutput = "" + scenario.WantError = "test error" + scenarios = append(scenarios, scenario) + } + + return } -// TestEnablement executes the test scenarios common to all products. -func TestEnablement[O any](i TestEnablementInput[O]) { - scenarios := []testutil.CLIScenario{ - { - Name: "validate missing Service ID: enable", - Args: "enable", - WantError: "error reading service: no service ID found", - }, - { - Name: "validate missing Service ID: disable", - Args: "enable", - WantError: "error reading service: no service ID found", - }, - { - Name: "validate missing Service ID: status", - Args: "enable", - WantError: "error reading service: no service ID found", - }, - { - Name: "validate invalid json/verbose flag combo: enable", - Args: "enable --service-id 123 --json --verbose", - WantError: "invalid flag combination, --verbose and --json", - }, - { - Name: "validate invalid json/verbose flag combo: disable", - Args: "disable --service-id 123 --json --verbose", - WantError: "invalid flag combination, --verbose and --json", - }, - { - Name: "validate invalid json/verbose flag combo: status", - Args: "status --service-id 123 --json --verbose", - WantError: "invalid flag combination, --verbose and --json", - }, - { - Name: "validate text output success for enabling product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { - return - } - }, - Args: "enable --service-id 123", - WantOutput: "SUCCESS: Enabled " + i.ProductName + " on service 123", - }, - { - Name: "validate JSON output success for enabling product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { - return - } - }, - Args: "enable --service-id 123 --json", - WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": true\n}", - }, - { - Name: "validate failure for enabling product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.EnableFunc = func(_ api.Interface, _ string) (o O, err error) { - err = testutil.Err - return - } - }, - Args: "enable --service-id 123", - WantError: "test error", - }, - { - Name: "validate text output success for disabling product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { - return nil - } - }, - Args: "disable --service-id 123", - WantOutput: "SUCCESS: Disabled " + i.ProductName + " on service 123", - }, - { - Name: "validate JSON output success for disabling product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { - return nil - } - }, - Args: "disable --service-id 123 --json", - WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": false\n}", - }, - { - Name: "validate failure for disabling product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.DisableFunc = func(_ api.Interface, _ string) error { - return testutil.Err - } - }, - Args: "disable --service-id 123", - WantError: "test error", - }, - { - Name: "validate text status output for enabled product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { - return - } - }, - Args: "status --service-id 123", - WantOutput: "INFO: " + i.ProductName + " is enabled on service 123", - }, - { - Name: "validate JSON status output for enabled product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { - return - } - }, - Args: "status --service-id 123 --json", - WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": true\n}", - }, - { - Name: "validate text status output for disabled product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { - // The API returns a 'Bad Request' error when the - // product has not been enabled on the service - err = &fastly.HTTPError{StatusCode: 400} - return - } - }, - Args: "status --service-id 123", - WantOutput: "INFO: " + i.ProductName + " is disabled on service 123", - }, - { - Name: "validate JSON status output for disabled product", - Setup: func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { - i.Hooks.GetFunc = func(_ api.Interface, _ string) (o O, err error) { - // The API returns a 'Bad Request' error when the - // product has not been enabled on the service - err = &fastly.HTTPError{StatusCode: 400} - return - } - }, - Args: "status --service-id 123 --json", - WantOutput: "{\n \"product_id\": \"" + i.ProductID + "\",\n \"enabled\": false\n}", - }, +func DisableScenarios[O products.ProductOutput](cra []CommandRequiredArguments, productID, productName string, hooks *productcore.EnablementHookFuncs[O]) (scenarios []testutil.CLIScenario) { + for _, v := range cra { + if v.Command != "disable" { + continue + } + + scenario := testutil.CLIScenario{} + + scenario.Name = "validate text output success for disabling product" + scenario.Args = v.Command + for _, a := range v.Arguments { + scenario.Args += " " + a + } + scenario.Setup = func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { + hooks.DisableFunc = func(_ api.Interface, serviceID string) error { + return nil + } + } + scenario.WantOutput = "SUCCESS: " + productName + " is disabled on service " + TestServiceID + scenarios = append(scenarios, scenario) + + scenario.Name = "validate JSON output success for disabling product" + scenario.Args += " --json" + scenario.WantOutput = "{\n \"product_id\": \"" + productID + "\",\n \"service_id\": \"" + TestServiceID + "\",\n \"enabled\": false\n}" + scenarios = append(scenarios, scenario) + + scenario.Name = "validate failure for disabling product" + scenario.Setup = func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { + hooks.DisableFunc = func(_ api.Interface, serviceID string) error { + return testutil.Err + } + } + scenario.WantOutput = "" + scenario.WantError = "test error" + scenarios = append(scenarios, scenario) + } + + return +} + +func StatusScenarios[O products.ProductOutput](cra []CommandRequiredArguments, productID, productName string, hooks *productcore.EnablementHookFuncs[O], mocker func(string) O) (scenarios []testutil.CLIScenario) { + for _, v := range cra { + if v.Command != "statu" { + continue + } + + scenario := testutil.CLIScenario{} + + scenario.Name = "validate text output for enabled product" + scenario.Args = v.Command + for _, a := range v.Arguments { + scenario.Args += " " + a + } + scenario.Setup = func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { + hooks.GetFunc = func(_ api.Interface, serviceID string) (O, error) { + return mocker(serviceID), nil + } + } + scenario.WantOutput = "INFO: " + productName + " is enabled on service " + TestServiceID + scenarios = append(scenarios, scenario) + + scenario.Name = "validate JSON output for enabled product" + scenario.Args += " --json" + scenario.WantOutput = "{\n \"product_id\": \"" + productID + "\",\n \"service_id\": \"" + TestServiceID + "\",\n \"enabled\": true\n}" + scenarios = append(scenarios, scenario) + + scenario.Name = "validate text output for disabled product" + scenario.Args = v.Command + for _, a := range v.Arguments { + scenario.Args += " " + a + } + scenario.Setup = func(_ *testing.T, _ *testutil.CLIScenario, _ *global.Data) { + hooks.GetFunc = func(_ api.Interface, serviceID string) (O, error) { + // The API returns a 'Bad Request' error when the + // product has not been enabled on the service + return mocker(serviceID), &fastly.HTTPError{StatusCode: 400} + } + } + scenario.WantOutput = "INFO: " + productName + " is disabled on service " + TestServiceID + scenarios = append(scenarios, scenario) + + scenario.Name = "validate JSON output for disabled product" + scenario.Args += " --json" + scenario.WantOutput = "{\n \"product_id\": \"" + productID + "\",\n \"service_id\": \"" + TestServiceID + "\",\n \"enabled\": false\n}" + scenarios = append(scenarios, scenario) } - testutil.RunCLIScenarios(i.T, i.Commands, scenarios) + return } diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 8cd65ef7c..00265a5e3 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -60,14 +60,15 @@ import ( "github.com/fastly/cli/pkg/commands/pop" "github.com/fastly/cli/pkg/commands/product" "github.com/fastly/cli/pkg/commands/product/botmanagement" - "github.com/fastly/cli/pkg/commands/product/brotlicompression" - "github.com/fastly/cli/pkg/commands/product/ddosprotection" - "github.com/fastly/cli/pkg/commands/product/domaininspector" - "github.com/fastly/cli/pkg/commands/product/fanout" - "github.com/fastly/cli/pkg/commands/product/imageoptimizer" - "github.com/fastly/cli/pkg/commands/product/logexplorerinsights" - "github.com/fastly/cli/pkg/commands/product/origininspector" - "github.com/fastly/cli/pkg/commands/product/websockets" + + // "github.com/fastly/cli/pkg/commands/product/brotlicompression" + // "github.com/fastly/cli/pkg/commands/product/ddosprotection" + // "github.com/fastly/cli/pkg/commands/product/domaininspector" + // "github.com/fastly/cli/pkg/commands/product/fanout" + // "github.com/fastly/cli/pkg/commands/product/imageoptimizer" + // "github.com/fastly/cli/pkg/commands/product/logexplorerinsights" + // "github.com/fastly/cli/pkg/commands/product/origininspector" + // "github.com/fastly/cli/pkg/commands/product/websockets" "github.com/fastly/cli/pkg/commands/products" "github.com/fastly/cli/pkg/commands/profile" "github.com/fastly/cli/pkg/commands/purge" @@ -416,45 +417,45 @@ func Define( // nolint:revive // function-length productBotManagementEnable := botmanagement.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data) productBotManagementStatus := botmanagement.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data) - productBrotliCompressionCmdRoot := brotlicompression.NewRootCommand(productCmdRoot.CmdClause, data) - productBrotliCompressionDisable := brotlicompression.NewDisableCommand(productBrotliCompressionCmdRoot.CmdClause, data) - productBrotliCompressionEnable := brotlicompression.NewEnableCommand(productBrotliCompressionCmdRoot.CmdClause, data) - productBrotliCompressionStatus := brotlicompression.NewStatusCommand(productBrotliCompressionCmdRoot.CmdClause, data) + // productBrotliCompressionCmdRoot := brotlicompression.NewRootCommand(productCmdRoot.CmdClause, data) + // productBrotliCompressionDisable := brotlicompression.NewDisableCommand(productBrotliCompressionCmdRoot.CmdClause, data) + // productBrotliCompressionEnable := brotlicompression.NewEnableCommand(productBrotliCompressionCmdRoot.CmdClause, data) + // productBrotliCompressionStatus := brotlicompression.NewStatusCommand(productBrotliCompressionCmdRoot.CmdClause, data) - productDDoSProtectionCmdRoot := ddosprotection.NewRootCommand(productCmdRoot.CmdClause, data) - productDDoSProtectionDisable := ddosprotection.NewDisableCommand(productDDoSProtectionCmdRoot.CmdClause, data) - productDDoSProtectionEnable := ddosprotection.NewEnableCommand(productDDoSProtectionCmdRoot.CmdClause, data) - productDDoSProtectionStatus := ddosprotection.NewStatusCommand(productDDoSProtectionCmdRoot.CmdClause, data) + // productDDoSProtectionCmdRoot := ddosprotection.NewRootCommand(productCmdRoot.CmdClause, data) + // productDDoSProtectionDisable := ddosprotection.NewDisableCommand(productDDoSProtectionCmdRoot.CmdClause, data) + // productDDoSProtectionEnable := ddosprotection.NewEnableCommand(productDDoSProtectionCmdRoot.CmdClause, data) + // productDDoSProtectionStatus := ddosprotection.NewStatusCommand(productDDoSProtectionCmdRoot.CmdClause, data) - productDomainInspectorCmdRoot := domaininspector.NewRootCommand(productCmdRoot.CmdClause, data) - productDomainInspectorDisable := domaininspector.NewDisableCommand(productDomainInspectorCmdRoot.CmdClause, data) - productDomainInspectorEnable := domaininspector.NewEnableCommand(productDomainInspectorCmdRoot.CmdClause, data) - productDomainInspectorStatus := domaininspector.NewStatusCommand(productDomainInspectorCmdRoot.CmdClause, data) + // productDomainInspectorCmdRoot := domaininspector.NewRootCommand(productCmdRoot.CmdClause, data) + // productDomainInspectorDisable := domaininspector.NewDisableCommand(productDomainInspectorCmdRoot.CmdClause, data) + // productDomainInspectorEnable := domaininspector.NewEnableCommand(productDomainInspectorCmdRoot.CmdClause, data) + // productDomainInspectorStatus := domaininspector.NewStatusCommand(productDomainInspectorCmdRoot.CmdClause, data) - productFanoutCmdRoot := fanout.NewRootCommand(productCmdRoot.CmdClause, data) - productFanoutDisable := fanout.NewDisableCommand(productFanoutCmdRoot.CmdClause, data) - productFanoutEnable := fanout.NewEnableCommand(productFanoutCmdRoot.CmdClause, data) - productFanoutStatus := fanout.NewStatusCommand(productFanoutCmdRoot.CmdClause, data) + // productFanoutCmdRoot := fanout.NewRootCommand(productCmdRoot.CmdClause, data) + // productFanoutDisable := fanout.NewDisableCommand(productFanoutCmdRoot.CmdClause, data) + // productFanoutEnable := fanout.NewEnableCommand(productFanoutCmdRoot.CmdClause, data) + // productFanoutStatus := fanout.NewStatusCommand(productFanoutCmdRoot.CmdClause, data) - productImageOptimizerCmdRoot := imageoptimizer.NewRootCommand(productCmdRoot.CmdClause, data) - productImageOptimizerDisable := imageoptimizer.NewDisableCommand(productImageOptimizerCmdRoot.CmdClause, data) - productImageOptimizerEnable := imageoptimizer.NewEnableCommand(productImageOptimizerCmdRoot.CmdClause, data) - productImageOptimizerStatus := imageoptimizer.NewStatusCommand(productImageOptimizerCmdRoot.CmdClause, data) + // productImageOptimizerCmdRoot := imageoptimizer.NewRootCommand(productCmdRoot.CmdClause, data) + // productImageOptimizerDisable := imageoptimizer.NewDisableCommand(productImageOptimizerCmdRoot.CmdClause, data) + // productImageOptimizerEnable := imageoptimizer.NewEnableCommand(productImageOptimizerCmdRoot.CmdClause, data) + // productImageOptimizerStatus := imageoptimizer.NewStatusCommand(productImageOptimizerCmdRoot.CmdClause, data) - productLogExplorerInsightsCmdRoot := logexplorerinsights.NewRootCommand(productCmdRoot.CmdClause, data) - productLogExplorerInsightsDisable := logexplorerinsights.NewDisableCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) - productLogExplorerInsightsEnable := logexplorerinsights.NewEnableCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) - productLogExplorerInsightsStatus := logexplorerinsights.NewStatusCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) + // productLogExplorerInsightsCmdRoot := logexplorerinsights.NewRootCommand(productCmdRoot.CmdClause, data) + // productLogExplorerInsightsDisable := logexplorerinsights.NewDisableCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) + // productLogExplorerInsightsEnable := logexplorerinsights.NewEnableCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) + // productLogExplorerInsightsStatus := logexplorerinsights.NewStatusCommand(productLogExplorerInsightsCmdRoot.CmdClause, data) - productOriginInspectorCmdRoot := origininspector.NewRootCommand(productCmdRoot.CmdClause, data) - productOriginInspectorDisable := origininspector.NewDisableCommand(productOriginInspectorCmdRoot.CmdClause, data) - productOriginInspectorEnable := origininspector.NewEnableCommand(productOriginInspectorCmdRoot.CmdClause, data) - productOriginInspectorStatus := origininspector.NewStatusCommand(productOriginInspectorCmdRoot.CmdClause, data) + // productOriginInspectorCmdRoot := origininspector.NewRootCommand(productCmdRoot.CmdClause, data) + // productOriginInspectorDisable := origininspector.NewDisableCommand(productOriginInspectorCmdRoot.CmdClause, data) + // productOriginInspectorEnable := origininspector.NewEnableCommand(productOriginInspectorCmdRoot.CmdClause, data) + // productOriginInspectorStatus := origininspector.NewStatusCommand(productOriginInspectorCmdRoot.CmdClause, data) - productWebSocketsCmdRoot := websockets.NewRootCommand(productCmdRoot.CmdClause, data) - productWebSocketsDisable := websockets.NewDisableCommand(productWebSocketsCmdRoot.CmdClause, data) - productWebSocketsEnable := websockets.NewEnableCommand(productWebSocketsCmdRoot.CmdClause, data) - productWebSocketsStatus := websockets.NewStatusCommand(productWebSocketsCmdRoot.CmdClause, data) + // productWebSocketsCmdRoot := websockets.NewRootCommand(productCmdRoot.CmdClause, data) + // productWebSocketsDisable := websockets.NewDisableCommand(productWebSocketsCmdRoot.CmdClause, data) + // productWebSocketsEnable := websockets.NewEnableCommand(productWebSocketsCmdRoot.CmdClause, data) + // productWebSocketsStatus := websockets.NewStatusCommand(productWebSocketsCmdRoot.CmdClause, data) productsCmdRoot := products.NewRootCommand(app, data) profileCmdRoot := profile.NewRootCommand(app, data) @@ -879,38 +880,38 @@ func Define( // nolint:revive // function-length productBotManagementDisable, productBotManagementEnable, productBotManagementStatus, - productBrotliCompressionCmdRoot, - productBrotliCompressionDisable, - productBrotliCompressionEnable, - productBrotliCompressionStatus, - productDDoSProtectionCmdRoot, - productDDoSProtectionDisable, - productDDoSProtectionEnable, - productDDoSProtectionStatus, - productDomainInspectorCmdRoot, - productDomainInspectorDisable, - productDomainInspectorEnable, - productDomainInspectorStatus, - productFanoutCmdRoot, - productFanoutDisable, - productFanoutEnable, - productFanoutStatus, - productImageOptimizerCmdRoot, - productImageOptimizerDisable, - productImageOptimizerEnable, - productImageOptimizerStatus, - productLogExplorerInsightsCmdRoot, - productLogExplorerInsightsDisable, - productLogExplorerInsightsEnable, - productLogExplorerInsightsStatus, - productOriginInspectorCmdRoot, - productOriginInspectorDisable, - productOriginInspectorEnable, - productOriginInspectorStatus, - productWebSocketsCmdRoot, - productWebSocketsDisable, - productWebSocketsEnable, - productWebSocketsStatus, + // productBrotliCompressionCmdRoot, + // productBrotliCompressionDisable, + // productBrotliCompressionEnable, + // productBrotliCompressionStatus, + // productDDoSProtectionCmdRoot, + // productDDoSProtectionDisable, + // productDDoSProtectionEnable, + // productDDoSProtectionStatus, + // productDomainInspectorCmdRoot, + // productDomainInspectorDisable, + // productDomainInspectorEnable, + // productDomainInspectorStatus, + // productFanoutCmdRoot, + // productFanoutDisable, + // productFanoutEnable, + // productFanoutStatus, + // productImageOptimizerCmdRoot, + // productImageOptimizerDisable, + // productImageOptimizerEnable, + // productImageOptimizerStatus, + // productLogExplorerInsightsCmdRoot, + // productLogExplorerInsightsDisable, + // productLogExplorerInsightsEnable, + // productLogExplorerInsightsStatus, + // productOriginInspectorCmdRoot, + // productOriginInspectorDisable, + // productOriginInspectorEnable, + // productOriginInspectorStatus, + // productWebSocketsCmdRoot, + // productWebSocketsDisable, + // productWebSocketsEnable, + // productWebSocketsStatus, productsCmdRoot, profileCmdRoot, profileCreate, diff --git a/pkg/commands/product/botmanagement/commands.go b/pkg/commands/product/botmanagement/commands.go index 84359c076..ce5182667 100644 --- a/pkg/commands/product/botmanagement/commands.go +++ b/pkg/commands/product/botmanagement/commands.go @@ -14,14 +14,14 @@ import ( // EnablementHooks is a structure of dependency-injection points used // by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ +var EnablementHooks = productcore.EnablementHookFuncs[product.EnableOutput]{ DisableFunc: func(client api.Interface, serviceID string) error { return product.Disable(client.(*fastly.Client), serviceID) }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + EnableFunc: func(client api.Interface, serviceID string) (product.EnableOutput, error) { return product.Enable(client.(*fastly.Client), serviceID) }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { + GetFunc: func(client api.Interface, serviceID string) (product.EnableOutput, error) { return product.Get(client.(*fastly.Client), serviceID) }, } @@ -51,24 +51,25 @@ func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { // EnableCommand calls the Fastly API to disable the product. type EnableCommand struct { - productcore.Enable[*product.EnableOutput] + productcore.Enable[product.EnableOutput, *productcore.EnablementStatus[product.EnableOutput]] } // NewEnableCommand returns a usable command registered under the parent. func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + c.Init(parent, g, product.ProductName, &EnablementHooks) return &c } // Exec invokes the application logic for the command. func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) + status := &productcore.EnablementStatus[product.EnableOutput]{ProductName: product.ProductName} + return cmd.Enable.Exec(out, status) } // DisableCommand calls the Fastly API to disable the product. type DisableCommand struct { - productcore.Disable[*product.EnableOutput] + productcore.Disable[product.EnableOutput, *productcore.EnablementStatus[product.EnableOutput]] } // NewDisableCommand returns a usable command registered under the parent. @@ -80,22 +81,24 @@ func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableComm // Exec invokes the application logic for the command. func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) + status := &productcore.EnablementStatus[product.EnableOutput]{ProductName: product.ProductName} + return cmd.Disable.Exec(out, status) } // StatusCommand calls the Fastly API to get the enablement status of the product. type StatusCommand struct { - productcore.Status[*product.EnableOutput] + productcore.Status[product.EnableOutput, *productcore.EnablementStatus[product.EnableOutput]] } // NewStatusCommand returns a usable command registered under the parent. func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) + c.Init(parent, g, product.ProductName, &EnablementHooks) return &c } // Exec invokes the application logic for the command. func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) + status := &productcore.EnablementStatus[product.EnableOutput]{ProductName: product.ProductName} + return cmd.Status.Exec(out, status) } diff --git a/pkg/commands/product/botmanagement/commands_test.go b/pkg/commands/product/botmanagement/commands_test.go index b62b55274..9560bb024 100644 --- a/pkg/commands/product/botmanagement/commands_test.go +++ b/pkg/commands/product/botmanagement/commands_test.go @@ -7,14 +7,34 @@ import ( root "github.com/fastly/cli/pkg/commands/product" sub "github.com/fastly/cli/pkg/commands/product/botmanagement" product "github.com/fastly/go-fastly/v9/fastly/products/botmanagement" + "github.com/fastly/cli/pkg/testutil" ) +var CRA = []productcore_test.CommandRequiredArguments{ + { + Command: "enable", + Arguments: []string{"--service-id " + productcore_test.TestServiceID}, + }, + { + Command: "disable", + Arguments: []string{"--service-id " + productcore_test.TestServiceID}, + }, + { + Command: "status", + Arguments: []string{"--service-id " + productcore_test.TestServiceID}, + }, +} + +func MockEnableOutput(serviceID string) product.EnableOutput { + return product.NewEnableOutput(serviceID) +} + func TestBotManagementEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) + scenarios := productcore_test.MissingServiceIDScenarios(CRA) + scenarios = append(scenarios, productcore_test.InvalidJSONVerboseScenarios(CRA)...) + scenarios = append(scenarios, productcore_test.EnableScenarios(CRA, product.ProductID, product.ProductName, &sub.EnablementHooks, MockEnableOutput)...) + scenarios = append(scenarios, productcore_test.DisableScenarios(CRA, product.ProductID, product.ProductName, &sub.EnablementHooks)...) + scenarios = append(scenarios, productcore_test.StatusScenarios(CRA, product.ProductID, product.ProductName, &sub.EnablementHooks, MockEnableOutput)...) + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName}, scenarios) } diff --git a/pkg/commands/product/brotlicompression/commands.go b/pkg/commands/product/brotlicompression/commands.go deleted file mode 100644 index a2dafda6a..000000000 --- a/pkg/commands/product/brotlicompression/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package brotlicompression - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "brotli_compression" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Brotli Compression product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/brotlicompression/commands_test.go b/pkg/commands/product/brotlicompression/commands_test.go deleted file mode 100644 index 072b26d05..000000000 --- a/pkg/commands/product/brotlicompression/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package brotlicompression_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/brotlicompression" - product "github.com/fastly/go-fastly/v9/fastly/products/brotlicompression" -) - -func TestBrotliCompressionEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/brotlicompression/doc.go b/pkg/commands/product/brotlicompression/doc.go deleted file mode 100644 index 8c09f66e2..000000000 --- a/pkg/commands/product/brotlicompression/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package brotlicompression contains commands to enable and disable the -// Fastly Brotli Compression product. -package brotlicompression diff --git a/pkg/commands/product/ddosprotection/commands.go b/pkg/commands/product/ddosprotection/commands.go deleted file mode 100644 index 9fed94dc8..000000000 --- a/pkg/commands/product/ddosprotection/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package ddosprotection - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "ddos_protection" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the DDoS Protection product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/ddosprotection/commands_test.go b/pkg/commands/product/ddosprotection/commands_test.go deleted file mode 100644 index 91f4f3c2f..000000000 --- a/pkg/commands/product/ddosprotection/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package ddosprotection_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/ddosprotection" - product "github.com/fastly/go-fastly/v9/fastly/products/ddosprotection" -) - -func TestDDoSProtectionEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/ddosprotection/doc.go b/pkg/commands/product/ddosprotection/doc.go deleted file mode 100644 index 4a490d690..000000000 --- a/pkg/commands/product/ddosprotection/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package ddosprotection contains commands to enable and disable the -// Fastly DDoS Protection product. -package ddosprotection diff --git a/pkg/commands/product/domaininspector/commands.go b/pkg/commands/product/domaininspector/commands.go deleted file mode 100644 index 967429028..000000000 --- a/pkg/commands/product/domaininspector/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package domaininspector - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "domain_inspector" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Domain Inspector product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/domaininspector/commands_test.go b/pkg/commands/product/domaininspector/commands_test.go deleted file mode 100644 index cda8337dc..000000000 --- a/pkg/commands/product/domaininspector/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package domaininspector_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/domaininspector" - product "github.com/fastly/go-fastly/v9/fastly/products/domaininspector" -) - -func TestDomainInspectorEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/domaininspector/doc.go b/pkg/commands/product/domaininspector/doc.go deleted file mode 100644 index 69c34b29f..000000000 --- a/pkg/commands/product/domaininspector/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package domaininspector contains commands to enable and disable the -// Fastly Domain Inspector product. -package domaininspector diff --git a/pkg/commands/product/fanout/commands.go b/pkg/commands/product/fanout/commands.go deleted file mode 100644 index 445e50083..000000000 --- a/pkg/commands/product/fanout/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package fanout - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/fanout" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "fanout" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Fanout product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/fanout/commands_test.go b/pkg/commands/product/fanout/commands_test.go deleted file mode 100644 index 67963e1c8..000000000 --- a/pkg/commands/product/fanout/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package fanout_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/fanout" - product "github.com/fastly/go-fastly/v9/fastly/products/fanout" -) - -func TestFanoutEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/fanout/doc.go b/pkg/commands/product/fanout/doc.go deleted file mode 100644 index 84f696f55..000000000 --- a/pkg/commands/product/fanout/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package fanout contains commands to enable and disable the -// Fastly Fanout product. -package fanout diff --git a/pkg/commands/product/imageoptimizer/commands.go b/pkg/commands/product/imageoptimizer/commands.go deleted file mode 100644 index 46554f48d..000000000 --- a/pkg/commands/product/imageoptimizer/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package imageoptimizer - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "image_optimizer" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Image Optimizer product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/imageoptimizer/commands_test.go b/pkg/commands/product/imageoptimizer/commands_test.go deleted file mode 100644 index 0b7ee2e83..000000000 --- a/pkg/commands/product/imageoptimizer/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package imageoptimizer_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/imageoptimizer" - product "github.com/fastly/go-fastly/v9/fastly/products/imageoptimizer" -) - -func TestImageoptimizerEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/imageoptimizer/doc.go b/pkg/commands/product/imageoptimizer/doc.go deleted file mode 100644 index 29fb55985..000000000 --- a/pkg/commands/product/imageoptimizer/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package imageoptimizer contains commands to enable and disable the -// Fastly Image Optimizer product. -package imageoptimizer diff --git a/pkg/commands/product/logexplorerinsights/commands.go b/pkg/commands/product/logexplorerinsights/commands.go deleted file mode 100644 index 0dca17c8b..000000000 --- a/pkg/commands/product/logexplorerinsights/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package logexplorerinsights - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "log_explorer_insights" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Log Explorer & Insights product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/logexplorerinsights/commands_test.go b/pkg/commands/product/logexplorerinsights/commands_test.go deleted file mode 100644 index 67ccf3930..000000000 --- a/pkg/commands/product/logexplorerinsights/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package logexplorerinsights_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/logexplorerinsights" - product "github.com/fastly/go-fastly/v9/fastly/products/logexplorerinsights" -) - -func TestLogexplorerinsightsEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/logexplorerinsights/doc.go b/pkg/commands/product/logexplorerinsights/doc.go deleted file mode 100644 index e22fc0b7f..000000000 --- a/pkg/commands/product/logexplorerinsights/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package logexplorerinsights contains commands to enable and disable the -// Fastly Log Explorer & Insights product. -package logexplorerinsights diff --git a/pkg/commands/product/origininspector/commands.go b/pkg/commands/product/origininspector/commands.go deleted file mode 100644 index aad1f2add..000000000 --- a/pkg/commands/product/origininspector/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package origininspector - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/origininspector" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "origin_inspector" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the Origin Inspector product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/origininspector/commands_test.go b/pkg/commands/product/origininspector/commands_test.go deleted file mode 100644 index 49e3cb1c0..000000000 --- a/pkg/commands/product/origininspector/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package origininspector_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/origininspector" - product "github.com/fastly/go-fastly/v9/fastly/products/origininspector" -) - -func TestOrigininspectorEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/origininspector/doc.go b/pkg/commands/product/origininspector/doc.go deleted file mode 100644 index 72d198a1b..000000000 --- a/pkg/commands/product/origininspector/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package origininspector contains commands to enable and disable the -// Fastly Origin Inspector product. -package origininspector diff --git a/pkg/commands/product/websockets/commands.go b/pkg/commands/product/websockets/commands.go deleted file mode 100644 index 2b981ec2b..000000000 --- a/pkg/commands/product/websockets/commands.go +++ /dev/null @@ -1,101 +0,0 @@ -package websockets - -import ( - "io" - - "github.com/fastly/cli/pkg/argparser" - "github.com/fastly/cli/pkg/global" - "github.com/fastly/go-fastly/v9/fastly" - product "github.com/fastly/go-fastly/v9/fastly/products/websockets" - - "github.com/fastly/cli/internal/productcore" - "github.com/fastly/cli/pkg/api" -) - -// EnablementHooks is a structure of dependency-injection points used -// by unit tests to provide mock behaviors -var EnablementHooks = productcore.EnablementHookFuncs[*product.EnableOutput]{ - DisableFunc: func(client api.Interface, serviceID string) error { - return product.Disable(client.(*fastly.Client), serviceID) - }, - EnableFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Enable(client.(*fastly.Client), serviceID) - }, - GetFunc: func(client api.Interface, serviceID string) (*product.EnableOutput, error) { - return product.Get(client.(*fastly.Client), serviceID) - }, -} - -// RootCommand is the parent command for all subcommands in this package. -// It should be installed under the primary root command. -type RootCommand struct { - argparser.Base - // no flags -} - -// CommandName is the string to be used to invoke this command -const CommandName = "websockets" - -// NewRootCommand returns a new command registered in the parent. -func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { - var c RootCommand - c.Globals = g - c.CmdClause = parent.Command(CommandName, "Enable and disable the WebSockets product") - return &c -} - -// Exec implements the command interface. -func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error { - panic("unreachable") -} - -// EnableCommand calls the Fastly API to disable the product. -type EnableCommand struct { - productcore.Enable[*product.EnableOutput] -} - -// NewEnableCommand returns a usable command registered under the parent. -func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand { - c := EnableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Enable.Exec(out) -} - -// DisableCommand calls the Fastly API to disable the product. -type DisableCommand struct { - productcore.Disable[*product.EnableOutput] -} - -// NewDisableCommand returns a usable command registered under the parent. -func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand { - c := DisableCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Disable.Exec(out) -} - -// StatusCommand calls the Fastly API to get the enablement status of the product. -type StatusCommand struct { - productcore.Status[*product.EnableOutput] -} - -// NewStatusCommand returns a usable command registered under the parent. -func NewStatusCommand(parent argparser.Registerer, g *global.Data) *StatusCommand { - c := StatusCommand{} - c.Init(parent, g, product.ProductID, product.ProductName, &EnablementHooks) - return &c -} - -// Exec invokes the application logic for the command. -func (cmd *StatusCommand) Exec(_ io.Reader, out io.Writer) error { - return cmd.Status.Exec(out) -} diff --git a/pkg/commands/product/websockets/commands_test.go b/pkg/commands/product/websockets/commands_test.go deleted file mode 100644 index 900d4fdaf..000000000 --- a/pkg/commands/product/websockets/commands_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package websockets_test - -import ( - "testing" - - "github.com/fastly/cli/internal/productcore_test" - root "github.com/fastly/cli/pkg/commands/product" - sub "github.com/fastly/cli/pkg/commands/product/websockets" - product "github.com/fastly/go-fastly/v9/fastly/products/websockets" -) - -func TestWebSocketsEnablement(t *testing.T) { - productcore_test.TestEnablement(productcore_test.TestEnablementInput[*product.EnableOutput]{ - T: t, - Commands: []string{root.CommandName, sub.CommandName}, - ProductID: product.ProductID, - ProductName: product.ProductName, - Hooks: &sub.EnablementHooks, - }) -} diff --git a/pkg/commands/product/websockets/doc.go b/pkg/commands/product/websockets/doc.go deleted file mode 100644 index 69aa77cc4..000000000 --- a/pkg/commands/product/websockets/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -// Package websockets contains commands to enable and disable the -// Fastly WebSockets product. -package websockets