Skip to content

Commit 497d9c9

Browse files
authored
CDTOOL-1260: Introduce 'service purge' command and 'purge' alias. (#1612)
### Change summary This PR adds the 'service purge' command as a replacement for the 'purge' command. The 'purge' command is still available as an alias, but will issue a deprecation warning when it is used. This involved a number of changes: 1. The existing 'pkg/commands/purge' package content was moved to 'pkg/commands/service/purge', and modified to work as expected in that location. 2. A new 'pkg/commands/alias' package was added to hold alias-only packages. 3. An alias was added in 'pkg/commands/alias/purge'. This alias uses `struct` embedding to avoid duplicating any of the code from the `pkg/commands/service/purge' package. The alias command is 'unlisted' and issues a deprecation warning when it is used. This PR also fixes the `make fmt` target in the `Makefile` to work properly. All Submissions: * [X] Have you followed the guidelines in our Contributing document? * [X] Have you checked to ensure there aren't other open [Pull Requests](https://github.com/fastly/cli/pulls) for the same update/change? <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### New Feature Submissions: * [X] Does your submission pass tests? ### User Impact <!-- What is the user impact of this change? --> New deprecation warning for the `purge` command.
1 parent f0a46b9 commit 497d9c9

11 files changed

Lines changed: 72 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
### Enhancements:
88
- feat(rust): Allow testing with prerelease Rust versions ([#1604](https://github.com/fastly/cli/pull/1604))
99
- feat(compute/hashfiles): remove hashsum subcommand ([#1608](https://github.com/fastly/cli/pull/1608))
10-
- feat(commands/ngwaf/rules): add support for CRUD operations for NGWAF rules ([#1578](https://github.com/fastly/cli/pull/1605))
10+
- feat(commands/ngwaf/rules): add support for CRUD operations for NGWAF rules ([#1605](https://github.com/fastly/cli/pull/1605))
1111
- feat(compute/deploy): added the `--no-default-domain` flag to allow for the skipping of automatic domain creation when deploying a Compute service([#1610](https://github.com/fastly/cli/pull/1610))
12+
- feat(commands/service/purge): Add 'service purge' command as replacement for 'purge', with an unlisted and deprecated alias of 'purge'. ([#1612](https://github.com/fastly/cli/pull/1612))
1213

1314
### Bug fixes:
1415

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ tidy:
7474
# Run formatter.
7575
.PHONY: fmt
7676
fmt:
77-
golangci-lint fmt
77+
$(GOLANGCI_LINT) fmt
7878

7979
# Run semgrep checker.
8080
# NOTE: We can only exclude the import-text-template rule via a semgrep CLI flag

pkg/app/run_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ object-storage
8787
pops
8888
products
8989
profile
90-
purge
9190
rate-limit
9291
resource-link
9392
secret-store

pkg/commands/alias/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package alias contains aliases for commands which have been renamed/relocated and are deprecated.
2+
package alias

pkg/commands/alias/purge/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package purge contains the 'purge' alias for the 'service purge' command.
2+
package purge

pkg/commands/alias/purge/purge.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package purge
2+
3+
import (
4+
"io"
5+
6+
servicepurge "github.com/fastly/cli/pkg/commands/service/purge"
7+
8+
"github.com/fastly/cli/pkg/argparser"
9+
"github.com/fastly/cli/pkg/global"
10+
"github.com/fastly/cli/pkg/text"
11+
)
12+
13+
// PurgeCommand wraps the PurgeCommand from the servicepurge package.
14+
type Command struct {
15+
*servicepurge.PurgeCommand
16+
}
17+
18+
// NewCommand returns a usable command registered under the parent.
19+
func NewCommand(parent argparser.Registerer, g *global.Data) *Command {
20+
c := Command{servicepurge.NewPurgeCommand(parent, g)}
21+
c.CmdClause.Hidden()
22+
return &c
23+
}
24+
25+
// Exec implements the command interface.
26+
func (c *Command) Exec(in io.Reader, out io.Writer) error {
27+
text.Deprecated(out, "Use the 'service purge' command instead.")
28+
return c.PurgeCommand.Exec(in, out)
29+
}

pkg/commands/commands.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/fastly/cli/pkg/commands/acl"
88
"github.com/fastly/cli/pkg/commands/aclentry"
99
"github.com/fastly/cli/pkg/commands/alerts"
10+
aliaspurge "github.com/fastly/cli/pkg/commands/alias/purge"
1011
"github.com/fastly/cli/pkg/commands/authtoken"
1112
"github.com/fastly/cli/pkg/commands/backend"
1213
"github.com/fastly/cli/pkg/commands/compute"
@@ -88,12 +89,12 @@ import (
8889
"github.com/fastly/cli/pkg/commands/pop"
8990
"github.com/fastly/cli/pkg/commands/products"
9091
"github.com/fastly/cli/pkg/commands/profile"
91-
"github.com/fastly/cli/pkg/commands/purge"
9292
"github.com/fastly/cli/pkg/commands/ratelimit"
9393
"github.com/fastly/cli/pkg/commands/resourcelink"
9494
"github.com/fastly/cli/pkg/commands/secretstore"
9595
"github.com/fastly/cli/pkg/commands/secretstoreentry"
9696
"github.com/fastly/cli/pkg/commands/service"
97+
servicepurge "github.com/fastly/cli/pkg/commands/service/purge"
9798
"github.com/fastly/cli/pkg/commands/serviceauth"
9899
"github.com/fastly/cli/pkg/commands/serviceversion"
99100
"github.com/fastly/cli/pkg/commands/shellcomplete"
@@ -593,7 +594,6 @@ func Define( // nolint:revive // function-length
593594
profileSwitch := profile.NewSwitchCommand(profileCmdRoot.CmdClause, data, ssoCmdRoot)
594595
profileToken := profile.NewTokenCommand(profileCmdRoot.CmdClause, data)
595596
profileUpdate := profile.NewUpdateCommand(profileCmdRoot.CmdClause, data, ssoCmdRoot)
596-
purgeCmdRoot := purge.NewRootCommand(app, data)
597597
rateLimitCmdRoot := ratelimit.NewRootCommand(app, data)
598598
rateLimitCreate := ratelimit.NewCreateCommand(rateLimitCmdRoot.CmdClause, data)
599599
rateLimitDelete := ratelimit.NewDeleteCommand(rateLimitCmdRoot.CmdClause, data)
@@ -623,6 +623,7 @@ func Define( // nolint:revive // function-length
623623
serviceList := service.NewListCommand(serviceCmdRoot.CmdClause, data)
624624
serviceSearch := service.NewSearchCommand(serviceCmdRoot.CmdClause, data)
625625
serviceUpdate := service.NewUpdateCommand(serviceCmdRoot.CmdClause, data)
626+
servicePurge := servicepurge.NewPurgeCommand(serviceCmdRoot.CmdClause, data)
626627
serviceauthCmdRoot := serviceauth.NewRootCommand(app, data)
627628
serviceauthCreate := serviceauth.NewCreateCommand(serviceauthCmdRoot.CmdClause, data)
628629
serviceauthDelete := serviceauth.NewDeleteCommand(serviceauthCmdRoot.CmdClause, data)
@@ -712,6 +713,9 @@ func Define( // nolint:revive // function-length
712713
versionCmdRoot := version.NewRootCommand(app, data)
713714
whoamiCmdRoot := whoami.NewRootCommand(app, data)
714715

716+
// Aliases for deprecated commands
717+
aliasPurge := aliaspurge.NewCommand(app, data)
718+
715719
return []argparser.Command{
716720
shellcompleteCmdRoot,
717721
aclCmdRoot,
@@ -1167,7 +1171,6 @@ func Define( // nolint:revive // function-length
11671171
profileSwitch,
11681172
profileToken,
11691173
profileUpdate,
1170-
purgeCmdRoot,
11711174
rateLimitCmdRoot,
11721175
rateLimitCreate,
11731176
rateLimitDelete,
@@ -1195,6 +1198,7 @@ func Define( // nolint:revive // function-length
11951198
serviceList,
11961199
serviceSearch,
11971200
serviceUpdate,
1201+
servicePurge,
11981202
serviceauthCmdRoot,
11991203
serviceauthCreate,
12001204
serviceauthDelete,
@@ -1284,5 +1288,6 @@ func Define( // nolint:revive // function-length
12841288
vclSnippetUpdate,
12851289
versionCmdRoot,
12861290
whoamiCmdRoot,
1291+
aliasPurge,
12871292
}
12881293
}
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@ import (
2121
// CommandName is the string to be used to invoke this command.
2222
const CommandName = "purge"
2323

24-
// NewRootCommand returns a new command registered in the parent.
25-
func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand {
26-
var c RootCommand
24+
// PurgeCommand calls the Fastly API to purge items from the cache.
25+
type PurgeCommand struct { //revive:disable:exported
26+
argparser.Base
27+
28+
all bool
29+
file string
30+
key string
31+
serviceName argparser.OptionalServiceNameID
32+
soft bool
33+
url string
34+
}
35+
36+
// NewPurgeCommand returns a usable command registered under the parent.
37+
func NewPurgeCommand(parent argparser.Registerer, g *global.Data) *PurgeCommand {
38+
var c PurgeCommand
39+
2740
c.CmdClause = parent.Command(CommandName, "Invalidate objects in the Fastly cache")
2841
c.Globals = g
2942

@@ -49,21 +62,8 @@ func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand {
4962
return &c
5063
}
5164

52-
// RootCommand is the parent command for all subcommands in this package.
53-
// It should be installed under the primary root command.
54-
type RootCommand struct {
55-
argparser.Base
56-
57-
all bool
58-
file string
59-
key string
60-
serviceName argparser.OptionalServiceNameID
61-
soft bool
62-
url string
63-
}
64-
6565
// Exec implements the command interface.
66-
func (c *RootCommand) Exec(_ io.Reader, out io.Writer) error {
66+
func (c *PurgeCommand) Exec(_ io.Reader, out io.Writer) error {
6767
serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog)
6868
if err != nil {
6969
return err
@@ -135,7 +135,7 @@ func (c *RootCommand) Exec(_ io.Reader, out io.Writer) error {
135135
return nil
136136
}
137137

138-
func (c *RootCommand) purgeAll(serviceID string, out io.Writer) error {
138+
func (c *PurgeCommand) purgeAll(serviceID string, out io.Writer) error {
139139
p, err := c.Globals.APIClient.PurgeAll(context.TODO(), &fastly.PurgeAllInput{
140140
ServiceID: serviceID,
141141
})
@@ -152,7 +152,7 @@ func (c *RootCommand) purgeAll(serviceID string, out io.Writer) error {
152152
// purgeKey now uses the bulk purge endpoint to avoid serialization of the 'key' field values.
153153
// This serialization occurs due to the nature of the POST /service/{service_id}/purge/{surrogate_key}
154154
// endpoint storing the 'key' as part of the URL.
155-
func (c *RootCommand) purgeKey(serviceID string, out io.Writer) error {
155+
func (c *PurgeCommand) purgeKey(serviceID string, out io.Writer) error {
156156
m, err := c.Globals.APIClient.PurgeKeys(context.TODO(), &fastly.PurgeKeysInput{
157157
ServiceID: serviceID,
158158
Keys: []string{c.key},
@@ -177,7 +177,7 @@ func (c *RootCommand) purgeKey(serviceID string, out io.Writer) error {
177177
return nil
178178
}
179179

180-
func (c *RootCommand) purgeKeys(serviceID string, out io.Writer) error {
180+
func (c *PurgeCommand) purgeKeys(serviceID string, out io.Writer) error {
181181
keys, err := populateKeys(c.file, c.Globals.ErrLog)
182182
if err != nil {
183183
c.Globals.ErrLog.AddWithContext(err, map[string]any{
@@ -189,7 +189,7 @@ func (c *RootCommand) purgeKeys(serviceID string, out io.Writer) error {
189189
return c.purgeBulkKeys(serviceID, keys, out)
190190
}
191191

192-
func (c *RootCommand) purgeBulkKeys(serviceID string, keys []string, out io.Writer) error {
192+
func (c *PurgeCommand) purgeBulkKeys(serviceID string, keys []string, out io.Writer) error {
193193
m, err := c.Globals.APIClient.PurgeKeys(context.TODO(), &fastly.PurgeKeysInput{
194194
ServiceID: serviceID,
195195
Keys: keys,
@@ -220,7 +220,7 @@ func (c *RootCommand) purgeBulkKeys(serviceID string, keys []string, out io.Writ
220220
return nil
221221
}
222222

223-
func (c *RootCommand) purgeURL(out io.Writer) error {
223+
func (c *PurgeCommand) purgeURL(out io.Writer) error {
224224
p, err := c.Globals.APIClient.Purge(context.TODO(), &fastly.PurgeInput{
225225
URL: c.url,
226226
Soft: c.soft,
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
"github.com/fastly/go-fastly/v12/fastly"
99

10-
root "github.com/fastly/cli/pkg/commands/purge"
10+
root "github.com/fastly/cli/pkg/commands/service"
11+
purge "github.com/fastly/cli/pkg/commands/service/purge"
1112
"github.com/fastly/cli/pkg/mock"
1213
"github.com/fastly/cli/pkg/testutil"
1314
)
@@ -53,7 +54,7 @@ func TestPurgeAll(t *testing.T) {
5354
},
5455
}
5556

56-
testutil.RunCLIScenarios(t, []string{root.CommandName}, scenarios)
57+
testutil.RunCLIScenarios(t, []string{root.CommandName, purge.CommandName}, scenarios)
5758
}
5859

5960
func TestPurgeKeys(t *testing.T) {
@@ -103,7 +104,7 @@ func TestPurgeKeys(t *testing.T) {
103104
},
104105
}
105106

106-
testutil.RunCLIScenarios(t, []string{root.CommandName}, scenarios)
107+
testutil.RunCLIScenarios(t, []string{root.CommandName, purge.CommandName}, scenarios)
107108
assertKeys(keys, t)
108109
}
109110

@@ -167,7 +168,7 @@ func TestPurgeKey(t *testing.T) {
167168
},
168169
}
169170

170-
testutil.RunCLIScenarios(t, []string{root.CommandName}, scenarios)
171+
testutil.RunCLIScenarios(t, []string{root.CommandName, purge.CommandName}, scenarios)
171172
}
172173

173174
func TestPurgeURL(t *testing.T) {
@@ -217,5 +218,5 @@ func TestPurgeURL(t *testing.T) {
217218
},
218219
}
219220

220-
testutil.RunCLIScenarios(t, []string{root.CommandName}, scenarios)
221+
testutil.RunCLIScenarios(t, []string{root.CommandName, purge.CommandName}, scenarios)
221222
}

0 commit comments

Comments
 (0)