From 41ff80269cc52053d79f9ed9e49cbcf5d2ca4652 Mon Sep 17 00:00:00 2001 From: Kathy Chern Date: Mon, 16 Nov 2026 12:29:51 -0800 Subject: [PATCH 1/2] fix: show an empty GET response as no resource found When a response has no body, the generated command handler prints the last path parameter as the key. That fits a delete or a replace. It does not fit a GET that returns 204, such as the default release plan of an environment that has none, where the output looked like a plan named after the environment. A GET with no body now prints {} in JSON output and "No found" in plaintext output. Other methods keep the key output. Ticket: EMSR-2795. Co-Authored-By: Claude Fable 5.1 --- cmd/resources/empty_response_test.go | 60 ++++++++++++++++++++++++++++ cmd/resources/resources.go | 15 +++++++ 2 files changed, 75 insertions(+) create mode 100644 cmd/resources/empty_response_test.go diff --git a/cmd/resources/empty_response_test.go b/cmd/resources/empty_response_test.go new file mode 100644 index 000000000..71850fcaf --- /dev/null +++ b/cmd/resources/empty_response_test.go @@ -0,0 +1,60 @@ +package resources_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/launchdarkly/ldcli/cmd" + "github.com/launchdarkly/ldcli/internal/analytics" + "github.com/launchdarkly/ldcli/internal/resources" +) + +// A GET that returns no body, such as the default release plan of an +// environment that has none, must not print the last path parameter as a key. +func TestEmptyResponse(t *testing.T) { + tests := []struct { + name string + args []string + expected string + }{ + { + name: "GET with JSON output prints an empty object", + args: []string{"projects", "get", "--project", "my-project", "--output", "json"}, + expected: "{}\n", + }, + { + name: "GET with plaintext output says no resource was found", + args: []string{"projects", "get", "--project", "my-project", "--output", "plaintext"}, + expected: "No projects found\n", + }, + { + name: "GET on a beta resource drops the suffix from the message", + args: []string{"flags-beta", "list-dependent", "--project", "my-project", "--flag", "my-flag", "--output", "plaintext"}, + expected: "No flags found\n", + }, + { + name: "DELETE still prints the key of the deleted resource", + args: []string{"projects", "delete", "--project", "my-project", "--output", "json"}, + expected: `{"key": "my-project"}` + "\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client := &resources.MockClient{Response: []byte("")} + args := append(tt.args, "--access-token", "abcd1234") + + output, err := cmd.CallCmd( + t, + cmd.APIClients{ResourcesClient: client}, + analytics.NoopClientFn{}.Tracker(), + args, + ) + + require.NoError(t, err) + assert.Equal(t, tt.expected, string(output)) + }) + } +} diff --git a/cmd/resources/resources.go b/cmd/resources/resources.go index 188204e14..5999ad640 100644 --- a/cmd/resources/resources.go +++ b/cmd/resources/resources.go @@ -344,6 +344,10 @@ func (op *OperationCmd) makeRequest(cmd *cobra.Command, args []string) error { } if string(res) == "" { + if strings.EqualFold(op.HTTPMethod, "GET") { + fmt.Fprint(cmd.OutOrStdout(), emptyGetOutput(cmd)+"\n") + return nil + } // assuming the key to be deleted/replaced is last in the list of params, // e.g. contexts delete-instances or code-refs replace-branch res = []byte(fmt.Sprintf(`{"key": %q}`, urlParms[len(urlParms)-1])) @@ -362,6 +366,17 @@ func (op *OperationCmd) makeRequest(cmd *cobra.Command, args []string) error { return nil } +// emptyGetOutput reports a GET that found no resource, such as an environment +// with no default release plan, so the response is not mistaken for a resource. +func emptyGetOutput(cmd *cobra.Command) string { + if cliflags.GetOutputKind(cmd) == output.OutputKindJSON.String() { + return "{}" + } + resourceName := strings.ReplaceAll(strings.TrimSuffix(cmd.Parent().Name(), "-beta"), "-", " ") + + return fmt.Sprintf("No %s found", resourceName) +} + func NewOperationCmd( parentCmd *cobra.Command, client resources.Client, From a546e8a3c14a54674a8f4e3025e0c213baed3913 Mon Sep 17 00:00:00 2001 From: Kathy Chern Date: Mon, 16 Nov 2026 13:48:31 -0800 Subject: [PATCH 2/2] fix: say no resource was returned for an empty GET "No found" is not true for get-default, because the environment can have release plans and still have no default. "No returned" describes every empty GET. Co-Authored-By: Claude Fable 5.1 --- cmd/resources/empty_response_test.go | 6 +++--- cmd/resources/resources.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/resources/empty_response_test.go b/cmd/resources/empty_response_test.go index 71850fcaf..d18c31761 100644 --- a/cmd/resources/empty_response_test.go +++ b/cmd/resources/empty_response_test.go @@ -25,14 +25,14 @@ func TestEmptyResponse(t *testing.T) { expected: "{}\n", }, { - name: "GET with plaintext output says no resource was found", + name: "GET with plaintext output says no resource was returned", args: []string{"projects", "get", "--project", "my-project", "--output", "plaintext"}, - expected: "No projects found\n", + expected: "No projects returned\n", }, { name: "GET on a beta resource drops the suffix from the message", args: []string{"flags-beta", "list-dependent", "--project", "my-project", "--flag", "my-flag", "--output", "plaintext"}, - expected: "No flags found\n", + expected: "No flags returned\n", }, { name: "DELETE still prints the key of the deleted resource", diff --git a/cmd/resources/resources.go b/cmd/resources/resources.go index 5999ad640..1dde37ad3 100644 --- a/cmd/resources/resources.go +++ b/cmd/resources/resources.go @@ -374,7 +374,7 @@ func emptyGetOutput(cmd *cobra.Command) string { } resourceName := strings.ReplaceAll(strings.TrimSuffix(cmd.Parent().Name(), "-beta"), "-", " ") - return fmt.Sprintf("No %s found", resourceName) + return fmt.Sprintf("No %s returned", resourceName) } func NewOperationCmd(