Skip to content

Commit 7a60b65

Browse files
committed
More WIP
1 parent 149b665 commit 7a60b65

File tree

17 files changed

+207
-199
lines changed

17 files changed

+207
-199
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ require (
7979

8080
require 4d63.com/optional v0.2.0
8181

82-
replace github.com/fastly/go-fastly/v9 => github.com/kpfleming/go-fastly/v9 v9.12.1-0.20241217164724-8f6ebe66851a
82+
replace github.com/fastly/go-fastly/v9 => github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44

Diff for: go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
6565
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
6666
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
6767
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
68-
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20241217164724-8f6ebe66851a h1:4NkDjmddTs2qY+41phkmIqV07XX4vDUZhLuOfZJtcXo=
69-
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20241217164724-8f6ebe66851a/go.mod h1:rB3T7CBBYBw+/W4rpzmZPev8BbARin6vriirVCY0yaw=
68+
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44 h1:YdOuOWFVfUN3V3pv3MXE11PTh5A9Emh3NlW8qg2dLJk=
69+
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44/go.mod h1:rB3T7CBBYBw+/W4rpzmZPev8BbARin6vriirVCY0yaw=
7070
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
7171
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
7272
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=

Diff for: internal/productcore/base.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// Base is a base type for all product commands.
1111
type Base struct {
1212
argparser.Base
13+
argparser.JSONOutput
1314
Manifest manifest.Data
1415

1516
ServiceName argparser.OptionalServiceNameID
@@ -34,15 +35,26 @@ func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productName s
3435
Description: argparser.FlagServiceNameDesc,
3536
Dst: &cmd.ServiceName.Value,
3637
})
38+
cmd.RegisterFlagBool(cmd.JSONFlag()) // --json
3739
}
3840

39-
type EnablementHookFns[O any] struct {
40-
DisableFn func(api.Interface, string) error
41-
EnableFn func(api.Interface, string) (O, error)
42-
GetFn func(api.Interface, string) (O, error)
41+
// EnablementStatus is a structure used to generate JSON output from
42+
// the enablement-related commands
43+
type EnablementStatus struct {
44+
Enabled bool `json:"enabled"`
4345
}
4446

45-
type ConfigurationHookFns[O, I any] struct {
46-
GetConfigurationFn func(api.Interface, string) (O, error)
47-
UpdateConfigurationFn func(api.Interface, string, I) (O, error)
47+
// EnablementHookFuncs is a structure of dependency-injection points
48+
// used by unit tests to provide mock behaviors
49+
type EnablementHookFuncs[O any] struct {
50+
DisableFunc func(api.Interface, string) error
51+
EnableFunc func(api.Interface, string) (O, error)
52+
GetFunc func(api.Interface, string) (O, error)
53+
}
54+
55+
// ConfigurationHookFuncs is a structure of dependency-injection
56+
// points by unit tests to provide mock behaviors
57+
type ConfigurationHookFuncs[O, I any] struct {
58+
GetConfigurationFunc func(api.Interface, string) (O, error)
59+
UpdateConfigurationFunc func(api.Interface, string, I) (O, error)
4860
}

Diff for: internal/productcore/disable.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@ package productcore
22

33
import (
44
"io"
5-
"github.com/fastly/cli/pkg/api"
5+
fsterr "github.com/fastly/cli/pkg/errors"
6+
67
"github.com/fastly/cli/pkg/argparser"
78
"github.com/fastly/cli/pkg/global"
89
"github.com/fastly/cli/pkg/text"
910
)
1011

11-
// DisableFn is the type of the function that will be used to perform
12-
// the disablement.
13-
type DisableFn func(api.Interface, string) error
14-
1512
// Disable is a base type for all 'disable' commands.
16-
type Disable struct {
13+
type Disable[O any] struct {
1714
Base
15+
hooks *EnablementHookFuncs[O]
1816
}
1917

2018
// Init prepares the structure for use by the CLI core.
21-
func (cmd *Disable) Init(parent argparser.Registerer, g *global.Data, productName string) {
19+
func (cmd *Disable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) {
2220
cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product")
21+
cmd.hooks = hooks
2322

2423
cmd.Base.Init(parent, g, productName)
2524
}
2625

2726
// Exec executes the disablement operation.
28-
func (cmd *Disable) Exec(out io.Writer, op DisableFn) error {
27+
func (cmd *Disable[O]) Exec(out io.Writer) error {
28+
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
29+
return fsterr.ErrInvalidVerboseJSONCombo
30+
}
31+
2932
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog)
3033
if err != nil {
3134
cmd.Globals.ErrLog.Add(err)
@@ -36,14 +39,18 @@ func (cmd *Disable) Exec(out io.Writer, op DisableFn) error {
3639
argparser.DisplayServiceID(serviceID, flag, source, out)
3740
}
3841

39-
err = op(cmd.Globals.APIClient, serviceID)
42+
err = cmd.hooks.DisableFunc(cmd.Globals.APIClient, serviceID)
4043
if err != nil {
4144
cmd.Globals.ErrLog.Add(err)
4245
return err
4346
}
4447

48+
if ok, err := cmd.WriteJSON(out, EnablementStatus{Enabled: false}); ok {
49+
return err
50+
}
51+
4552
text.Success(out,
46-
"Disabled "+cmd.ProductName+" on service %s", serviceID)
53+
"Disabled %s on service %s", cmd.ProductName, serviceID)
4754

4855
return nil
4956
}

Diff for: internal/productcore/enable.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package productcore
22

33
import (
44
"io"
5+
fsterr "github.com/fastly/cli/pkg/errors"
56
"github.com/fastly/cli/pkg/argparser"
67
"github.com/fastly/cli/pkg/global"
78
"github.com/fastly/cli/pkg/text"
@@ -10,19 +11,23 @@ import (
1011
// Enable is a base type for all 'enable' commands.
1112
type Enable[O any] struct {
1213
Base
13-
hooks *HookFns[O]
14+
hooks *EnablementHookFuncs[O]
1415
}
1516

1617
// Init prepares the structure for use by the CLI core.
17-
func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *HookFns[O]) {
18+
func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) {
1819
cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product")
1920
cmd.hooks = hooks
2021

2122
cmd.Base.Init(parent, g, productName)
2223
}
2324

24-
// Exec executes the disablement operation.
25+
// Exec executes the enablement operation.
2526
func (cmd *Enable[O]) Exec(out io.Writer) error {
27+
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
28+
return fsterr.ErrInvalidVerboseJSONCombo
29+
}
30+
2631
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog)
2732
if err != nil {
2833
cmd.Globals.ErrLog.Add(err)
@@ -33,14 +38,18 @@ func (cmd *Enable[O]) Exec(out io.Writer) error {
3338
argparser.DisplayServiceID(serviceID, flag, source, out)
3439
}
3540

36-
_, err = cmd.hooks.EnableFn(cmd.Globals.APIClient, serviceID)
41+
_, err = cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID)
3742
if err != nil {
3843
cmd.Globals.ErrLog.Add(err)
3944
return err
4045
}
4146

47+
if ok, err := cmd.WriteJSON(out, EnablementStatus{Enabled: true}); ok {
48+
return err
49+
}
50+
4251
text.Success(out,
43-
"Enabled "+cmd.ProductName+" on service %s", serviceID)
52+
"Enabled %s on service %s", cmd.ProductName, serviceID)
4453

4554
return nil
4655
}

Diff for: internal/productcore/status.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package productcore
2+
3+
import (
4+
"io"
5+
"errors"
6+
"github.com/fastly/go-fastly/v9/fastly"
7+
fsterr "github.com/fastly/cli/pkg/errors"
8+
"github.com/fastly/cli/pkg/argparser"
9+
"github.com/fastly/cli/pkg/global"
10+
"github.com/fastly/cli/pkg/text"
11+
)
12+
13+
// Status is a base type for all 'status' commands.
14+
type Status[O any] struct {
15+
Base
16+
hooks *EnablementHookFuncs[O]
17+
}
18+
19+
// Init prepares the structure for use by the CLI core.
20+
func (cmd *Status[O]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) {
21+
cmd.CmdClause = parent.Command("status", "Get the enablement status of the "+productName+" product")
22+
cmd.hooks = hooks
23+
24+
cmd.Base.Init(parent, g, productName)
25+
}
26+
27+
// Exec executes the status operation.
28+
func (cmd *Status[O]) Exec(out io.Writer) error {
29+
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
30+
return fsterr.ErrInvalidVerboseJSONCombo
31+
}
32+
33+
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog)
34+
if err != nil {
35+
cmd.Globals.ErrLog.Add(err)
36+
return err
37+
}
38+
39+
if cmd.Globals.Verbose() {
40+
argparser.DisplayServiceID(serviceID, flag, source, out)
41+
}
42+
43+
s := EnablementStatus{}
44+
state := "disabled"
45+
46+
_, err = cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID)
47+
if err != nil {
48+
var herr *fastly.HTTPError
49+
50+
// The API returns a 'Bad Request' error when the
51+
// product has not been enabled on the service; any
52+
// other error should be reported
53+
if !errors.As(err, &herr) || !herr.IsBadRequest() {
54+
cmd.Globals.ErrLog.Add(err)
55+
return err
56+
}
57+
} else {
58+
s.Enabled = true
59+
state = "enabled"
60+
}
61+
62+
if ok, err := cmd.WriteJSON(out, s); ok {
63+
return err
64+
}
65+
66+
text.Info(out,
67+
"%s is %s on service %s", cmd.ProductName, state, serviceID)
68+
69+
return nil
70+
}

Diff for: pkg/commands/commands.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import (
5252
"github.com/fastly/cli/pkg/commands/logtail"
5353
"github.com/fastly/cli/pkg/commands/pop"
5454
"github.com/fastly/cli/pkg/commands/product"
55-
"github.com/fastly/cli/pkg/commands/product/bot_management"
55+
"github.com/fastly/cli/pkg/commands/product/botmanagement"
5656
"github.com/fastly/cli/pkg/commands/products"
5757
"github.com/fastly/cli/pkg/commands/profile"
5858
"github.com/fastly/cli/pkg/commands/purge"
@@ -359,10 +359,10 @@ func Define(
359359
loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, data)
360360
popCmdRoot := pop.NewRootCommand(app, data)
361361
productCmdRoot := product.NewRootCommand(app, data)
362-
productBotManagementCmdRoot := bot_management.NewRootCommand(productCmdRoot.CmdClause, data)
363-
productBotManagementDisable := bot_management.NewDisableCommand(productBotManagementCmdRoot.CmdClause, data)
364-
productBotManagementEnable := bot_management.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data)
365-
productBotManagementStatus := bot_management.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data)
362+
productBotManagementCmdRoot := botmanagement.NewRootCommand(productCmdRoot.CmdClause, data)
363+
productBotManagementDisable := botmanagement.NewDisableCommand(productBotManagementCmdRoot.CmdClause, data)
364+
productBotManagementEnable := botmanagement.NewEnableCommand(productBotManagementCmdRoot.CmdClause, data)
365+
productBotManagementStatus := botmanagement.NewStatusCommand(productBotManagementCmdRoot.CmdClause, data)
366366
productsCmdRoot := products.NewRootCommand(app, data)
367367
profileCmdRoot := profile.NewRootCommand(app, data)
368368
profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, data, ssoCmdRoot)

Diff for: pkg/commands/product/bot_management/common.go

-21
This file was deleted.

Diff for: pkg/commands/product/bot_management/doc.go

-3
This file was deleted.

0 commit comments

Comments
 (0)