diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a2af88a..229cc4c23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Bug Fixes: +- fix(json): `secret-store list --json` emits `[]` rather than `null` on an account with no secret stores + ### Enhancements: ### Dependencies: diff --git a/pkg/commands/secretstore/list.go b/pkg/commands/secretstore/list.go index 769676469..be35ae6bd 100644 --- a/pkg/commands/secretstore/list.go +++ b/pkg/commands/secretstore/list.go @@ -45,7 +45,10 @@ func (c *ListCommand) Exec(in io.Reader, out io.Writer) error { return fsterr.ErrInvalidVerboseJSONCombo } - var data []fastly.SecretStore + // Initialised rather than declared nil so that an account with no secret + // stores encodes as `[]` instead of `null`: encoding/json writes a nil + // slice as null, and `--json` output should be a list either way. + data := make([]fastly.SecretStore, 0) for { o, err := c.Globals.APIClient.ListSecretStores(context.TODO(), &c.Input) diff --git a/pkg/commands/secretstore/secretstore_test.go b/pkg/commands/secretstore/secretstore_test.go index 634b7ed3f..d46f6349f 100644 --- a/pkg/commands/secretstore/secretstore_test.go +++ b/pkg/commands/secretstore/secretstore_test.go @@ -341,6 +341,30 @@ func TestListStoresCommand(t *testing.T) { wantAPIInvoked: true, wantOutput: fstfmt.EncodeJSON([]fastly.SecretStore{stores.Data[0]}), }, + // An account with no secret stores used to print `null` here while + // `config-store list --json` printed `[]` for the same situation. + { + args: "list --json", + api: mock.API{ + ListSecretStoresFn: func(_ context.Context, _ *fastly.ListSecretStoresInput) (*fastly.SecretStores, error) { + return &fastly.SecretStores{Data: []fastly.SecretStore{}}, nil + }, + }, + wantAPIInvoked: true, + wantOutput: "[]\n", + }, + // The same, reached by the other route: the API returned no response + // body at all, so nothing was ever appended. + { + args: "list --json", + api: mock.API{ + ListSecretStoresFn: func(_ context.Context, _ *fastly.ListSecretStoresInput) (*fastly.SecretStores, error) { + return nil, nil + }, + }, + wantAPIInvoked: true, + wantOutput: "[]\n", + }, } for _, testcase := range scenarios {