diff --git a/README.md b/README.md index 793c1233..17570120 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,35 @@ -## kclvm-go: KCLVM binding for Go +# kclvm-go: KCLVM SDK for Go [![GoDoc](https://godoc.org/github.com/KusionStack/kclvm-go?status.svg)](https://godoc.org/github.com/KusionStack/kclvm-go) [![Coverage Status](https://coveralls.io/repos/github/KusionStack/kclvm-go/badge.svg)](https://coveralls.io/github/KusionStack/kclvm-go) [![license](https://img.shields.io/github/license/KusionStack/kclvm-go.svg)](https://github.com/KusionStack/kclvm-go/blob/master/LICENSE) +## Building + - [Install Go 1.17+](https://go.dev/dl/) -- [Install kclvm](https://kcl-lang.io/docs/user_docs/getting-started/install) (or `go run ./cmds/kcl-go/ setup-kclvm` and add `./_build/{Darwin|Darwin-arm64|Linux}/bin` to `PATH`) +- [Install KCLVM](https://kcl-lang.io/docs/user_docs/getting-started/install) (or `go run ./cmds/kcl-go/ setup-kclvm` and add `./_build/{Darwin|Darwin-arm64|Linux}/bin` to `PATH`) -``` -$ go version -go version go1.17.8 darwin/arm64 -$ which kclvm -$ kclvm -m kclvm --version -kclvm version is 0.4.5; checksum: *** +```bash +$ go run ./cmds/kcl-go +$ go run ./cmds/kcl-go run hello.k +name: kcl +age: 1 +two: 2 +x0: + name: kcl + age: 1 +x1: + name: kcl + age: 101 ``` -## Run Test and hello.k +## Testing -``` -$ go test ./... -$ go run ./cmds/kcl-go -$ go run ./cmds/kcl-go run hello.k +```bash +go test ./... ``` -## Run KCL Code with kclvm-go +## Run KCL Code with KCLVM Go SDK ```go package main @@ -35,46 +41,63 @@ import ( ) func main() { - yaml := kclvm.MustRun("hello.k", kclvm.WithCode(k_code)).GetRawYamlResult() + yaml := kclvm.MustRun("kubernetes.k", kclvm.WithCode(k_code)).GetRawYamlResult() fmt.Println(yaml) } const k_code = ` -import kcl_plugin.hello - -name = "kcl" -age = 1 -two = hello.add(1, 1) - -schema Person: - name: str = "kcl" - age: int = 1 - -x0 = Person {} -x1 = Person { - age = 101 +apiVersion = "apps/v1" +kind = "Deployment" +metadata = { + name = "nginx" + labels.app = "nginx" +} +spec = { + replicas = 3 + selector.matchLabels = metadata.labels + template.metadata.labels = metadata.labels + template.spec.containers = [ + { + name = metadata.name + image = "${metadata.name}:1.14.2" + ports = [{ containerPort = 80 }] + } + ] } ` ``` -Output: +Run the command: -``` -$ go run ./examples/hello/main.go -age: 1 -name: kcl -two: 2 -x0: - age: 1 - name: kcl -x1: - age: 101 - name: kcl +```bash +go run ./examples/kubernetes/main.go ``` -## Develop Guide +Output: -- [docs/readme.md](docs/readme.md) +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 +``` ## License diff --git a/cmds/kcl-go/command/cmd_run.go b/cmds/kcl-go/command/cmd_run.go index feaa1917..9008cf88 100644 --- a/cmds/kcl-go/command/cmd_run.go +++ b/cmds/kcl-go/command/cmd_run.go @@ -47,10 +47,6 @@ var runRunFlags = []cli.Flag{ Name: "sort-keys", Usage: "Sort result keys", }, - &cli.BoolFlag{ - Name: "use-raw-result", - Usage: "Use kclvm raw result", - }, &cli.BoolFlag{ Name: "strict-range-check", Aliases: []string{"r"}, @@ -126,30 +122,14 @@ func NewRunCmd() *cli.Command { switch strings.ToLower(c.String("output-type")) { case "json": - if c.Bool("use-raw-result") { - fmt.Println(result.GetRawJsonResult()) - } else { - fmt.Println(result.First().JSONString()) - } + fmt.Println(result.GetRawJsonResult()) case "yaml": - { - if c.Bool("use-raw-result") { - fmt.Println(result.GetRawYamlResult()) - } else { - var resultStringList []string - for _, r := range result.Slice() { - resultStringList = append(resultStringList, r.YAMLString()) - } - fmt.Println(strings.Join(resultStringList, "\n---\n")) - } - } + fmt.Println(result.GetRawYamlResult()) + default: - if c.Bool("use-raw-result") { - fmt.Println(result.GetRawYamlResult()) - } else { - fmt.Println(result.First().YAMLString()) - } + fmt.Println(result.GetRawYamlResult()) + } return nil }, diff --git a/examples/kubernetes/main.go b/examples/kubernetes/main.go new file mode 100644 index 00000000..ea0c9668 --- /dev/null +++ b/examples/kubernetes/main.go @@ -0,0 +1,39 @@ +// Copyright 2022 The KCL Authors. All rights reserved. + +// Run CGO Mode: +// KCLVM_SERVICE_CLIENT_HANDLER=native KCLVM_PLUGIN_DEBUG=1 go run -tags=kclvm_service_capi . + +package main + +import ( + "fmt" + + "kusionstack.io/kclvm-go" + _ "kusionstack.io/kclvm-go/pkg/kcl_plugin/hello_plugin" +) + +func main() { + yaml := kclvm.MustRun("kubernetes.k", kclvm.WithCode(k_code)).GetRawYamlResult() + fmt.Println(yaml) +} + +const k_code = ` +apiVersion = "apps/v1" +kind = "Deployment" +metadata = { + name = "nginx" + labels.app = "nginx" +} +spec = { + replicas = 3 + selector.matchLabels = metadata.labels + template.metadata.labels = metadata.labels + template.spec.containers = [ + { + name = metadata.name + image = "${metadata.name}:1.14.2" + ports = [{ containerPort = 80 }] + } + ] +} +` diff --git a/scripts/kclvm.go b/scripts/kclvm.go index 06bc790b..0d435838 100644 --- a/scripts/kclvm.go +++ b/scripts/kclvm.go @@ -23,7 +23,7 @@ const ( ) const ( - KclvmAbiVersion KclvmVersionType = "v0.4.4" + KclvmAbiVersion KclvmVersionType = KclvmVersionType_v0_4_5 KclvmVersionType_latest = KclvmVersionType_v0_4_5 KclvmVersionType_v0_4_5 KclvmVersionType = "v0.4.5"