# README
KCL Go SDK
KCL is an open-source, constraint-based record and functional language that enhances the writing of complex configurations, including those for cloud-native scenarios. With its advanced programming language technology and practices, KCL is dedicated to promoting better modularity, scalability, and stability for configurations. It enables simpler logic writing and offers ease of automation APIs and integration with homegrown systems.
What is it for?
You can use KCL to
- Generate low-level static configuration data such as JSON, YAML, etc., or integrate with existing data.
- Reduce boilerplate in configuration data with the schema modeling.
- Define schemas with rule constraints for configuration data and validate them automatically.
- Organize, simplify, unify and manage large configurations without side effects through gradient automation schemes and GitOps.
- Manage large configurations in a scalable way with isolated configuration blocks.
- Mutating or validating Kubernetes resources with cloud-native configuration tool plugins.
- Used as a platform engineering programming language to deliver modern applications with Kusion Stack.
Building & Testing
go test ./...
Run KCL Code with Go SDK
package main
import (
"fmt"
kcl "kcl-lang.io/kcl-go"
)
func main() {
yaml := kcl.MustRun("kubernetes.k", kcl.WithCode(code)).GetRawYamlResult()
fmt.Println(yaml)
}
const 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 }]
}
]
}
`
Run the command:
go run ./examples/kubernetes/main.go
Output:
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
Run KCL Code with Go Plugin
package main
import (
"fmt"
"kcl-lang.io/kcl-go/pkg/kcl"
_ "kcl-lang.io/kcl-go/pkg/plugin/hello_plugin" // Import the hello plugin
)
func main() {
yaml := kcl.MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult()
fmt.Println(yaml)
}
const code = `
import kcl_plugin.hello
name = "kcl"
three = hello.add(1,2) # hello.add is written by Go
`
Note: CGO is required when using plugins
Documents
See the KCL website
License
Apache License Version 2.0
# Functions
FormatCode returns the formatted code.
FormatPath formats files from the given path path: if path is `.` or empty string, all KCL files in current directory will be formatted, not recursively if path is `path/file.k`, the specified KCL file will be formatted if path is `path/to/dir`, all KCL files in the specified dir will be formatted, not recursively if path is `path/to/dir/...`, all KCL files in the specified dir will be formatted recursively
the returned changedPaths are the changed file paths (relative path).
GetSchemaType returns schema types from a kcl file or code.
GetSchemaTypeMapping returns a <schemaName>:<schemaType> mapping of schema types from a kcl file or code.
GetVersion returns the KCL service version information.
LintPath lint files from the given path.
ListDepFiles return the depend files from the given path.
ListDownStreamFiles return a list of downstream depend files from the given changed path list.
ListOptions provides users with the ability to parse kcl program and get all option calling information.
ListUpStreamFiles return a list of upstream depend files from the given path list.
ListVariables provides users with the ability to parse KCL program and get all variables by specs.
LoadPackage provides users with the ability to parse KCL program and semantic model information including symbols, types, definitions, etc.
MustRun is like Run but panics if return any error.
NewOption returns a new Option.
OverrideFile rewrites a file with override spec file: string.
Parse KCL program with entry files and return the AST JSON string.
Run evaluates the KCL program with path and opts, then returns the object list.
RunFiles evaluates the KCL program with multi file path and opts, then returns the object list.
Test calls the test tool to run uni tests in packages.
Download and update dependencies defined in the kcl.mod file and return the external package name and location list.
Validate validates the given data file against the specified schema file with the provided options.
ValidateCode validate data string match code string.
WithCode returns a Option which hold a kcl source code list.
WithDisableNone returns a Option which hold a disable none switch.
WithExternalPkgAndPath returns a Option which hold a external package.
WithExternalPkgs returns a Option which hold a external package list.
WithFullTypePath returns a Option which hold a include full type string in the `_type` attribute.
WithIncludeSchemaTypePath returns a Option which hold a include schema type path switch.
WithKFilenames returns a Option which hold a filenames list.
WithLogger returns a Option which hold a logger.
WithOptions returns a Option which hold a key=value pair list for option function.
WithOverrides returns a Option which hold a override list.
WithPrintOverridesAST returns a Option which hold a printOverridesAST switch.
WithSelectors returns a Option which hold a path selector list.
WithSettings returns a Option which hold a settings file.
WithShowHidden returns a Option which holds a showHidden switch.
WithSortKeys returns a Option which holds a sortKeys switch.
WithWorkDir returns a Option which hold a work dir.
# Constants
KclvmAbiVersion is the current kclvm ABI version.
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author