Skip to content

Commit

Permalink
fix: json output with sort keys
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Apr 29, 2024
1 parent cde3a7a commit 714d78d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.30.0
github.com/spf13/cobra v1.8.0
kcl-lang.io/kcl-go v0.8.5
kcl-lang.io/kcl-go v0.8.6-0.20240429200928-7db8b504239b
kcl-lang.io/kcl-openapi v0.6.1
kcl-lang.io/kcl-playground v0.5.1
kcl-lang.io/kpm v0.8.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1735,8 +1735,8 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kcl-lang.io/kcl-go v0.8.5 h1:YuaZju34cclGVB8Z1O1hhxZx6lYF4cW3x6yDqK6l3iI=
kcl-lang.io/kcl-go v0.8.5/go.mod h1:CkXBerH9YchN2mP7fTfq5DXdmhXHH2lrbg5TFVT4KL8=
kcl-lang.io/kcl-go v0.8.6-0.20240429200928-7db8b504239b h1:MVb4qLIHdEcGRY1h84BpBlYSIBBADZO2JWAW0rz4RWI=
kcl-lang.io/kcl-go v0.8.6-0.20240429200928-7db8b504239b/go.mod h1:CkXBerH9YchN2mP7fTfq5DXdmhXHH2lrbg5TFVT4KL8=
kcl-lang.io/kcl-openapi v0.6.1 h1:iPH0EvPgDGZS5Lk00/Su5Av6AQP5IBG8f7gAUyevkHE=
kcl-lang.io/kcl-openapi v0.6.1/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
kcl-lang.io/kcl-playground v0.5.1 h1:MKQQUHgt4+2QyU2NVwa73oksOaBJGDi4keGoggA0MiU=
Expand Down
12 changes: 7 additions & 5 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package options

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -205,12 +207,12 @@ func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
}
var output []byte
if strings.ToLower(o.Format) == Json {
// If we have multiple result, output the JSON array format, else output the single JSON object.
if result.Len() > 1 {
output = []byte(result.GetRawJsonResult() + "\n")
} else {
output = []byte(result.First().JSONString() + "\n")
var out bytes.Buffer
err := json.Indent(&out, []byte(result.GetRawJsonResult()), "", " ")
if err != nil {
return err
}
output = []byte(out.String() + "\n")
} else {
// Both considering the raw YAML format and the YAML stream format that contains the `---` separator.
output = []byte(result.GetRawYamlResult() + "\n")
Expand Down
2 changes: 1 addition & 1 deletion pkg/options/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ spec:
var buf2 bytes.Buffer
options.Writer = &buf2
options.Format = Json
options.SortKeys = false
options.SortKeys = true

err = options.Run()
if err != nil {
Expand Down

0 comments on commit 714d78d

Please sign in to comment.