Skip to content

Commit 149b665

Browse files
committed
WIP
1 parent 80860f7 commit 149b665

File tree

7 files changed

+179
-105
lines changed

7 files changed

+179
-105
lines changed

internal/productcore/base.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package productcore
2+
3+
import (
4+
"github.com/fastly/cli/pkg/api"
5+
"github.com/fastly/cli/pkg/argparser"
6+
"github.com/fastly/cli/pkg/global"
7+
"github.com/fastly/cli/pkg/manifest"
8+
)
9+
10+
// Base is a base type for all product commands.
11+
type Base struct {
12+
argparser.Base
13+
Manifest manifest.Data
14+
15+
ServiceName argparser.OptionalServiceNameID
16+
ProductName string
17+
}
18+
19+
// Init prepares the structure for use by the CLI core.
20+
func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productName string) {
21+
cmd.Globals = g
22+
cmd.ProductName = productName
23+
24+
// Optional flags.
25+
cmd.RegisterFlag(argparser.StringFlagOpts{
26+
Name: argparser.FlagServiceIDName,
27+
Description: argparser.FlagServiceIDDesc,
28+
Dst: &g.Manifest.Flag.ServiceID,
29+
Short: 's',
30+
})
31+
cmd.RegisterFlag(argparser.StringFlagOpts{
32+
Action: cmd.ServiceName.Set,
33+
Name: argparser.FlagServiceName,
34+
Description: argparser.FlagServiceNameDesc,
35+
Dst: &cmd.ServiceName.Value,
36+
})
37+
}
38+
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)
43+
}
44+
45+
type ConfigurationHookFns[O, I any] struct {
46+
GetConfigurationFn func(api.Interface, string) (O, error)
47+
UpdateConfigurationFn func(api.Interface, string, I) (O, error)
48+
}

internal/productcore/disable.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package productcore
2+
3+
import (
4+
"io"
5+
"github.com/fastly/cli/pkg/api"
6+
"github.com/fastly/cli/pkg/argparser"
7+
"github.com/fastly/cli/pkg/global"
8+
"github.com/fastly/cli/pkg/text"
9+
)
10+
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+
15+
// Disable is a base type for all 'disable' commands.
16+
type Disable struct {
17+
Base
18+
}
19+
20+
// Init prepares the structure for use by the CLI core.
21+
func (cmd *Disable) Init(parent argparser.Registerer, g *global.Data, productName string) {
22+
cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product")
23+
24+
cmd.Base.Init(parent, g, productName)
25+
}
26+
27+
// Exec executes the disablement operation.
28+
func (cmd *Disable) Exec(out io.Writer, op DisableFn) error {
29+
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog)
30+
if err != nil {
31+
cmd.Globals.ErrLog.Add(err)
32+
return err
33+
}
34+
35+
if cmd.Globals.Verbose() {
36+
argparser.DisplayServiceID(serviceID, flag, source, out)
37+
}
38+
39+
err = op(cmd.Globals.APIClient, serviceID)
40+
if err != nil {
41+
cmd.Globals.ErrLog.Add(err)
42+
return err
43+
}
44+
45+
text.Success(out,
46+
"Disabled "+cmd.ProductName+" on service %s", serviceID)
47+
48+
return nil
49+
}

internal/productcore/enable.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package productcore
2+
3+
import (
4+
"io"
5+
"github.com/fastly/cli/pkg/argparser"
6+
"github.com/fastly/cli/pkg/global"
7+
"github.com/fastly/cli/pkg/text"
8+
)
9+
10+
// Enable is a base type for all 'enable' commands.
11+
type Enable[O any] struct {
12+
Base
13+
hooks *HookFns[O]
14+
}
15+
16+
// 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+
cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product")
19+
cmd.hooks = hooks
20+
21+
cmd.Base.Init(parent, g, productName)
22+
}
23+
24+
// Exec executes the disablement operation.
25+
func (cmd *Enable[O]) Exec(out io.Writer) error {
26+
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog)
27+
if err != nil {
28+
cmd.Globals.ErrLog.Add(err)
29+
return err
30+
}
31+
32+
if cmd.Globals.Verbose() {
33+
argparser.DisplayServiceID(serviceID, flag, source, out)
34+
}
35+
36+
_, err = cmd.hooks.EnableFn(cmd.Globals.APIClient, serviceID)
37+
if err != nil {
38+
cmd.Globals.ErrLog.Add(err)
39+
return err
40+
}
41+
42+
text.Success(out,
43+
"Enabled "+cmd.ProductName+" on service %s", serviceID)
44+
45+
return nil
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package bot_management
2+
3+
import (
4+
"github.com/fastly/go-fastly/v9/fastly"
5+
"github.com/fastly/go-fastly/v9/fastly/products/bot_management"
6+
7+
"github.com/fastly/cli/internal/productcore"
8+
"github.com/fastly/cli/pkg/api"
9+
)
10+
11+
var EnablementHooks = productcore.EnablementHookFns[*bot_management.EnableOutput]{
12+
DisableFn: func(client api.Interface, serviceID string) error {
13+
return bot_management.Disable(client.(*fastly.Client), serviceID)
14+
},
15+
EnableFn: func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) {
16+
return bot_management.Enable(client.(*fastly.Client), serviceID)
17+
},
18+
GetFn: func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) {
19+
return bot_management.Get(client.(*fastly.Client), serviceID)
20+
},
21+
}

pkg/commands/product/bot_management/disable.go

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"github.com/fastly/go-fastly/v9/fastly"
77
"github.com/fastly/go-fastly/v9/fastly/products/bot_management"
88

9+
"github.com/fastly/cli/internal/productcore"
910
"github.com/fastly/cli/pkg/api"
1011
"github.com/fastly/cli/pkg/argparser"
1112
"github.com/fastly/cli/pkg/global"
12-
"github.com/fastly/cli/pkg/manifest"
13-
"github.com/fastly/cli/pkg/text"
1413
)
1514

1615
// DisableFn is a dependency-injection point for unit tests to provide
@@ -21,57 +20,17 @@ var DisableFn = func(client api.Interface, serviceID string) error {
2120

2221
// DisableCommand calls the Fastly API to disable the product.
2322
type DisableCommand struct {
24-
argparser.Base
25-
Manifest manifest.Data
26-
27-
serviceName argparser.OptionalServiceNameID
23+
productcore.Disable
2824
}
2925

3026
// NewDisableCommand returns a usable command registered under the parent.
3127
func NewDisableCommand(parent argparser.Registerer, g *global.Data) *DisableCommand {
32-
c := DisableCommand{
33-
Base: argparser.Base{
34-
Globals: g,
35-
},
36-
}
37-
c.CmdClause = parent.Command("disable", "Disable the "+bot_management.ProductName+" product")
38-
39-
// Optional.
40-
c.RegisterFlag(argparser.StringFlagOpts{
41-
Name: argparser.FlagServiceIDName,
42-
Description: argparser.FlagServiceIDDesc,
43-
Dst: &g.Manifest.Flag.ServiceID,
44-
Short: 's',
45-
})
46-
c.RegisterFlag(argparser.StringFlagOpts{
47-
Action: c.serviceName.Set,
48-
Name: argparser.FlagServiceName,
49-
Description: argparser.FlagServiceNameDesc,
50-
Dst: &c.serviceName.Value,
51-
})
28+
c := DisableCommand{}
29+
c.Init(parent, g, bot_management.ProductName)
5230
return &c
5331
}
5432

5533
// Exec invokes the application logic for the command.
56-
func (c *DisableCommand) Exec(_ io.Reader, out io.Writer) error {
57-
serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
58-
if err != nil {
59-
c.Globals.ErrLog.Add(err)
60-
return err
61-
}
62-
63-
if c.Globals.Verbose() {
64-
argparser.DisplayServiceID(serviceID, flag, source, out)
65-
}
66-
67-
err = DisableFn(c.Globals.APIClient, serviceID)
68-
if err != nil {
69-
c.Globals.ErrLog.Add(err)
70-
return err
71-
}
72-
73-
text.Success(out,
74-
"Disabled "+bot_management.ProductName+" on service %s", serviceID)
75-
76-
return nil
34+
func (cmd *DisableCommand) Exec(_ io.Reader, out io.Writer) error {
35+
return cmd.Disable.Exec(out, DisableFn)
7736
}

pkg/commands/product/bot_management/enable.go

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,26 @@ package bot_management
33
import (
44
"io"
55

6-
"github.com/fastly/go-fastly/v9/fastly"
76
"github.com/fastly/go-fastly/v9/fastly/products/bot_management"
87

9-
"github.com/fastly/cli/pkg/api"
8+
"github.com/fastly/cli/internal/productcore"
109
"github.com/fastly/cli/pkg/argparser"
1110
"github.com/fastly/cli/pkg/global"
12-
"github.com/fastly/cli/pkg/manifest"
13-
"github.com/fastly/cli/pkg/text"
1411
)
1512

16-
// EnableFn is a dependency-injection point for unit tests to provide
17-
// a mock implementation of the API operation.
18-
var EnableFn = func(client api.Interface, serviceID string) (*bot_management.EnableOutput, error) {
19-
return bot_management.Enable(client.(*fastly.Client), serviceID)
20-
}
21-
22-
// EnableCommand calls the Fastly API to enable the product.
13+
// EnableCommand calls the Fastly API to disable the product.
2314
type EnableCommand struct {
24-
argparser.Base
25-
Manifest manifest.Data
26-
27-
serviceName argparser.OptionalServiceNameID
15+
productcore.Enable[*bot_management.EnableOutput]
2816
}
2917

3018
// NewEnableCommand returns a usable command registered under the parent.
3119
func NewEnableCommand(parent argparser.Registerer, g *global.Data) *EnableCommand {
32-
c := EnableCommand{
33-
Base: argparser.Base{
34-
Globals: g,
35-
},
36-
}
37-
c.CmdClause = parent.Command("enable", "Enable the "+bot_management.ProductName+" product")
38-
39-
// Optional.
40-
c.RegisterFlag(argparser.StringFlagOpts{
41-
Name: argparser.FlagServiceIDName,
42-
Description: argparser.FlagServiceIDDesc,
43-
Dst: &g.Manifest.Flag.ServiceID,
44-
Short: 's',
45-
})
46-
c.RegisterFlag(argparser.StringFlagOpts{
47-
Action: c.serviceName.Set,
48-
Name: argparser.FlagServiceName,
49-
Description: argparser.FlagServiceNameDesc,
50-
Dst: &c.serviceName.Value,
51-
})
20+
c := EnableCommand{}
21+
c.Init(parent, g, bot_management.ProductName, &EnablementHooks)
5222
return &c
5323
}
5424

5525
// Exec invokes the application logic for the command.
56-
func (c *EnableCommand) Exec(_ io.Reader, out io.Writer) error {
57-
serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
58-
if err != nil {
59-
c.Globals.ErrLog.Add(err)
60-
return err
61-
}
62-
63-
if c.Globals.Verbose() {
64-
argparser.DisplayServiceID(serviceID, flag, source, out)
65-
}
66-
67-
_, err = EnableFn(c.Globals.APIClient, serviceID)
68-
if err != nil {
69-
c.Globals.ErrLog.Add(err)
70-
return err
71-
}
72-
73-
text.Success(out,
74-
"Enabled "+bot_management.ProductName+" on service %s", serviceID)
75-
76-
return nil
26+
func (cmd *EnableCommand) Exec(_ io.Reader, out io.Writer) error {
27+
return cmd.Enable.Exec(out)
7728
}

pkg/commands/product/bot_management/product_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestProductEnablement(t *testing.T) {
3838
{
3939
Name: "validate success for enabling product",
4040
Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) {
41-
sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) {
41+
sub.EnablementHooks.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) {
4242
return nil, nil
4343
}
4444
},
@@ -48,7 +48,7 @@ func TestProductEnablement(t *testing.T) {
4848
{
4949
Name: "validate failure for enabling product",
5050
Setup: func(t *testing.T, scenario *testutil.CLIScenario, opts *global.Data) {
51-
sub.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) {
51+
sub.EnablementHooks.EnableFn = func(_ api.Interface, _ string) (*bot_management.EnableOutput, error) {
5252
return nil, testutil.Err
5353
}
5454
},

0 commit comments

Comments
 (0)