diff --git a/CHANGELOG.md b/CHANGELOG.md index ccd6c960d..96f1f03e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - feat(env): add detection for workspace ID ([#1560](https://github.com/fastly/cli/pull/1560)) ### Bug fixes: +- fix(service-version): Add JSON support to service-version clone command to address issue ([#1353](https://github.com/fastly/cli/issues/1353)) - fix(compute): clarify fastly.toml error message when file not found ([#1556](https://github.com/fastly/cli/pull/1556)) ### Dependencies: diff --git a/pkg/commands/serviceversion/clone.go b/pkg/commands/serviceversion/clone.go index 9b0180483..a2bf5b22d 100644 --- a/pkg/commands/serviceversion/clone.go +++ b/pkg/commands/serviceversion/clone.go @@ -15,6 +15,8 @@ import ( // CloneCommand calls the Fastly API to clone a service version. type CloneCommand struct { argparser.Base + argparser.JSONOutput + Input fastly.CloneVersionInput serviceName argparser.OptionalServiceNameID serviceVersion argparser.OptionalServiceVersion @@ -25,6 +27,7 @@ func NewCloneCommand(parent argparser.Registerer, g *global.Data) *CloneCommand var c CloneCommand c.Globals = g c.CmdClause = parent.Command("clone", "Clone a Fastly service version") + c.RegisterFlagBool(c.JSONFlag()) // --json c.RegisterFlag(argparser.StringFlagOpts{ Name: argparser.FlagServiceIDName, Description: argparser.FlagServiceIDDesc, @@ -48,6 +51,9 @@ func NewCloneCommand(parent argparser.Registerer, g *global.Data) *CloneCommand // Exec invokes the application logic for the command. func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error { + if c.Globals.Verbose() && c.JSONOutput.Enabled { + return errors.ErrInvalidVerboseJSONCombo + } serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ APIClient: c.Globals.APIClient, Manifest: *c.Globals.Manifest, @@ -76,6 +82,9 @@ func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error { return err } + if ok, err := c.WriteJSON(out, ver); ok { + return err + } text.Success(out, "Cloned service %s version %d to version %d", fastly.ToValue(ver.ServiceID), c.Input.ServiceVersion, fastly.ToValue(ver.Number)) return nil } diff --git a/pkg/commands/serviceversion/serviceversion_test.go b/pkg/commands/serviceversion/serviceversion_test.go index badacc287..53d5ae7cf 100644 --- a/pkg/commands/serviceversion/serviceversion_test.go +++ b/pkg/commands/serviceversion/serviceversion_test.go @@ -33,6 +33,15 @@ func TestVersionClone(t *testing.T) { }, WantOutput: "Cloned service 123 version 1 to version 4", }, + { + Name: "validate successful clone json output", + Args: "--service-id 123 --version 1 --json", + API: mock.API{ + ListVersionsFn: testutil.ListVersions, + CloneVersionFn: testutil.CloneVersionResult(4), + }, + WantOutput: cloneServiceVersionJSONOutput, + }, { Name: "validate error will be passed through if cloning fails", Args: "--service-id 456 --version 1", @@ -326,6 +335,23 @@ func TestVersionUnstage(t *testing.T) { testutil.RunCLIScenarios(t, []string{root.CommandName, "unstage"}, scenarios) } +var cloneServiceVersionJSONOutput = strings.TrimSpace(` +{ + "Active": null, + "Comment": null, + "CreatedAt": null, + "DeletedAt": null, + "Deployed": null, + "Locked": null, + "Number": 4, + "ServiceID": "123", + "Staging": null, + "Testing": null, + "UpdatedAt": null, + "Environments": null +} +`) + "\n" + var listVersionsShortOutput = strings.TrimSpace(` NUMBER ACTIVE STAGED LAST EDITED (UTC) 1 true false 2000-01-01 01:00