diff --git a/VERSION b/VERSION index dc83924..80797a4 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.0-beta.1 \ No newline at end of file +0.9.0-rc.1 \ No newline at end of file diff --git a/cmd/kcl/commands/run.go b/cmd/kcl/commands/run.go index 223d43b..78fa730 100644 --- a/cmd/kcl/commands/run.go +++ b/cmd/kcl/commands/run.go @@ -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 diff --git a/go.mod b/go.mod index 6ced563..843a360 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/go.sum b/go.sum index df0d424..2d05a91 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/pkg/options/constants.go b/pkg/options/constants.go index 7e291e1..5d62ded 100644 --- a/pkg/options/constants.go +++ b/pkg/options/constants.go @@ -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" diff --git a/pkg/options/run.go b/pkg/options/run.go index da5135d..20031a2 100644 --- a/pkg/options/run.go +++ b/pkg/options/run.go @@ -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" @@ -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 { @@ -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") diff --git a/pkg/options/run_test.go b/pkg/options/run_test.go index a0fe1cd..2028ab4 100644 --- a/pkg/options/run_test.go +++ b/pkg/options/run_test.go @@ -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()) } diff --git a/pkg/version/version.go b/pkg/version/version.go index d3dc692..4539484 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -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" )