Skip to content

Commit a7be78c

Browse files
authored
Merge pull request #95 from Peefy/feat-toml-format
feat: toml format output
2 parents 26f40e6 + 3cc4966 commit a7be78c

File tree

8 files changed

+65
-29
lines changed

8 files changed

+65
-29
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.0-beta.1
1+
0.9.0-rc.1

cmd/kcl/commands/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ For example, 'kcl run path/to/kcl.k' will run the file named path/to/kcl.k
1818
# Run a single file and output JSON
1919
kcl run path/to/kcl.k --format json
2020
21+
# Run a single file and output TOML
22+
kcl run path/to/kcl.k --format toml
23+
2124
# Run multiple files
2225
kcl run path/to/kcl1.k path/to/kcl2.k
2326

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/onsi/ginkgo/v2 v2.19.0
88
github.com/onsi/gomega v1.33.1
99
github.com/spf13/cobra v1.8.0
10-
kcl-lang.io/kcl-go v0.9.0-beta.1
10+
kcl-lang.io/kcl-go v0.9.0-rc.1
1111
kcl-lang.io/kcl-openapi v0.6.1
1212
kcl-lang.io/kcl-playground v0.5.1
1313
kcl-lang.io/kpm v0.9.0-rc.1
@@ -48,10 +48,12 @@ require (
4848
github.com/kubescape/go-git-url v0.0.30 // indirect
4949
github.com/mitchellh/go-homedir v1.1.0 // indirect
5050
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
51+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
5152
github.com/moby/sys/mountinfo v0.7.1 // indirect
5253
github.com/moby/sys/user v0.1.0 // indirect
5354
github.com/opencontainers/runtime-spec v1.2.0 // indirect
5455
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
56+
github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc // indirect
5557
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
5658
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
5759
github.com/ulikunitz/xz v0.5.12 // indirect
@@ -118,7 +120,7 @@ require (
118120
github.com/go-playground/locales v0.14.1 // indirect
119121
github.com/go-playground/universal-translator v0.18.1 // indirect
120122
github.com/go-playground/validator/v10 v10.14.0 // indirect
121-
github.com/goccy/go-yaml v1.11.3 // indirect
123+
github.com/goccy/go-yaml v1.11.3
122124
github.com/gofrs/flock v0.8.1 // indirect
123125
github.com/gogo/protobuf v1.3.2 // indirect
124126
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
717717
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
718718
github.com/kubescape/go-git-url v0.0.30 h1:PIbg86ae0ftee/p/Tu/6CA1ju6VoJ51G3sQWNHOm6wg=
719719
github.com/kubescape/go-git-url v0.0.30/go.mod h1:3ddc1HEflms1vMhD9owt/3FBES070UaYTUarcjx8jDk=
720+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
721+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
720722
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
721723
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
722724
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@@ -744,6 +746,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
744746
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
745747
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
746748
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
749+
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
750+
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
747751
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
748752
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
749753
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
@@ -859,6 +863,8 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
859863
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
860864
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
861865
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
866+
github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc h1:DRZwH75/E4a2SOr7+gKZ99OEhmjzBzAhgyTnzo1TepY=
867+
github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
862868
github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA=
863869
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
864870
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
@@ -1677,8 +1683,8 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
16771683
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
16781684
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
16791685
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
1680-
kcl-lang.io/kcl-go v0.9.0-beta.1 h1:NfOiktimPlSx/pafqRTvPvPTh8nHtncZCFz0UQfbWiE=
1681-
kcl-lang.io/kcl-go v0.9.0-beta.1/go.mod h1:mkAN+NjbmyaVljshuneKtT4PKOZzu2Z+ayX3yWS+j0E=
1686+
kcl-lang.io/kcl-go v0.9.0-rc.1 h1:AyS6PDmn1L3BgNVW/r05L+d/5KmuKtEwgJr7n8YQ7p8=
1687+
kcl-lang.io/kcl-go v0.9.0-rc.1/go.mod h1:P3tVAITO71STsZ1M62MDj8jE/9nx3XuNFXy6Kr3UTRY=
16821688
kcl-lang.io/kcl-openapi v0.6.1 h1:iPH0EvPgDGZS5Lk00/Su5Av6AQP5IBG8f7gAUyevkHE=
16831689
kcl-lang.io/kcl-openapi v0.6.1/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
16841690
kcl-lang.io/kcl-playground v0.5.1 h1:MKQQUHgt4+2QyU2NVwa73oksOaBJGDi4keGoggA0MiU=

pkg/options/constants.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const (
66
// Json is the JSON output format.
77
Json string = "json"
88
// Yaml is the YAML output format.
9-
Yaml string = "yaml"
9+
Yaml string = "yaml"
10+
// Toml is the TOML output format.
11+
Toml string = "toml"
1012
GoStruct string = "gostruct"
1113
Auto string = "auto"
1214
Crd string = "crd"

pkg/options/run.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import (
1212
"strings"
1313

1414
"github.com/acarl005/stripansi"
15+
"github.com/goccy/go-yaml"
1516
"github.com/pkg/errors"
1617
"kcl-lang.io/cli/pkg/fs"
18+
"kcl-lang.io/kcl-go/pkg/3rdparty/toml"
1719
"kcl-lang.io/kcl-go/pkg/kcl"
20+
"kcl-lang.io/kcl-go/pkg/tools/gen"
1821
"kcl-lang.io/kpm/pkg/api"
1922
"kcl-lang.io/kpm/pkg/client"
2023
"kcl-lang.io/kpm/pkg/git"
@@ -202,8 +205,8 @@ func (o *RunOptions) Complete(args []string) error {
202205

203206
// Validate validates the options.
204207
func (o *RunOptions) Validate() error {
205-
if o.Format != "" && strings.ToLower(o.Format) != Json && strings.ToLower(o.Format) != Yaml {
206-
return fmt.Errorf("invalid output format, expected %v, got %v", []string{Json, Yaml}, o.Format)
208+
if o.Format != "" && strings.ToLower(o.Format) != Json && strings.ToLower(o.Format) != Yaml && strings.ToLower(o.Format) != Toml {
209+
return fmt.Errorf("invalid output format, expected %v, got %v", []string{Json, Yaml, Toml}, o.Format)
207210
}
208211
for _, setting := range o.Settings {
209212
if _, err := os.Stat(setting); err != nil {
@@ -225,6 +228,26 @@ func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
225228
return err
226229
}
227230
output = []byte(out.String() + "\n")
231+
} else if strings.ToLower(o.Format) == Toml {
232+
var out []byte
233+
var err error
234+
if o.SortKeys {
235+
yamlData := make(map[string]any)
236+
if err := yaml.UnmarshalWithOptions([]byte(result.GetRawYamlResult()), &yamlData); err != nil {
237+
return err
238+
}
239+
out, err = toml.Marshal(&yamlData)
240+
} else {
241+
yamlData := &yaml.MapSlice{}
242+
if err := yaml.UnmarshalWithOptions([]byte(result.GetRawYamlResult()), yamlData, yaml.UseOrderedMap()); err != nil {
243+
return err
244+
}
245+
out, err = gen.MarshalTOML(yamlData)
246+
}
247+
if err != nil {
248+
return err
249+
}
250+
output = []byte(string(out) + "\n")
228251
} else {
229252
// Both considering the raw YAML format and the YAML stream format that contains the `---` separator.
230253
output = []byte(result.GetRawYamlResult() + "\n")

pkg/options/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestRunOptions_Validate(t *testing.T) {
136136
if err == nil {
137137
t.Errorf("RunOptions.Validate() did not return an error")
138138
} else {
139-
expectedError := "invalid output format, expected [json yaml], got invalid_format"
139+
expectedError := "invalid output format, expected [json yaml toml], got invalid_format"
140140
if err.Error() != expectedError {
141141
t.Errorf("unexpected error message:\nexpected: %s\ngot: %s", expectedError, err.Error())
142142
}

pkg/version/version.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ func getVersion(version string) string {
3232
}
3333

3434
const (
35-
VersionTypeLatest = Version_0_9_0_beta_1
36-
37-
Version_0_9_0_beta_1 VersionType = "0.9.0-beta.1"
38-
Version_0_8_9 VersionType = "0.8.9"
39-
Version_0_8_8 VersionType = "0.8.8"
40-
Version_0_8_7 VersionType = "0.8.7"
41-
Version_0_8_6 VersionType = "0.8.6"
42-
Version_0_8_5 VersionType = "0.8.5"
43-
Version_0_8_4 VersionType = "0.8.4"
44-
Version_0_8_3 VersionType = "0.8.3"
45-
Version_0_8_2 VersionType = "0.8.2"
46-
Version_0_8_1 VersionType = "0.8.1"
47-
Version_0_8_0 VersionType = "0.8.0"
48-
Version_0_7_5 VersionType = "0.7.5"
49-
Version_0_7_4 VersionType = "0.7.4"
50-
Version_0_7_3 VersionType = "0.7.3"
51-
Version_0_7_2 VersionType = "0.7.2"
52-
Version_0_7_1 VersionType = "0.7.1"
53-
Version_0_7_0 VersionType = "0.7.0"
54-
Version_0_6_0 VersionType = "0.6.0"
35+
VersionTypeLatest = Version_0_9_0_rc_1
36+
37+
Version_0_9_0_rc_1 VersionType = "0.9.0-rc.1"
38+
Version_0_8_9 VersionType = "0.8.9"
39+
Version_0_8_8 VersionType = "0.8.8"
40+
Version_0_8_7 VersionType = "0.8.7"
41+
Version_0_8_6 VersionType = "0.8.6"
42+
Version_0_8_5 VersionType = "0.8.5"
43+
Version_0_8_4 VersionType = "0.8.4"
44+
Version_0_8_3 VersionType = "0.8.3"
45+
Version_0_8_2 VersionType = "0.8.2"
46+
Version_0_8_1 VersionType = "0.8.1"
47+
Version_0_8_0 VersionType = "0.8.0"
48+
Version_0_7_5 VersionType = "0.7.5"
49+
Version_0_7_4 VersionType = "0.7.4"
50+
Version_0_7_3 VersionType = "0.7.3"
51+
Version_0_7_2 VersionType = "0.7.2"
52+
Version_0_7_1 VersionType = "0.7.1"
53+
Version_0_7_0 VersionType = "0.7.0"
54+
Version_0_6_0 VersionType = "0.6.0"
5555
)

0 commit comments

Comments
 (0)