diff --git a/go.mod b/go.mod index 879b930..ca5071a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 34de02d..67b960a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/options/run.go b/pkg/options/run.go index 42f3798..239ad5a 100644 --- a/pkg/options/run.go +++ b/pkg/options/run.go @@ -3,6 +3,8 @@ package options import ( + "bytes" + "encoding/json" "fmt" "io" "os" @@ -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") diff --git a/pkg/options/run_test.go b/pkg/options/run_test.go index 144b8b2..a0fe1cd 100644 --- a/pkg/options/run_test.go +++ b/pkg/options/run_test.go @@ -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 {