Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(product): Add 'product' commands for each Fastly product. #1362

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ ifeq ($(OS), Windows_NT)
.SHELLFLAGS = /c
GO_FILES = $(shell where /r pkg *.go)
GO_FILES += $(shell where /r cmd *.go)
GO_FILES += $(shell where /r internal *.go)
CONFIG_SCRIPT = scripts\config.sh
CONFIG_FILE = pkg\config\config.toml
else
GO_FILES = $(shell find cmd pkg -type f -name '*.go')
GO_FILES = $(shell find cmd pkg internal -type f -name '*.go')
CONFIG_SCRIPT = ./scripts/config.sh
CONFIG_FILE = pkg/config/config.toml
endif
Expand Down Expand Up @@ -75,13 +76,13 @@ tidy:
# Run formatter.
.PHONY: fmt
fmt:
@echo gofmt -l ./{cmd,pkg}
@eval "bash -c 'F=\$$(gofmt -l ./{cmd,pkg}) ; if [[ \$$F ]] ; then echo \$$F ; exit 1 ; fi'"
@echo gofmt -l ./{cmd,pkg,internal}
@eval "bash -c 'F=\$$(gofmt -l ./{cmd,pkg,internal}) ; if [[ \$$F ]] ; then echo \$$F ; exit 1 ; fi'"

# Run static analysis.
.PHONY: vet
vet: config ## Run vet static analysis
$(GO_BIN) vet ./{cmd,pkg}/...
$(GO_BIN) vet ./{cmd,pkg,internal}/...

# Run linter.
.PHONY: revive
Expand All @@ -91,7 +92,7 @@ revive: ## Run linter (using revive)
# Run security vulnerability checker.
.PHONY: gosec
gosec: ## Run security vulnerability checker
gosec -quiet -exclude=G104 ./{cmd,pkg}/...
gosec -quiet -exclude=G104 ./{cmd,pkg,internal}/...

nilaway: ## Run nilaway
@nilaway ./...
Expand All @@ -108,13 +109,13 @@ semgrep: ## Run semgrep
# To ignore lines use: //lint:ignore <CODE> <REASON>
.PHONY: staticcheck
staticcheck: ## Run static analysis
staticcheck ./{cmd,pkg}/...
staticcheck ./{cmd,pkg,internal}/...

# Run imports formatter.
.PHONY: imports
imports:
@echo goimports ./{cmd,pkg}
@eval "bash -c 'F=\$$(goimports -l ./{cmd,pkg}) ; if [[ \$$F ]] ; then echo \$$F ; exit 1 ; fi'"
@echo goimports ./{cmd,pkg,internal}
@eval "bash -c 'F=\$$(goimports -l ./{cmd,pkg,internal}) ; if [[ \$$F ]] ; then echo \$$F ; exit 1 ; fi'"

.PHONY: golangci
golangci: ## Run golangci-lint
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ require (
)

require (
github.com/dnaeon/go-vcr v1.2.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/otiai10/mint v1.6.3 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

Expand Down Expand Up @@ -78,3 +79,5 @@ 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.20250113130142-b6a3af55bc44
kpfleming marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj6
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/dustinkirkland/golang-petname v0.0.0-20231002161417-6a283f1aaaf2 h1:S6Dco8FtAhEI/qkg/00H6RdEGC+MCy5GPiQ+xweNRFE=
github.com/dustinkirkland/golang-petname v0.0.0-20231002161417-6a283f1aaaf2/go.mod h1:8AuBTZBRSFqEYBPYULd+NN474/zZBLP+6WeT5S9xlAc=
github.com/fastly/go-fastly/v9 v9.12.0 h1:NUR4l+3LrSCux91sgKduFV8d/eejoGpQNlHa0ftKrFo=
github.com/fastly/go-fastly/v9 v9.12.0/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible h1:FhrXlfhgGCS+uc6YwyiFUt04alnjpoX7vgDKJxS6Qbk=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible/go.mod h1:U8UynVoU1SQaqD2I4ZqgYd5lx3A1ipQYn4aSt2Y5h6c=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
Expand Down Expand Up @@ -67,6 +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.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 All @@ -91,6 +91,7 @@ github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQ
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/nicksnyder/go-i18n v1.10.3 h1:0U60fnLBNrLBVt8vb8Q67yKNs+gykbQuLsIkiesJL+w=
github.com/nicksnyder/go-i18n v1.10.3/go.mod h1:hvLG5HTlZ4UfSuVLSRuX7JRUomIaoKQM19hm6f+no7o=
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
Expand Down Expand Up @@ -128,8 +129,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/theckman/yacspin v0.13.12 h1:CdZ57+n0U6JMuh2xqjnjRq5Haj6v1ner2djtLQRzJr4=
github.com/theckman/yacspin v0.13.12/go.mod h1:Rd2+oG2LmQi5f3zC3yeZAOl245z8QOvrH4OPOJNZxLg=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
Expand Down Expand Up @@ -209,6 +210,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
63 changes: 63 additions & 0 deletions internal/productcore/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package productcore

Check failure on line 1 in internal/productcore/base.go

View workflow job for this annotation

GitHub Actions / lint

should have a package comment

import (
"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
)

// Base is a base type for all product commands.
type Base struct {
argparser.Base
argparser.JSONOutput
Manifest manifest.Data

ServiceName argparser.OptionalServiceNameID
ProductID string
ProductName string
}

// Init prepares the structure for use by the CLI core.
func (cmd *Base) Init(parent argparser.Registerer, g *global.Data, productID, productName string) {

Check failure on line 22 in internal/productcore/base.go

View workflow job for this annotation

GitHub Actions / lint

parameter 'parent' seems to be unused, consider removing or renaming it as _
cmd.Globals = g
cmd.ProductID = productID
cmd.ProductName = productName

// Optional flags.
cmd.RegisterFlag(argparser.StringFlagOpts{
Name: argparser.FlagServiceIDName,
Description: argparser.FlagServiceIDDesc,
Dst: &g.Manifest.Flag.ServiceID,
Short: 's',
})
cmd.RegisterFlag(argparser.StringFlagOpts{
Action: cmd.ServiceName.Set,
Name: argparser.FlagServiceName,
Description: argparser.FlagServiceNameDesc,
Dst: &cmd.ServiceName.Value,
})
cmd.RegisterFlagBool(cmd.JSONFlag()) // --json
}

// EnablementStatus is a structure used to generate JSON output from
// the enablement-related commands
type EnablementStatus struct {
ProductID string `json:"product_id"`
Enabled bool `json:"enabled"`
}

// 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 used 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)
}
57 changes: 57 additions & 0 deletions internal/productcore/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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"
)

// Disable is a base type for all 'disable' commands.
type Disable[O any] struct {
Base
hooks *EnablementHookFuncs[O]
kpfleming marked this conversation as resolved.
Show resolved Hide resolved
}

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

cmd.Base.Init(parent, g, productID, productName)
}

// Exec executes the disablement operation.
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)
return err
}

if cmd.Globals.Verbose() {
argparser.DisplayServiceID(serviceID, flag, source, out)
}

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{ProductID: cmd.ProductID, Enabled: false}); ok {
return err
}

text.Success(out,
"Disabled %s on service %s", cmd.ProductName, serviceID)

return nil
}
56 changes: 56 additions & 0 deletions internal/productcore/enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package productcore

import (
"io"

"github.com/fastly/cli/pkg/argparser"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)

// Enable is a base type for all 'enable' commands.
type Enable[O any] struct {
Base
hooks *EnablementHookFuncs[O]
}

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

cmd.Base.Init(parent, g, productID, productName)
}

// 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)
return err
}

if cmd.Globals.Verbose() {
argparser.DisplayServiceID(serviceID, flag, source, out)
}

_, 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{ProductID: cmd.ProductID, Enabled: true}); ok {
return err
}

text.Success(out,
"Enabled %s on service %s", cmd.ProductName, serviceID)

return nil
}
71 changes: 71 additions & 0 deletions internal/productcore/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package productcore

import (
"errors"
"io"

"github.com/fastly/cli/pkg/argparser"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v9/fastly"
)

// 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, productID, 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, productID, 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{ProductID: cmd.ProductID}
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
}
Loading
Loading