From b42882f2a3d9c407bdc5209a0ced7e90acc09f4f Mon Sep 17 00:00:00 2001 From: "Deannah B. (she/her)" Date: Thu, 21 Aug 2025 14:37:46 -0600 Subject: [PATCH 1/6] Add JSON support to service-version clone command As part of issue 1353, the service-version clone command is missing the option for JSON output and to use it in scripts the output need to be parsed. This commit adds functionality to accept the --json argument to the service-version clone command and outputs the appropriate format. Unit tests passing. --- pkg/commands/serviceversion/clone.go | 17 ++++++++++-- .../serviceversion/serviceversion_test.go | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pkg/commands/serviceversion/clone.go b/pkg/commands/serviceversion/clone.go index 9b0180483..d419c1cd4 100644 --- a/pkg/commands/serviceversion/clone.go +++ b/pkg/commands/serviceversion/clone.go @@ -8,6 +8,7 @@ import ( "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/errors" + fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" ) @@ -15,6 +16,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 +28,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 +52,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 fsterr.ErrInvalidVerboseJSONCombo + } serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ APIClient: c.Globals.APIClient, Manifest: *c.Globals.Manifest, @@ -56,6 +63,7 @@ func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error { ServiceVersionFlag: c.serviceVersion, VerboseMode: c.Globals.Flags.Verbose, }) + if err != nil { c.Globals.ErrLog.AddWithContext(err, map[string]any{ "Service ID": serviceID, @@ -76,6 +84,11 @@ func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error { 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 + if ok, err := c.WriteJSON(out, ver); ok { + return err + } else { + + 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 From 261aa54aa851503a1f8564407e8a71fef0fbd4a5 Mon Sep 17 00:00:00 2001 From: "Deannah B. (she/her)" Date: Wed, 8 Oct 2025 13:07:58 -0600 Subject: [PATCH 2/6] Fix linter error in clone function --- pkg/commands/serviceversion/clone.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/commands/serviceversion/clone.go b/pkg/commands/serviceversion/clone.go index d419c1cd4..9be62dffc 100644 --- a/pkg/commands/serviceversion/clone.go +++ b/pkg/commands/serviceversion/clone.go @@ -86,9 +86,7 @@ func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error { if ok, err := c.WriteJSON(out, ver); ok { return err - } else { - - text.Success(out, "Cloned service %s version %d to version %d", fastly.ToValue(ver.ServiceID), c.Input.ServiceVersion, fastly.ToValue(ver.Number)) - return nil } + text.Success(out, "Cloned service %s version %d to version %d", fastly.ToValue(ver.ServiceID), c.Input.ServiceVersion, fastly.ToValue(ver.Number)) + return nil } From 1864a0526854deac91e2eb081d1d9c6582a32714 Mon Sep 17 00:00:00 2001 From: "Deannah B. (she/her)" Date: Tue, 14 Oct 2025 12:17:30 -0600 Subject: [PATCH 3/6] Add changelog notes --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f648f2f5..226491c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ### Enhancements: ### Bug fixes: +- fix(service-version): Add JSON support to service-version clone command to address issue ([#1353](https://github.com/fastly/cli/issues/1353)) + ### Dependencies: - build(deps): `github.com/hashicorp/cap` from 0.10.0 to 0.11.0 ([#1546](https://github.com/fastly/cli/pull/1546)) From 9ddb27a309c7434613aab58d9f0f8ac0a24a0e27 Mon Sep 17 00:00:00 2001 From: "Deannah B. (she/her)" Date: Tue, 14 Oct 2025 12:30:08 -0600 Subject: [PATCH 4/6] Fix spacing/code cleanup --- CHANGELOG.md | 1 - pkg/commands/serviceversion/clone.go | 1 - 2 files changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 226491c0d..76072d7b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ ### Bug fixes: - fix(service-version): Add JSON support to service-version clone command to address issue ([#1353](https://github.com/fastly/cli/issues/1353)) - ### Dependencies: - build(deps): `github.com/hashicorp/cap` from 0.10.0 to 0.11.0 ([#1546](https://github.com/fastly/cli/pull/1546)) - build(deps): `github.com/coreos/go-oidc/v3` from 3.15.0 to 3.16.0 ([#1546](https://github.com/fastly/cli/pull/1546)) diff --git a/pkg/commands/serviceversion/clone.go b/pkg/commands/serviceversion/clone.go index 9be62dffc..86ca17771 100644 --- a/pkg/commands/serviceversion/clone.go +++ b/pkg/commands/serviceversion/clone.go @@ -63,7 +63,6 @@ func (c *CloneCommand) Exec(_ io.Reader, out io.Writer) error { ServiceVersionFlag: c.serviceVersion, VerboseMode: c.Globals.Flags.Verbose, }) - if err != nil { c.Globals.ErrLog.AddWithContext(err, map[string]any{ "Service ID": serviceID, From 16f39505f299eb654e204c3f159eae3148c0ffb0 Mon Sep 17 00:00:00 2001 From: "Deannah B. (she/her)" Date: Wed, 22 Oct 2025 12:49:25 -0600 Subject: [PATCH 5/6] Remove redundant error package --- pkg/commands/serviceversion/clone.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/commands/serviceversion/clone.go b/pkg/commands/serviceversion/clone.go index 86ca17771..a2bf5b22d 100644 --- a/pkg/commands/serviceversion/clone.go +++ b/pkg/commands/serviceversion/clone.go @@ -8,7 +8,6 @@ import ( "github.com/fastly/cli/pkg/argparser" "github.com/fastly/cli/pkg/errors" - fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/text" ) @@ -53,7 +52,7 @@ 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 fsterr.ErrInvalidVerboseJSONCombo + return errors.ErrInvalidVerboseJSONCombo } serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ APIClient: c.Globals.APIClient, From d6b139cece9d2c7a0c359ff0301ef878ed159002 Mon Sep 17 00:00:00 2001 From: "Deannah B. (she/her)" Date: Wed, 5 Nov 2025 13:17:03 -0700 Subject: [PATCH 6/6] Change categorization from bug to feature in Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76072d7b6..49ad1fb51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,9 @@ ### Breaking: ### Enhancements: +- feat(service-version): Add JSON support to service-version clone command ([#1550](https://github.com/fastly/cli/pull/1550)) ### Bug fixes: -- fix(service-version): Add JSON support to service-version clone command to address issue ([#1353](https://github.com/fastly/cli/issues/1353)) ### Dependencies: - build(deps): `github.com/hashicorp/cap` from 0.10.0 to 0.11.0 ([#1546](https://github.com/fastly/cli/pull/1546))