From 295a1b6bef5ee4ff26b6baf71df32fdda6e735df Mon Sep 17 00:00:00 2001 From: Wes McNamee Date: Tue, 2 Jul 2024 22:15:39 -0700 Subject: [PATCH] =?UTF-8?q?feat(native):=20=E2=9C=A8=20=20add=20RunPaths?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit allows users to easily specify multiple files, like how the KCL CLI allows specifying multiple files. ref: https://www.kcl-lang.io/docs/next/reference/lang/tour#multi-file-compilation --- pkg/native/api.go | 13 +++++++++++++ pkg/native/api_test.go | 9 +++++++++ pkg/native/testdata/1.k | 2 ++ pkg/native/testdata/2.k | 1 + 4 files changed, 25 insertions(+) create mode 100644 pkg/native/testdata/1.k create mode 100644 pkg/native/testdata/2.k diff --git a/pkg/native/api.go b/pkg/native/api.go index ff89c917..6f358399 100644 --- a/pkg/native/api.go +++ b/pkg/native/api.go @@ -20,6 +20,19 @@ func Run(path string, opts ...kcl.Option) (*kcl.KCLResultList, error) { return run([]string{path}, opts...) } +func MustRunPaths(paths []string, opts ...kcl.Option) *kcl.KCLResultList { + v, err := RunPaths(paths, opts...) + if err != nil { + panic(err) + } + + return v +} + +func RunPaths(paths []string, opts ...kcl.Option) (*kcl.KCLResultList, error) { + return run(paths, opts...) +} + func run(pathList []string, opts ...kcl.Option) (*kcl.KCLResultList, error) { args, err := kcl.ParseArgs(pathList, opts...) if err != nil { diff --git a/pkg/native/api_test.go b/pkg/native/api_test.go index 41b353aa..aaa7d423 100644 --- a/pkg/native/api_test.go +++ b/pkg/native/api_test.go @@ -26,6 +26,15 @@ func TestNativeRun(t *testing.T) { fmt.Println(yaml) } +func ExampleNativeRunPaths() { + yaml := MustRunPaths([]string{"testdata/1.k", "testdata/2.k"}).GetRawYamlResult() + fmt.Println(yaml) + + // output: + // a: b + // c: d +} + func TestNativeRunWithPlugin(t *testing.T) { plugin.RegisterPlugin(plugin.Plugin{ Name: "my_plugin", diff --git a/pkg/native/testdata/1.k b/pkg/native/testdata/1.k new file mode 100644 index 00000000..5f3ccfdf --- /dev/null +++ b/pkg/native/testdata/1.k @@ -0,0 +1,2 @@ +a = "b" + diff --git a/pkg/native/testdata/2.k b/pkg/native/testdata/2.k new file mode 100644 index 00000000..3d0c5e61 --- /dev/null +++ b/pkg/native/testdata/2.k @@ -0,0 +1 @@ +c = "d"