Skip to content

Commit

Permalink
More WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kpfleming committed Jan 13, 2025
1 parent 149b665 commit 7a60b65
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 199 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ require (

require 4d63.com/optional v0.2.0

replace github.com/fastly/go-fastly/v9 => github.com/kpfleming/go-fastly/v9 v9.12.1-0.20241217164724-8f6ebe66851a
replace github.com/fastly/go-fastly/v9 => github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20241217164724-8f6ebe66851a h1:4NkDjmddTs2qY+41phkmIqV07XX4vDUZhLuOfZJtcXo=
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20241217164724-8f6ebe66851a/go.mod h1:rB3T7CBBYBw+/W4rpzmZPev8BbARin6vriirVCY0yaw=
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44 h1:YdOuOWFVfUN3V3pv3MXE11PTh5A9Emh3NlW8qg2dLJk=
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44/go.mod h1:rB3T7CBBYBw+/W4rpzmZPev8BbARin6vriirVCY0yaw=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down
26 changes: 19 additions & 7 deletions internal/productcore/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
27 changes: 17 additions & 10 deletions internal/productcore/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
19 changes: 14 additions & 5 deletions internal/productcore/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
}
70 changes: 70 additions & 0 deletions internal/productcore/status.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 5 additions & 5 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import (
"github.com/fastly/cli/pkg/commands/logtail"
"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"
Expand Down Expand Up @@ -359,10 +359,10 @@ func Define(
loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.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)
Expand Down
21 changes: 0 additions & 21 deletions pkg/commands/product/bot_management/common.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/commands/product/bot_management/doc.go

This file was deleted.

Loading

0 comments on commit 7a60b65

Please sign in to comment.