Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be listed as a feature, not a bug fix, even though lack of JSON support could be considered a bug if we had documentation claiming that all commands support JSON output :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the link in the CHANGELOG entry should be to the PR, not to the issue.

- fix(compute): clarify fastly.toml error message when file not found ([#1556](https://github.com/fastly/cli/pull/1556))

### Dependencies:
Expand Down
9 changes: 9 additions & 0 deletions pkg/commands/serviceversion/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions pkg/commands/serviceversion/serviceversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
Loading