Skip to content

Commit

Permalink
refactor: update README, add kubernetes examples and refine kcl run c…
Browse files Browse the repository at this point in the history
…odes. (#91)
  • Loading branch information
Peefy authored Mar 1, 2023
1 parent e966161 commit 7de90d5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 68 deletions.
107 changes: 65 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
30 changes: 5 additions & 25 deletions cmds/kcl-go/command/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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
},
Expand Down
39 changes: 39 additions & 0 deletions examples/kubernetes/main.go
Original file line number Diff line number Diff line change
@@ -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 }]
}
]
}
`
2 changes: 1 addition & 1 deletion scripts/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7de90d5

Please sign in to comment.