Skip to content

Commit c37cb3e

Browse files
chore(cli): let --format raw be used in conjunction with --transform
1 parent 81a96d9 commit c37cb3e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

pkg/cmd/cmdutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func shouldUseColors(w io.Writer) bool {
312312
}
313313

314314
func formatJSON(expectedOutput *os.File, title string, res gjson.Result, format string, transform string) ([]byte, error) {
315-
if format != "raw" && transform != "" {
315+
if transform != "" {
316316
transformed := res.Get(transform)
317317
if transformed.Exists() {
318318
res = transformed
@@ -356,7 +356,7 @@ func formatJSON(expectedOutput *os.File, title string, res gjson.Result, format
356356

357357
// Display JSON to the user in various different formats
358358
func ShowJSON(out *os.File, title string, res gjson.Result, format string, transform string) error {
359-
if format != "raw" && transform != "" {
359+
if transform != "" {
360360
transformed := res.Get(transform)
361361
if transformed.Exists() {
362362
res = transformed

pkg/cmd/cmdutil_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13+
"github.com/tidwall/gjson"
1314
)
1415

1516
func TestStreamOutput(t *testing.T) {
@@ -148,3 +149,44 @@ func TestValidateBaseURL(t *testing.T) {
148149
assert.Contains(t, err.Error(), "--base-url")
149150
})
150151
}
152+
153+
func TestFormatJSON(t *testing.T) {
154+
t.Parallel()
155+
156+
t.Run("RawWithTransform", func(t *testing.T) {
157+
t.Parallel()
158+
159+
res := gjson.Parse(`{"id":"abc123","name":"test"}`)
160+
formatted, err := formatJSON(os.Stdout, "test", res, "raw", "id")
161+
require.NoError(t, err)
162+
require.Equal(t, `"abc123"`+"\n", string(formatted))
163+
})
164+
165+
t.Run("RawWithoutTransform", func(t *testing.T) {
166+
t.Parallel()
167+
168+
res := gjson.Parse(`{"id":"abc123","name":"test"}`)
169+
formatted, err := formatJSON(os.Stdout, "test", res, "raw", "")
170+
require.NoError(t, err)
171+
require.Equal(t, `{"id":"abc123","name":"test"}`+"\n", string(formatted))
172+
})
173+
174+
t.Run("RawWithNestedTransform", func(t *testing.T) {
175+
t.Parallel()
176+
177+
res := gjson.Parse(`{"data":{"items":[1,2,3]}}`)
178+
formatted, err := formatJSON(os.Stdout, "test", res, "raw", "data.items")
179+
require.NoError(t, err)
180+
require.Equal(t, "[1,2,3]\n", string(formatted))
181+
})
182+
183+
t.Run("RawWithNonexistentTransform", func(t *testing.T) {
184+
t.Parallel()
185+
186+
res := gjson.Parse(`{"id":"abc123"}`)
187+
formatted, err := formatJSON(os.Stdout, "test", res, "raw", "missing")
188+
require.NoError(t, err)
189+
// Transform path doesn't exist, so original result is returned
190+
require.Equal(t, `{"id":"abc123"}`+"\n", string(formatted))
191+
})
192+
}

0 commit comments

Comments
 (0)