Skip to content

Commit 8cf280d

Browse files
committed
More WIP
1 parent 08979fe commit 8cf280d

File tree

34 files changed

+384
-1273
lines changed

34 files changed

+384
-1273
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,3 @@ require (
7979
)
8080

8181
require 4d63.com/optional v0.2.0
82-
83-
replace github.com/fastly/go-fastly/v9 => github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44

go.sum

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5O
2222
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
2323
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
2424
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
25+
github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0 h1:aYo8nnk3ojoQkP5iErif5Xxv0Mo0Ga/FR5+ffl/7+Nk=
26+
github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0/go.mod h1:8AuBTZBRSFqEYBPYULd+NN474/zZBLP+6WeT5S9xlAc=
27+
github.com/fastly/go-fastly/v9 v9.13.1 h1:fePpzSQOKYg9lILj5rRtCbUC7GPWreSLXP0A7rAB7m4=
28+
github.com/fastly/go-fastly/v9 v9.13.1/go.mod h1:O6R08EWvTThHCgttYA09EiLdfOoLVkgaXdPQanDo534=
2529
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible h1:FhrXlfhgGCS+uc6YwyiFUt04alnjpoX7vgDKJxS6Qbk=
2630
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible/go.mod h1:U8UynVoU1SQaqD2I4ZqgYd5lx3A1ipQYn4aSt2Y5h6c=
2731
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
@@ -63,8 +67,6 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
6367
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
6468
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
6569
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
66-
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44 h1:YdOuOWFVfUN3V3pv3MXE11PTh5A9Emh3NlW8qg2dLJk=
67-
github.com/kpfleming/go-fastly/v9 v9.12.1-0.20250113130142-b6a3af55bc44/go.mod h1:rB3T7CBBYBw+/W4rpzmZPev8BbARin6vriirVCY0yaw=
6870
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
6971
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
7072
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@@ -129,6 +131,11 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
129131
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
130132
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
131133
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
134+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
135+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
136+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
137+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
138+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
132139
github.com/theckman/yacspin v0.13.12 h1:CdZ57+n0U6JMuh2xqjnjRq5Haj6v1ner2djtLQRzJr4=
133140
github.com/theckman/yacspin v0.13.12/go.mod h1:Rd2+oG2LmQi5f3zC3yeZAOl245z8QOvrH4OPOJNZxLg=
134141
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
@@ -208,6 +215,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
208215
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
209216
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
210217
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
218+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
211219
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
212220
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
213221
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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)