Skip to content

Commit a745ec3

Browse files
authored
fix(service-version): Add JSON support to service-version clone command (#1550)
### Change summary Added JSON support to service-version clone command as reported in issue #1353. NOTE: Unable to reproduce the ordering issue/race condition that the customer reported by running the list command immediately after the clone command. We ran this sequence multiple times, but each time the list was returned in the expected order, with the newly cloned service-version listed last. 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? ### Changes to Core Features: * [x] Have you written new tests for your core changes, as applicable? * [x] Have you successfully run tests with your changes locally? ### User Impact * Able to get JSON output from service-version clone command ### Are there any considerations that need to be addressed for release? No breaking changes.
1 parent 1c9fb80 commit a745ec3

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Breaking:
66

77
### Enhancements:
8+
- feat(service-version): Add JSON support to service-version clone command ([#1550](https://github.com/fastly/cli/pull/1550))
89

910
### Bug fixes:
1011

pkg/commands/serviceversion/clone.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
// CloneCommand calls the Fastly API to clone a service version.
1616
type CloneCommand struct {
1717
argparser.Base
18+
argparser.JSONOutput
19+
1820
Input fastly.CloneVersionInput
1921
serviceName argparser.OptionalServiceNameID
2022
serviceVersion argparser.OptionalServiceVersion
@@ -25,6 +27,7 @@ func NewCloneCommand(parent argparser.Registerer, g *global.Data) *CloneCommand
2527
var c CloneCommand
2628
c.Globals = g
2729
c.CmdClause = parent.Command("clone", "Clone a Fastly service version")
30+
c.RegisterFlagBool(c.JSONFlag()) // --json
2831
c.RegisterFlag(argparser.StringFlagOpts{
2932
Name: argparser.FlagServiceIDName,
3033
Description: argparser.FlagServiceIDDesc,
@@ -48,6 +51,9 @@ func NewCloneCommand(parent argparser.Registerer, g *global.Data) *CloneCommand
4851

4952
// Exec invokes the application logic for the command.
5053
func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error {
54+
if c.Globals.Verbose() && c.JSONOutput.Enabled {
55+
return errors.ErrInvalidVerboseJSONCombo
56+
}
5157
serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
5258
APIClient: c.Globals.APIClient,
5359
Manifest: *c.Globals.Manifest,
@@ -76,6 +82,9 @@ func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error {
7682
return err
7783
}
7884

85+
if ok, err := c.WriteJSON(out, ver); ok {
86+
return err
87+
}
7988
text.Success(out, "Cloned service %s version %d to version %d", fastly.ToValue(ver.ServiceID), c.Input.ServiceVersion, fastly.ToValue(ver.Number))
8089
return nil
8190
}

pkg/commands/serviceversion/serviceversion_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func TestVersionClone(t *testing.T) {
3333
},
3434
WantOutput: "Cloned service 123 version 1 to version 4",
3535
},
36+
{
37+
Name: "validate successful clone json output",
38+
Args: "--service-id 123 --version 1 --json",
39+
API: mock.API{
40+
ListVersionsFn: testutil.ListVersions,
41+
CloneVersionFn: testutil.CloneVersionResult(4),
42+
},
43+
WantOutput: cloneServiceVersionJSONOutput,
44+
},
3645
{
3746
Name: "validate error will be passed through if cloning fails",
3847
Args: "--service-id 456 --version 1",
@@ -326,6 +335,23 @@ func TestVersionUnstage(t *testing.T) {
326335
testutil.RunCLIScenarios(t, []string{root.CommandName, "unstage"}, scenarios)
327336
}
328337

338+
var cloneServiceVersionJSONOutput = strings.TrimSpace(`
339+
{
340+
"Active": null,
341+
"Comment": null,
342+
"CreatedAt": null,
343+
"DeletedAt": null,
344+
"Deployed": null,
345+
"Locked": null,
346+
"Number": 4,
347+
"ServiceID": "123",
348+
"Staging": null,
349+
"Testing": null,
350+
"UpdatedAt": null,
351+
"Environments": null
352+
}
353+
`) + "\n"
354+
329355
var listVersionsShortOutput = strings.TrimSpace(`
330356
NUMBER ACTIVE STAGED LAST EDITED (UTC)
331357
1 true false 2000-01-01 01:00

0 commit comments

Comments
 (0)