Skip to content

Commit

Permalink
feat(native): ✨ add RunPaths
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ghostsquad committed Jul 3, 2024
1 parent 6d5dd10 commit 295a1b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/native/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/native/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions pkg/native/testdata/1.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = "b"

1 change: 1 addition & 0 deletions pkg/native/testdata/2.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c = "d"

0 comments on commit 295a1b6

Please sign in to comment.