Skip to content

Commit

Permalink
feat: toml format output
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Jun 11, 2024
1 parent 26f40e6 commit 3cc4966
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.0-beta.1
0.9.0-rc.1
3 changes: 3 additions & 0 deletions cmd/kcl/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ For example, 'kcl run path/to/kcl.k' will run the file named path/to/kcl.k
# Run a single file and output JSON
kcl run path/to/kcl.k --format json
# Run a single file and output TOML
kcl run path/to/kcl.k --format toml
# Run multiple files
kcl run path/to/kcl1.k path/to/kcl2.k
Expand Down
6 changes: 4 additions & 2 deletions 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.19.0
github.com/onsi/gomega v1.33.1
github.com/spf13/cobra v1.8.0
kcl-lang.io/kcl-go v0.9.0-beta.1
kcl-lang.io/kcl-go v0.9.0-rc.1
kcl-lang.io/kcl-openapi v0.6.1
kcl-lang.io/kcl-playground v0.5.1
kcl-lang.io/kpm v0.9.0-rc.1
Expand Down Expand Up @@ -48,10 +48,12 @@ require (
github.com/kubescape/go-git-url v0.0.30 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/sys/mountinfo v0.7.1 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
Expand Down Expand Up @@ -118,7 +120,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/goccy/go-yaml v1.11.3
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubescape/go-git-url v0.0.30 h1:PIbg86ae0ftee/p/Tu/6CA1ju6VoJ51G3sQWNHOm6wg=
github.com/kubescape/go-git-url v0.0.30/go.mod h1:3ddc1HEflms1vMhD9owt/3FBES070UaYTUarcjx8jDk=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down Expand Up @@ -744,6 +746,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down Expand Up @@ -859,6 +863,8 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc h1:DRZwH75/E4a2SOr7+gKZ99OEhmjzBzAhgyTnzo1TepY=
github.com/protocolbuffers/txtpbfmt v0.0.0-20240416193709-1e18ef0a7fdc/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA=
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down Expand Up @@ -1677,8 +1683,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.9.0-beta.1 h1:NfOiktimPlSx/pafqRTvPvPTh8nHtncZCFz0UQfbWiE=
kcl-lang.io/kcl-go v0.9.0-beta.1/go.mod h1:mkAN+NjbmyaVljshuneKtT4PKOZzu2Z+ayX3yWS+j0E=
kcl-lang.io/kcl-go v0.9.0-rc.1 h1:AyS6PDmn1L3BgNVW/r05L+d/5KmuKtEwgJr7n8YQ7p8=
kcl-lang.io/kcl-go v0.9.0-rc.1/go.mod h1:P3tVAITO71STsZ1M62MDj8jE/9nx3XuNFXy6Kr3UTRY=
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
4 changes: 3 additions & 1 deletion pkg/options/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const (
// Json is the JSON output format.
Json string = "json"
// Yaml is the YAML output format.
Yaml string = "yaml"
Yaml string = "yaml"
// Toml is the TOML output format.
Toml string = "toml"
GoStruct string = "gostruct"
Auto string = "auto"
Crd string = "crd"
Expand Down
27 changes: 25 additions & 2 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
"strings"

"github.com/acarl005/stripansi"
"github.com/goccy/go-yaml"
"github.com/pkg/errors"
"kcl-lang.io/cli/pkg/fs"
"kcl-lang.io/kcl-go/pkg/3rdparty/toml"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/tools/gen"
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/git"
Expand Down Expand Up @@ -202,8 +205,8 @@ func (o *RunOptions) Complete(args []string) error {

// Validate validates the options.
func (o *RunOptions) Validate() error {
if o.Format != "" && strings.ToLower(o.Format) != Json && strings.ToLower(o.Format) != Yaml {
return fmt.Errorf("invalid output format, expected %v, got %v", []string{Json, Yaml}, o.Format)
if o.Format != "" && strings.ToLower(o.Format) != Json && strings.ToLower(o.Format) != Yaml && strings.ToLower(o.Format) != Toml {
return fmt.Errorf("invalid output format, expected %v, got %v", []string{Json, Yaml, Toml}, o.Format)
}
for _, setting := range o.Settings {
if _, err := os.Stat(setting); err != nil {
Expand All @@ -225,6 +228,26 @@ func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
return err
}
output = []byte(out.String() + "\n")
} else if strings.ToLower(o.Format) == Toml {
var out []byte
var err error
if o.SortKeys {
yamlData := make(map[string]any)
if err := yaml.UnmarshalWithOptions([]byte(result.GetRawYamlResult()), &yamlData); err != nil {
return err
}
out, err = toml.Marshal(&yamlData)
} else {
yamlData := &yaml.MapSlice{}
if err := yaml.UnmarshalWithOptions([]byte(result.GetRawYamlResult()), yamlData, yaml.UseOrderedMap()); err != nil {
return err
}
out, err = gen.MarshalTOML(yamlData)
}
if err != nil {
return err
}
output = []byte(string(out) + "\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 @@ -136,7 +136,7 @@ func TestRunOptions_Validate(t *testing.T) {
if err == nil {
t.Errorf("RunOptions.Validate() did not return an error")
} else {
expectedError := "invalid output format, expected [json yaml], got invalid_format"
expectedError := "invalid output format, expected [json yaml toml], got invalid_format"
if err.Error() != expectedError {
t.Errorf("unexpected error message:\nexpected: %s\ngot: %s", expectedError, err.Error())
}
Expand Down
40 changes: 20 additions & 20 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ func getVersion(version string) string {
}

const (
VersionTypeLatest = Version_0_9_0_beta_1

Version_0_9_0_beta_1 VersionType = "0.9.0-beta.1"
Version_0_8_9 VersionType = "0.8.9"
Version_0_8_8 VersionType = "0.8.8"
Version_0_8_7 VersionType = "0.8.7"
Version_0_8_6 VersionType = "0.8.6"
Version_0_8_5 VersionType = "0.8.5"
Version_0_8_4 VersionType = "0.8.4"
Version_0_8_3 VersionType = "0.8.3"
Version_0_8_2 VersionType = "0.8.2"
Version_0_8_1 VersionType = "0.8.1"
Version_0_8_0 VersionType = "0.8.0"
Version_0_7_5 VersionType = "0.7.5"
Version_0_7_4 VersionType = "0.7.4"
Version_0_7_3 VersionType = "0.7.3"
Version_0_7_2 VersionType = "0.7.2"
Version_0_7_1 VersionType = "0.7.1"
Version_0_7_0 VersionType = "0.7.0"
Version_0_6_0 VersionType = "0.6.0"
VersionTypeLatest = Version_0_9_0_rc_1

Version_0_9_0_rc_1 VersionType = "0.9.0-rc.1"
Version_0_8_9 VersionType = "0.8.9"
Version_0_8_8 VersionType = "0.8.8"
Version_0_8_7 VersionType = "0.8.7"
Version_0_8_6 VersionType = "0.8.6"
Version_0_8_5 VersionType = "0.8.5"
Version_0_8_4 VersionType = "0.8.4"
Version_0_8_3 VersionType = "0.8.3"
Version_0_8_2 VersionType = "0.8.2"
Version_0_8_1 VersionType = "0.8.1"
Version_0_8_0 VersionType = "0.8.0"
Version_0_7_5 VersionType = "0.7.5"
Version_0_7_4 VersionType = "0.7.4"
Version_0_7_3 VersionType = "0.7.3"
Version_0_7_2 VersionType = "0.7.2"
Version_0_7_1 VersionType = "0.7.1"
Version_0_7_0 VersionType = "0.7.0"
Version_0_6_0 VersionType = "0.6.0"
)

0 comments on commit 3cc4966

Please sign in to comment.