Skip to content
Merged
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 @@ -5,6 +5,7 @@
### Breaking:

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

### Bug fixes:

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