Skip to content

Commit 386f765

Browse files
committed
More WIP
1 parent cfb53fe commit 386f765

File tree

32 files changed

+374
-1269
lines changed

32 files changed

+374
-1269
lines changed

internal/productcore/base.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package productcore
22

33
import (
4+
"fmt"
5+
46
"github.com/fastly/cli/pkg/api"
57
"github.com/fastly/cli/pkg/argparser"
68
"github.com/fastly/cli/pkg/global"
79
"github.com/fastly/cli/pkg/manifest"
10+
"github.com/fastly/go-fastly/v9/fastly/products"
811
)
912

1013
// Base is a base type for all product commands.
@@ -14,15 +17,11 @@ type Base struct {
1417
Manifest manifest.Data
1518

1619
ServiceName argparser.OptionalServiceNameID
17-
ProductID string
18-
ProductName string
1920
}
2021

2122
// Init prepares the structure for use by the CLI core.
22-
func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productID, productName string) {
23+
func (cmd *Base) Init(parent argparser.Registerer, g *global.Data) {
2324
cmd.Globals = g
24-
cmd.ProductID = productID
25-
cmd.ProductName = productName
2625

2726
// Optional flags.
2827
cmd.RegisterFlag(argparser.StringFlagOpts{
@@ -40,16 +39,60 @@ func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productID, pr
4039
cmd.RegisterFlagBool(cmd.JSONFlag()) // --json
4140
}
4241

43-
// EnablementStatus is a structure used to generate JSON output from
42+
// EnablementStatus is a structure used to generate output from
4443
// the enablement-related commands
45-
type EnablementStatus struct {
46-
ProductID string `json:"product_id"`
47-
Enabled bool `json:"enabled"`
44+
type EnablementStatus[_ products.ProductOutput] struct {
45+
ProductName string `json:"-"`
46+
ProductID string `json:"product_id"`
47+
ServiceID string `json:"service_id"`
48+
Enabled bool `json:"enabled"`
49+
}
50+
51+
type StatusManager[O products.ProductOutput] interface {
52+
SetEnabled(bool)
53+
GetEnabled() string
54+
GetProductName() string
55+
SetProductID(string)
56+
SetServiceID(string)
57+
TransformOutput(O)
58+
GetTextResult() string
59+
}
60+
61+
func (s *EnablementStatus[_]) SetEnabled(e bool) {
62+
s.Enabled = e
63+
}
64+
65+
func (s *EnablementStatus[_]) GetEnabled() string {
66+
if s.Enabled {
67+
return "enabled"
68+
}
69+
return "disabled"
70+
}
71+
72+
func (s *EnablementStatus[_]) GetProductName() string {
73+
return s.ProductName
74+
}
75+
76+
func (s *EnablementStatus[_]) SetProductID(id string) {
77+
s.ProductID = id
78+
}
79+
80+
func (s *EnablementStatus[_]) SetServiceID(id string) {
81+
s.ServiceID = id
82+
}
83+
84+
func (s *EnablementStatus[O]) TransformOutput(o O) {
85+
s.ProductID = o.ProductID()
86+
s.ServiceID = o.ServiceID()
87+
}
88+
89+
func (s *EnablementStatus[O]) GetTextResult() string {
90+
return fmt.Sprintf("%s is %s on service %s", s.ProductName, s.GetEnabled(), s.ServiceID)
4891
}
4992

5093
// EnablementHookFuncs is a structure of dependency-injection points
5194
// used by unit tests to provide mock behaviors
52-
type EnablementHookFuncs[O any] struct {
95+
type EnablementHookFuncs[O products.ProductOutput] struct {
5396
DisableFunc func(api.Interface, string) error
5497
EnableFunc func(api.Interface, string) (O, error)
5598
GetFunc func(api.Interface, string) (O, error)

internal/productcore/disable.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,30 @@ import (
88
"github.com/fastly/cli/pkg/argparser"
99
"github.com/fastly/cli/pkg/global"
1010
"github.com/fastly/cli/pkg/text"
11+
"github.com/fastly/go-fastly/v9/fastly/products"
1112
)
1213

1314
// Disable is a base type for all 'disable' commands.
14-
type Disable[O any] struct {
15+
type Disable[O products.ProductOutput, _ StatusManager[O]] struct {
1516
Base
17+
ProductID string
1618
// hooks is a pointer to an EnablementHookFuncs structure so
1719
// that tests can modify the contents of the structure after
1820
// this structure has been initialized
1921
hooks *EnablementHookFuncs[O]
2022
}
2123

2224
// Init prepares the structure for use by the CLI core.
23-
func (cmd *Disable[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) {
25+
func (cmd *Disable[O, _]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) {
2426
cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product")
2527
cmd.hooks = hooks
2628

27-
cmd.Base.Init(parent, g, productID, productName)
29+
cmd.Base.Init(parent, g)
30+
cmd.ProductID = productID
2831
}
2932

3033
// Exec executes the disablement operation.
31-
func (cmd *Disable[O]) Exec(out io.Writer) error {
34+
func (cmd *Disable[O, S]) Exec(out io.Writer, status S) error {
3235
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
3336
return fsterr.ErrInvalidVerboseJSONCombo
3437
}
@@ -49,12 +52,18 @@ func (cmd *Disable[O]) Exec(out io.Writer) error {
4952
return err
5053
}
5154

52-
if ok, err := cmd.WriteJSON(out, EnablementStatus{ProductID: cmd.ProductID, Enabled: false}); ok {
55+
status.SetEnabled(false)
56+
// The API does not return details of the service and product
57+
// which were disabled, so they have to be inserted into
58+
// 'status' directly
59+
status.SetProductID(cmd.ProductID)
60+
status.SetServiceID(serviceID)
61+
62+
if ok, err := cmd.WriteJSON(out, status); ok {
5363
return err
5464
}
5565

56-
text.Success(out,
57-
"Disabled %s on service %s", cmd.ProductName, serviceID)
66+
text.Success(out, status.GetTextResult())
5867

5968
return nil
6069
}

internal/productcore/enable.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
fsterr "github.com/fastly/cli/pkg/errors"
88
"github.com/fastly/cli/pkg/global"
99
"github.com/fastly/cli/pkg/text"
10+
"github.com/fastly/go-fastly/v9/fastly/products"
1011
)
1112

1213
// Enable is a base type for all 'enable' commands.
13-
type Enable[O any] struct {
14+
type Enable[O products.ProductOutput, _ StatusManager[O]] struct {
1415
Base
1516
// hooks is a pointer to an EnablementHookFuncs structure so
1617
// that tests can modify the contents of the structure after
@@ -19,15 +20,15 @@ type Enable[O any] struct {
1920
}
2021

2122
// Init prepares the structure for use by the CLI core.
22-
func (cmd *Enable[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) {
23+
func (cmd *Enable[O, _]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) {
2324
cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product")
2425
cmd.hooks = hooks
2526

26-
cmd.Base.Init(parent, g, productID, productName)
27+
cmd.Base.Init(parent, g)
2728
}
2829

2930
// Exec executes the enablement operation.
30-
func (cmd *Enable[O]) Exec(out io.Writer) error {
31+
func (cmd *Enable[O, S]) Exec(out io.Writer, status S) error {
3132
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
3233
return fsterr.ErrInvalidVerboseJSONCombo
3334
}
@@ -42,18 +43,20 @@ func (cmd *Enable[O]) Exec(out io.Writer) error {
4243
argparser.DisplayServiceID(serviceID, flag, source, out)
4344
}
4445

45-
_, err = cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID)
46+
o, err := cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID)
4647
if err != nil {
4748
cmd.Globals.ErrLog.Add(err)
4849
return err
4950
}
5051

51-
if ok, err := cmd.WriteJSON(out, EnablementStatus{ProductID: cmd.ProductID, Enabled: true}); ok {
52+
status.SetEnabled(true)
53+
status.TransformOutput(o)
54+
55+
if ok, err := cmd.WriteJSON(out, status); ok {
5256
return err
5357
}
5458

55-
text.Success(out,
56-
"Enabled %s on service %s", cmd.ProductName, serviceID)
59+
text.Success(out, status.GetTextResult())
5760

5861
return nil
5962
}

internal/productcore/status.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"github.com/fastly/cli/pkg/global"
1010
"github.com/fastly/cli/pkg/text"
1111
"github.com/fastly/go-fastly/v9/fastly"
12+
"github.com/fastly/go-fastly/v9/fastly/products"
1213
)
1314

1415
// Status is a base type for all 'status' commands.
15-
type Status[O any] struct {
16+
type Status[O products.ProductOutput, _ StatusManager[O]] struct {
1617
Base
1718
// hooks is a pointer to an EnablementHookFuncs structure so
1819
// that tests can modify the contents of the structure after
@@ -21,15 +22,15 @@ type Status[O any] struct {
2122
}
2223

2324
// Init prepares the structure for use by the CLI core.
24-
func (cmd *Status[O]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) {
25+
func (cmd *Status[O, _]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) {
2526
cmd.CmdClause = parent.Command("status", "Get the enablement status of the "+productName+" product")
2627
cmd.hooks = hooks
2728

28-
cmd.Base.Init(parent, g, productID, productName)
29+
cmd.Base.Init(parent, g)
2930
}
3031

3132
// Exec executes the status operation.
32-
func (cmd *Status[O]) Exec(out io.Writer) error {
33+
func (cmd *Status[O, S]) Exec(out io.Writer, status S) error {
3334
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled {
3435
return fsterr.ErrInvalidVerboseJSONCombo
3536
}
@@ -44,10 +45,7 @@ func (cmd *Status[O]) Exec(out io.Writer) error {
4445
argparser.DisplayServiceID(serviceID, flag, source, out)
4546
}
4647

47-
s := EnablementStatus{ProductID: cmd.ProductID}
48-
state := "disabled"
49-
50-
_, err = cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID)
48+
o, err := cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID)
5149
if err != nil {
5250
var herr *fastly.HTTPError
5351

@@ -59,16 +57,15 @@ func (cmd *Status[O]) Exec(out io.Writer) error {
5957
return err
6058
}
6159
} else {
62-
s.Enabled = true
63-
state = "enabled"
60+
status.SetEnabled(true)
61+
status.TransformOutput(o)
6462
}
6563

66-
if ok, err := cmd.WriteJSON(out, s); ok {
64+
if ok, err := cmd.WriteJSON(out, status); ok {
6765
return err
6866
}
6967

70-
text.Info(out,
71-
"%s is %s on service %s", cmd.ProductName, state, serviceID)
68+
text.Info(out, status.GetTextResult())
7269

7370
return nil
7471
}

0 commit comments

Comments
 (0)