Skip to content

Commit 714d78d

Browse files
committed
fix: json output with sort keys
Signed-off-by: peefy <[email protected]>
1 parent cde3a7a commit 714d78d

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/onsi/ginkgo/v2 v2.17.1
88
github.com/onsi/gomega v1.30.0
99
github.com/spf13/cobra v1.8.0
10-
kcl-lang.io/kcl-go v0.8.5
10+
kcl-lang.io/kcl-go v0.8.6-0.20240429200928-7db8b504239b
1111
kcl-lang.io/kcl-openapi v0.6.1
1212
kcl-lang.io/kcl-playground v0.5.1
1313
kcl-lang.io/kpm v0.8.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,8 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
17351735
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
17361736
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
17371737
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
1738-
kcl-lang.io/kcl-go v0.8.5 h1:YuaZju34cclGVB8Z1O1hhxZx6lYF4cW3x6yDqK6l3iI=
1739-
kcl-lang.io/kcl-go v0.8.5/go.mod h1:CkXBerH9YchN2mP7fTfq5DXdmhXHH2lrbg5TFVT4KL8=
1738+
kcl-lang.io/kcl-go v0.8.6-0.20240429200928-7db8b504239b h1:MVb4qLIHdEcGRY1h84BpBlYSIBBADZO2JWAW0rz4RWI=
1739+
kcl-lang.io/kcl-go v0.8.6-0.20240429200928-7db8b504239b/go.mod h1:CkXBerH9YchN2mP7fTfq5DXdmhXHH2lrbg5TFVT4KL8=
17401740
kcl-lang.io/kcl-openapi v0.6.1 h1:iPH0EvPgDGZS5Lk00/Su5Av6AQP5IBG8f7gAUyevkHE=
17411741
kcl-lang.io/kcl-openapi v0.6.1/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
17421742
kcl-lang.io/kcl-playground v0.5.1 h1:MKQQUHgt4+2QyU2NVwa73oksOaBJGDi4keGoggA0MiU=

pkg/options/run.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package options
44

55
import (
6+
"bytes"
7+
"encoding/json"
68
"fmt"
79
"io"
810
"os"
@@ -205,12 +207,12 @@ func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
205207
}
206208
var output []byte
207209
if strings.ToLower(o.Format) == Json {
208-
// If we have multiple result, output the JSON array format, else output the single JSON object.
209-
if result.Len() > 1 {
210-
output = []byte(result.GetRawJsonResult() + "\n")
211-
} else {
212-
output = []byte(result.First().JSONString() + "\n")
210+
var out bytes.Buffer
211+
err := json.Indent(&out, []byte(result.GetRawJsonResult()), "", " ")
212+
if err != nil {
213+
return err
213214
}
215+
output = []byte(out.String() + "\n")
214216
} else {
215217
// Both considering the raw YAML format and the YAML stream format that contains the `---` separator.
216218
output = []byte(result.GetRawYamlResult() + "\n")

pkg/options/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ spec:
5454
var buf2 bytes.Buffer
5555
options.Writer = &buf2
5656
options.Format = Json
57-
options.SortKeys = false
57+
options.SortKeys = true
5858

5959
err = options.Run()
6060
if err != nil {

0 commit comments

Comments
 (0)