### Clone KCL Configuration Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/configuration.md Clone the example repository to get started with KCL configuration. ```shell git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/configuration ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/gitops/1-argocd.md Clone the example repository to get started with KCL and ArgoCD GitOps configurations. ```bash git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/gitops ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/3-kustomize-kcl-plugin.md Clone the Kustomize KCL example repository to get started with the provided samples. ```bash git clone https://github.com/kcl-lang/kustomize-kcl.git cd ./kustomize-kcl/examples/set-annotation/ ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/5-helmfile-kcl-plugin.md Clone the example repository to get started with the Helmfile KCL plugin. ```bash git clone https://github.com/kcl-lang/helmfile-kcl.git cd ./helmfile-kcl/examples/hello-world/ ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/2-helm-kcl-plugin.md Clone the repository containing the Helm KCL plugin examples to get started. ```bash git clone https://github.com/kcl-lang/helm-kcl.git/ cd ./helm-kcl/examples/workload-charts-with-kcl ``` -------------------------------- ### Clone KCL Automation Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/automation.md Clone the example repository to get started with KCL automation. ```bash git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/automation ``` -------------------------------- ### Quick Start KCL Execution Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/kotlin-api.md A basic example demonstrating how to execute a KCL file using the Kotlin API. ```kotlin import com.kcl.api.API import com.kcl.api.execProgramArgs val args = execProgramArgs { kFilenameList += "schema.k" } val api = API() val result = api.execProgram(args) ``` -------------------------------- ### Clone KPT KCL SDK Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/4-kpt-kcl-sdk.md Clone the example repository to get started with KPT KCL SDK. ```bash git clone https://github.com/kcl-lang/kpt-kcl-sdk.git/ cd ./kpt-kcl-sdk/get-started/set-annotation ``` -------------------------------- ### Install KCL Python SDK and Get Help Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2022-12-06-kcl-0.4.4-release-blog/index.md Install the KCL Python SDK using pip and access help information. Requires Python 3.7.3+ and pip. ```bash python3 -m pip install kclvm --user && python3 -m kclvm --help ``` -------------------------------- ### Install Dependencies Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/README.md Run this command to install all necessary project dependencies. ```bash npm install ``` -------------------------------- ### Start Minikube Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-11-20-search-k8s-module-on-artifacthub/index.md Starts a minikube cluster with image caching enabled. Ensure minikube is installed and configured. ```shell minikube start --cache-images=true ``` -------------------------------- ### Quick Start: Execute KCL Program Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/java-api.md A basic example demonstrating how to execute a KCL file and print its YAML output using the Java API. ```java import com.kcl.api.API; import com.kcl.api.Spec.ExecProgramArgs; import com.kcl.api.Spec.ExecProgramResult; public class ExecProgramTest { public static void main(String[] args) throws Exception { API api = new API(); ExecProgramResult result = api .execProgram(ExecProgramArgs.newBuilder().addKFilenameList("path/to/kcl.k").build()); System.out.println(result.getYamlResult()); } } ``` -------------------------------- ### Start Development Server Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/README.md Use this command to start a local development server and view the website at http://localhost:3000. ```bash npm run start ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/1-kubectl-kcl-plugin.md Clone the kubectl-kcl repository to access example configurations. ```bash git clone https://github.com/kcl-lang/kubectl-kcl.git/ cd ./kubectl-kcl/examples/ ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/1-adapt-from-kubernetes.md Clone the KCL language documentation repository to access Kubernetes examples. ```shell git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/kubernetes/from-kubernetes ``` -------------------------------- ### Quick Start: Execute KCL Program in Rust Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/rust-api.md A basic example demonstrating how to execute a KCL program using the `kcl_lang` API in Rust. It shows how to set up arguments and print the YAML result. ```rust use kcl_lang::* use anyhow::Result; fn main() -> Result<()> { let api = API::default(); let args = &ExecProgramArgs { k_filename_list: vec!["main.k".to_string()], k_code_list: vec!["a = 1".to_string()], ..Default::default() }; let exec_result = api.exec_program(args)?; println!("{}", exec_result.yaml_result); Ok(()) } ``` -------------------------------- ### Quick Start Executing a KCL Program Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/dotnet-api.md A basic example demonstrating how to execute a KCL file and retrieve the YAML result using the KclLib API. ```typescript using KclLib.API; var api = new API(); var execArgs = new ExecProgramArgs(); var path = Path.Combine("test_data", "schema.k"); execArgs.KFilenameList.Add(path); var result = api.ExecProgram(execArgs); Console.WriteLine(result.YamlResult); ``` -------------------------------- ### Setup KCL Language Server Source: https://context7.com/kcl-lang/kcl-lang.io/llms.txt This is a basic setup for the KCL language server configuration. Ensure the language server is installed and accessible. ```lua require('lspconfig').kcl.setup {} ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/data-integration.md Clone the KCL language repository to access example data integration code. ```bash git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/data-integration ``` -------------------------------- ### Clone Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/secret-management/1-vault.md Clone the example repository to access the KCL configuration and deployment files for secret management. ```shell git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/secret-management/vault ``` -------------------------------- ### Execute KCL Program with Updated Dependencies Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/go-api.md This example demonstrates how to execute a KCL program after updating dependencies. It first calls `UpdateDependencies` to get the external package information and then uses `ExecProgram` to run the KCL file, printing the YAML result. ```go package main import ( "fmt" "log" "kcl-lang.io/kcl-go/pkg/native" "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" ) func main() { // [package] // name = "mod_update" // edition = "0.0.1" // version = "0.0.1" // // [dependencies] // helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" } // flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" } svc := native.NewNativeServiceClient() result, err := svc.UpdateDependencies(&gpyrpc.UpdateDependenciesArgs{ ManifestPath: "testdata/update_dependencies", }) if err != nil { log.Fatal(err) } // import helloworld // import flask // a = helloworld.The_first_kcl_program // fmt.Println(result.ExternalPkgs) execResult, err := svc.ExecProgram(&gpyrpc.ExecProgramArgs{ KFilenameList: []string{"testdata/update_dependencies/main.k"}, ExternalPkgs: result.ExternalPkgs, }) if err != nil { log.Fatal(err) } fmt.Println(execResult.YamlResult) } ``` -------------------------------- ### Install KCL via Scoop (Windows) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Add the KCL bucket to Scoop and install KCL on Windows. ```bash scoop bucket add kcl-lang https://github.com/kcl-lang/scoop-bucket.git scoop install kcl-lang/kcl ``` -------------------------------- ### NeoVim LSP configuration for KCL Source: https://context7.com/kcl-lang/kcl-lang.io/llms.txt Example configuration for integrating the KCL language server with NeoVim using `nvim-lspconfig`. This setup enables features like syntax highlighting and diagnostics. ```lua # NeoVim: add to Mason or use the KCL language server directly # kcl-language-server is automatically started by the KCL NeoVim plugin # Configuration example for nvim-lspconfig: ``` -------------------------------- ### Install KCL OpenAPI Tool Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-07-14-kcl-0.5.0-release/index.md Provides commands for installing the kcl-openapi command-line tool using go install or curl. ```bash # go install go install kcl-lang.io/kcl-openapi@latest # curl install (MacOS & Linux) curl -fsSL https://kcl-lang.io/script/install-kcl-openapi.sh | /bin/bash ``` -------------------------------- ### Verify KCL Installation Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Run the --help command to confirm KCL has been installed correctly. ```bash kcl --help ``` -------------------------------- ### Clone Kustomize KCL Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-04-14-kcl-0.4.6-release/index.md Use this command to clone the repository containing Kustomize KCL examples. ```shell git clone https://github.com/kcl-lang/kustomize-kcl.git &&cd ./kustomize-kcl/examples/set-annotation/ ``` -------------------------------- ### Clone KCL Example Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/schema-definition.md Clone the example repository to access the KCL schema definition files. ```bash git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/definition ``` -------------------------------- ### Install KCL from Go Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Install KCL using the Go command, requiring Go version 1.22 or later. ```bash go install kcl-lang.io/cli/cmd/kcl@latest ``` -------------------------------- ### Install KCL Language Server via Scoop (Windows) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Add the KCL bucket to Scoop and install the KCL Language Server on Windows. ```bash scoop bucket add kcl-lang https://github.com/kcl-lang/scoop-bucket.git scoop install kcl-lang/kcl-lsp ``` -------------------------------- ### Clone kubectl-kcl Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-07-14-kcl-0.5.0-release/index.md Use this command to clone the example repository for the kubectl-kcl plugin. ```shell git clone https://github.com/kcl-lang/kubectl-kcl.git && cd ./kubectl-kcl/examples/ ``` -------------------------------- ### Example Conversion Command Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/tools/cli/openapi/openapi-to-kcl.md This is an example of the `kcl import` command used with a specific OpenAPI spec file and output directory. ```shell kcl import -m openapi -o ~/ -s test_open_api_spec.yaml ``` -------------------------------- ### Install KCL using Powershell Script Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-02-27-kcl-0.4.5-release-blog/index.md Execute a Powershell script to download and install KCL. This is a convenient method for setting up KCL on Windows. ```powershell powershell -Command "iwr -useb https://kcl-lang.io/script/install.ps1 | iex" ``` -------------------------------- ### Install KCL Language Server (Linux) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Use a wget script to install or upgrade the KCL Language Server on Linux. ```bash wget -q https://kcl-lang.io/script/install-kcl-lsp.sh -O - | /bin/bash ``` -------------------------------- ### Install KCL Node.js Package Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/nodejs-api.md Install the kcl-lib package using npm. ```shell npm install kcl-lib ``` -------------------------------- ### Clone Terraform Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-terraform/3-validation.md Clone the KCL language IO repository to access Terraform validation examples. ```bash git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/terraform/validation ``` -------------------------------- ### KCL App Configuration Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/schema-definition.md An example of an application configuration defined using KCL schemas, including relative path imports. ```kcl import .app_module # A relative path import app: app_module.App { domainType = "Standard" containerPort = 80 volumes = [ { mountPath = "/tmp" } ] services = [ { clusterIP = "None" $type = "ClusterIP" } ] } ``` -------------------------------- ### Instantiate Server Configurations Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/kcl-quick-start.md Create a list of server configurations, providing values for the required 'ip' and 'role' attributes for each server. ```kcl servers = [ ServerConfig {ip = "10.0.0.1", role = "frontend"} ServerConfig {ip = "10.0.0.2", role = "backend"} ] ``` -------------------------------- ### Schema with Arguments Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/spec/schema.md Shows how to define a schema that accepts arguments, which are then used within the schema's initialization logic. This example uses a 'separator' argument for constructing the 'fullName'. ```kcl schema Person[separator]: firstName: str = "John" lastName: str fullName: str = firstName + separator + lastName JohnDoe = Person('_') { lastName = "Doe" } ``` -------------------------------- ### Display Example Configuration Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/1-kubectl-kcl-plugin.md View the content of the kcl-run-oci.yaml file, which defines Kubernetes resources and a KCLRun configuration. ```shell cat ./kcl-run-oci.yaml ``` -------------------------------- ### Clone Konfig Repository Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-konfig/3-quick-start.md Clone the Konfig repository to access example configurations and start using KCL. ```shell git clone https://github.com/kcl-lang/konfig.git && cd konfig ``` -------------------------------- ### Initialize Helmfile Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-k8s/3-mutate-manifests/5-helmfile-kcl-plugin.md Initialize the helmfile tool. This may prompt for plugin installation. ```bash helmfile init ``` -------------------------------- ### Simple Express Web Application Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-11-20-search-k8s-module-on-artifacthub/index.md A basic Node.js Express application with a single GET route. This serves as the example application to be containerized. ```javascript const express = require("express"); const app = express(); const port = 8080; // Define a route that responds to GET requests app.get("/", (req, res) => { res.send("Welcome to my web application!"); }); // Start the server app.listen(port, () => { console.log(`Web app listening at http://localhost:${port}`); }); ``` -------------------------------- ### KCL REST API Server with `kcl server` Source: https://context7.com/kcl-lang/kcl-lang.io/llms.txt Start a local REST server using `kcl server` to expose KCL functionality via HTTP POST endpoints. Includes examples for health checks, listing methods, and executing programs. ```shell # Start the KCL REST server (default port 2021) kcl server ``` ```shell # Health check curl -X POST http://127.0.0.1:2021/api:protorpc/BuiltinService.Ping --data '{}' # {"error": "", "result": {}} ``` ```shell # List all available methods curl -X POST http://127.0.0.1:2021/api:protorpc/BuiltinService.ListMethod --data '{}' ``` ```shell # Execute a KCL program (ExecProgram) curl -X POST http://127.0.0.1:2021/api:protorpc/KclService.ExecProgram \ -H "Content-Type: application/json" \ -d '{"k_filename_list": ["main.k"], "work_dir": "./myproject"}' # {"error":"","result":{"json_result":"{\"alice\":{\"age\":18}}","yaml_result":"alice:\n age: 18",...}} ``` ```shell # Validate JSON data against a KCL schema (ValidateCode) curl -X POST http://127.0.0.1:2021/api:protorpc/KclService.ValidateCode \ -H "accept: application/json" \ -d '{ "code": "schema Person:\n key: str\n check:\n \"value\" in key", "data": "{\"key\": \"value\"}" }' # {"error": "", "result": {"success": true}} ``` ```shell # Override a file in place (OverrideFile) curl -X POST http://127.0.0.1:2021/api:protorpc/KclService.OverrideFile \ -H "Content-Type: application/json" \ -d '{"file": "main.k", "specs": ["app.name=\"nginx\"", "app.replicas=3"]}' # {"error": "", "result": {"result": true, "parse_errors": []}} ``` ```shell # Format source code (FormatCode) curl -X POST http://127.0.0.1:2021/api:protorpc/KclService.FormatCode \ -H "Content-Type: application/json" \ -d '{"source": "a = 1+2"}' ``` -------------------------------- ### Go: Run KCL with Hello Plugin Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/plugin/overview.md Demonstrates how to run KCL code that imports and uses a Go-implemented plugin. Ensure the plugin is correctly imported and registered. ```go package main import ( "fmt" "kcl-lang.io/kcl-go/pkg/kcl" _ "kcl-lang.io/kcl-go/pkg/plugin/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 " ``` -------------------------------- ### Install ArgoCD Controllers Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-07-31-kcl-github-argocd-gitops/index.md Installs the ArgoCD controllers into the Kubernetes cluster. Ensure ArgoCD is installed before proceeding. ```bash kubectl create namespace argocd kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml ``` -------------------------------- ### Configuration Definition Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/spec/schema.md Shows how to define a configuration for the 'person' schema, including setting attribute values and utilizing default values. ```kcl johnDoe = person { # In the result, 'lastName' appears later than 'firstName', according the schema lastName = 'Doe' firstName = 'John' # If we don't specify the 'age', the default value 0 is used. # 'age': 20 } ``` -------------------------------- ### Install Crossplane KCL Function Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2024-01-04-biweekly-newsletter/index.md Applies a Crossplane Function object to install the KCL function package. Ensure Crossplane CLI 1.14+ is installed. ```yaml apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: kcl-function spec: package: xpkg.upbound.io/crossplane-contrib/function-kcl:latest ``` -------------------------------- ### Import and Instantiate Server Schema Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/codelab/schema.md Imports a package and instantiates the `Server` schema with specific configurations for name, image, volumes, commands, labels, and service ports. This example shows how to use the defined schema in a practical scenario. ```kcl import pkg server = pkg.Server { name = "my-nginx" image = "nginx:1.14.2" volumes = [pkg.Volume { name = "mydir" mountPath = "/test-pd" hostPath = "/data" }] command = ["nginx"] labels = { run = "my-nginx" env = "pre-prod" } service.ports = [pkg.Port { name = "http" protocol = "TCP" port = 80 targetPort = 9376 }] } ``` -------------------------------- ### Package and Module Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/working-with-konfig/4-best-practice.md Organize reusable functions into modules (e.g., `utils.k`) and import them into other KCL files (`main.k`) for use. ```kcl # utils.k # Define a function that adds two numbers and returns the result。 add = lambda x, y { x + y } # Define a function that subs two numbers and returns the result。 sub = lambda x, y { x - y } ``` -------------------------------- ### Example CRD YAML Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/tools/cli/openapi/crd-to-kcl.md This is an example of a Kubernetes CRD file for a `CronTab` resource. ```yaml # Deprecated in v1.16 in favor of apiextensions.k8s.io/v1 apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: # name must match the spec fields below, and be in the form: . name: crontabs.stable.example.com spec: # group name to use for REST API: /apis// group: stable.example.com # list of versions supported by this CustomResourceDefinition versions: - name: v1 # Each version can be enabled/disabled by Served flag. served: true # One and only one version must be marked as the storage version. storage: true # either Namespaced or Cluster scope: Namespaced names: # plural name to be used in the URL: /apis/// plural: crontabs # singular name to be used as an alias on the CLI and for display singular: crontab # kind is normally the CamelCased singular type. Your resource manifests use this. kind: CronTab # shortNames allow shorter string to match your resource on the CLI shortNames: - ct preserveUnknownFields: false validation: openAPIV3Schema: type: object properties: spec: type: object properties: cronSpec: type: string image: type: string replicas: type: integer ``` -------------------------------- ### Install KCL Python Library Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/python-api.md Install the KCL Python library using pip. ```shell python3 -m pip install kcl-lib ``` -------------------------------- ### Initialize and add dependencies with kpm Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-07-14-kcl-0.5.0-release/index.md Use `kpm init` to create a new KCL package and `kpm add` to include external dependencies. Ensure you are in the package directory before running commands. ```shell kpm init kubernetes_demo && cd kubernetes_demo && kpm add k8s ``` -------------------------------- ### Install Node.js Dependencies Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/wasm-api.md Install the KCL WASM library for use in a Node.js environment. ```shell npm install @kcl-lang/wasm-lib ``` -------------------------------- ### KCL AttributeError Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/error/exception.md Placeholder for AttributeError examples related to illegal attributes in KCL schemas. ```kcl # pkg schema A: field_A: str ``` -------------------------------- ### Write a Simple KCL Program Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/kcl-quick-start.md Create a basic KCL file to define a string attribute. Save this code as `hello.k`. ```kcl hello = "KCL" ``` -------------------------------- ### Create Kubernetes Cluster with K3d Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-12-15-kubevela-integration/index.md Installs K3d and creates a Kubernetes cluster. This is a prerequisite for installing KubeVela. ```shell k3d cluster create ``` -------------------------------- ### Initialize and Add Git Repository with kpm Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-04-14-kcl-0.4.6-release/index.md Use this command to initialize a new KCL project and add a Git repository as a dependency. Specify the repository URL and a tag for version control. ```shell kpm init kubernetes_demo && kpm add -git https://github.com/awesome-kusion/konfig.git -tag v0.0.1 ``` -------------------------------- ### KCL Application Schema Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/gitops/1-argocd.md An example of a KCL configuration defining an application with a container and service ports. ```kcl import .app config = app.App { name = "kcl-guestbook-ui" containers.guestbook = { image = "gcr.io/heptio-images/ks-guestbook-demo:0.2" ports = [{containerPort = 80}] } service.ports = [{ port = 80 }] service.type = "LoadBalancer" } ``` -------------------------------- ### List All Option Information Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/python-api.md Parse a KCL program and get all option information. Ensure the KCL file contains options. ```python import kcl_lib.api as api args = api.ParseProgramArgs(paths=["options.k"]) api = api.API() result = api.list_options(args) assert len(result.options) == 3 assert result.options[0].name == "key1" assert result.options[1].name == "key2" assert result.options[2].name == "metadata-key" ``` -------------------------------- ### Configuration Result Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/spec/schema.md Presents the resulting dictionary structure after applying a configuration to a schema instance. ```kcl { 'firstName': 'John' 'lastName': 'Doe' 'age': 0 } ``` -------------------------------- ### Install KCL Language Server (Windows) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Use PowerShell to install or upgrade the KCL Language Server on Windows. ```powershell powershell -Command "iwr -useb https://www.kcl-lang.io/script/install-kcl-lsp.ps1 | iex" ``` -------------------------------- ### List Construction Examples Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/spec/datatypes.md Demonstrates various ways to construct lists in KCL, including empty lists, lists with elements, and using list comprehensions or the list constructor. ```kcl [] ``` ```kcl [a] ``` ```kcl [a, b, c] ``` ```kcl [x for x in iterable] ``` ```kcl list() ``` ```kcl list(iterable) ``` ```kcl list('abc') ``` ```kcl list([1, 2, 3]) ``` -------------------------------- ### Install KCL CLI Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-12-15-kubevela-integration/index.md Installs the KCL command-line interface using a script. Ensure you have curl and bash available. ```shell curl -fsSL https://kcl-lang.io/script/install-cli.sh | /bin/bash ``` -------------------------------- ### Initialize a New KCL Package with kpm Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-09-15-kcl-0.6.0-release/index.md Create a new KCL package structure using the `kpm init` command. ```shell kpm init mynginx ``` -------------------------------- ### Initialize KCL Project and Add K8s Module Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-11-20-search-k8s-module-on-artifacthub/index.md Use this command to initialize a new KCL project and add the 'k8s' module as a dependency. ```shell kcl mod init my-kubernetes-config && cd my-kubernetes-config && kcl mod add k8s:1.28 ``` -------------------------------- ### Clone KCL Validation Examples Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/validation.md Clone the KCL language repository to access example validation code and data. ```shell git clone https://github.com/kcl-lang/kcl-lang.io.git/ cd ./kcl-lang.io/examples/validation ``` -------------------------------- ### Replace Command Line Arguments with settings.yaml Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/support/faq-cli.md Demonstrates how to consolidate multiple command-line arguments into a `settings.yaml` file for cleaner execution. Use the `-Y` flag to specify the configuration file. ```shell kcl main.k -D key=value -n -r ``` ```shell kcl -Y settings.yaml ``` -------------------------------- ### Basic Pull Command Examples Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/tools/cli/package-management/command-reference/9.pull.md Illustrates basic usage of the `kcl mod pull` command for fetching modules. ```shell # Pull the the module named "k8s" to the local path from the registry kcl mod pull k8s ``` ```shell # Pull the module dependency named "k8s" with the version "1.28" kcl mod pull k8s:1.28 ``` -------------------------------- ### Install KCL Language Server (MacOS) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Use a curl script to install or upgrade the KCL Language Server on MacOS. ```bash curl -fsSL https://kcl-lang.io/script/install-kcl-lsp.sh | /bin/bash ``` -------------------------------- ### Python: Register and Use Hello Plugin Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/plugin/overview.md Example of registering a Python plugin named 'my_plugin' with an 'add' function and then using it within KCL code. Assumes 'test.k' contains the KCL code that imports and calls the plugin. ```kcl import kcl_lib.plugin as plugin import kcl_lib.api as api plugin.register_plugin("my_plugin", {"add": lambda x, y: x + y}) def main(): result = api.API().exec_program( api.ExecProgram_Args(k_filename_list=["test.k"]) ) assert result.yaml_result == "result: 2" main() ``` ```kcl import kcl_plugin.my_plugin result = my_plugin.add(1, 1) ``` -------------------------------- ### Install KCL via Homebrew (MacOS) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Install or specify a version of KCL using the Homebrew package manager on MacOS. ```bash # Install the latest version brew install kcl-lang/tap/kcl # Specify a version brew install kcl-lang/tap/kcl@x.y.z ``` -------------------------------- ### Instantiate Database Configuration with Modified Values Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/kcl-quick-start.md Create a database configuration instance, overriding the default ports with a new list of integers. ```kcl database = DatabaseConfig { ports = [2020, 2021] } ``` -------------------------------- ### Install KCL Latest Release (Windows Script) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Use PowerShell to install or upgrade to the latest KCL version on Windows. ```powershell powershell -Command "iwr -useb https://www.kcl-lang.io/script/install-cli.ps1 | iex" ``` -------------------------------- ### Build and Serve Website Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/README.md Builds the production version of the website and serves it locally on port 8080. This is useful for testing the production build. ```bash npm run build npx http-server ./build ``` -------------------------------- ### Install KclLib Package Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/dotnet-api.md Use the dotnet CLI to add the KclLib package to your project. ```shell dotnet add package KclLib ``` -------------------------------- ### KCL Schema Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/tools/cli/kcl/docgen.md Include examples of schema usage within triple-quoted strings, following a specific reST-like format. ```kcl """ Examples -------- val = Schema { name = "Alice" age = 18 } """ ``` -------------------------------- ### Invalid Configuration Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/lang/codelab/schema.md An example of a configuration that violates the defined schema validation rules, specifically the image name format. ```kcl nginx = Deployment(priority=2) { name = "my-nginx" image = "nginx:1142" # image value is not matching the regex volumes = [Volume { name = "mydir" mountPath = "/test-pd" hostPath = "/data" }] command = ["nginx"] labels.run = "my-nginx" labels.env = "pre-prod" service.ports = [Port { name = "http" protocol = "TCP" port = 80 targetPort = 9376 }] } ``` -------------------------------- ### Install KCL Latest Release (Linux Script) Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/getting-started/install.md Use a wget script to install or upgrade to the latest KCL version on Linux. ```bash wget -q https://kcl-lang.io/script/install-cli.sh -O - | /bin/bash ``` -------------------------------- ### Python: Install KCL SDK Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/plugin/overview.md Command to install the KCL Python SDK using pip. This is a prerequisite for developing Python plugins. ```shell python3 -m pip kcl_lib ``` -------------------------------- ### Java: Register and Use Hello Plugin Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/plugin/overview.md Demonstrates registering a Java plugin named 'my_plugin' with an 'add' method and executing KCL code that utilizes it. The KCL code is defined in 'test.k'. ```java package com.kcl; import com.kcl.api.API; import com.kcl.api.Spec.ExecProgram_Args; import com.kcl.api.Spec.ExecProgram_Result; import java.util.Collections; public class PluginTest { public static void main(String[] mainArgs) throws Exception { API.registerPlugin("my_plugin", Collections.singletonMap("add", (args, kwArgs) -> { return (int) args[0] + (int) args[1]; })); API api = new API(); ExecProgram_Result result = api .execProgram(ExecProgram_Args.newBuilder().addKFilenameList("test.k").build()); System.out.println(result.getYamlResult()); } } ``` ```kcl import kcl_plugin.my_plugin result = my_plugin.add(1, 1) ``` -------------------------------- ### KCL Package Structure Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/package-management/4-how-to/6-push_github_action.md Illustrates the typical file structure for a KCL package within a GitHub repository, including the workflow file and KCL configuration files. ```bash ├── .github │ └── workflows │ └── push.yaml # github action workflow ├── LICENSE ├── README.md ├── kcl.mod # kcl.mod to define your kcl package ├── kcl.mod.lock # kcl.mod.lock generated by the kcl package management tool └── main.k # Your KCL program ``` -------------------------------- ### Install KubeVela Core Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-12-15-kubevela-integration/index.md Installs the core KubeVela components into the Kubernetes cluster. This enables KubeVela's application delivery capabilities. ```shell vela install ``` -------------------------------- ### Install KCL C++ Lib by Cloning Source Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/cpp-api.md Alternatively, clone the KCL C++ Lib source code into your project and link it using CMake. ```shell mkdir third_party cd third_party git clone https://github.com/kcl-lang/lib.git ``` ```shell add_subdirectory(third_party/lib/cpp) ``` ```shell target_link_libraries(your_target kcl-lib-cpp) ``` -------------------------------- ### Install KubeVela CLI Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/blog/2023-12-15-kubevela-integration/index.md Downloads and installs the KubeVela command-line interface using a script. This tool is used to manage KubeVela applications. ```shell curl -fsSl https://kubevela.net/script/install.sh | bash ``` -------------------------------- ### KCL Code Formatting Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/tools/cli/kcl/fmt.md Demonstrates the difference in KCL code style before and after applying the KCL Format tool. ```python import math mixin DeploymentMixin: service:str ="my-service" schema DeploymentBase: name: str image : str schema Deployment[replicas] ( DeploymentBase ) : mixin[DeploymentMixin] replicas : int = replicas command: [str ] labels: {str: str} deploy = Deployment(replicas = 3){} ``` ```python import math mixin DeploymentMixin: service: str = "my-service" schema DeploymentBase: name: str image: str schema Deployment[replicas](DeploymentBase): mixin [DeploymentMixin] replicas: int = replicas command: [str] labels: {str:str} deploy = Deployment(replicas=3) {} ``` -------------------------------- ### Start Vault Server and Set Environment Variables Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/user_docs/guides/secret-management/1-vault.md Start a Vault server in development mode and set the necessary environment variables for Vault address and token. Ensure to replace 'yourtoken' with your actual Vault token. ```shell vault server -dev export VAULT_ADDR='http://127.0.0.1:8200' # Note: Replace with your token export VAULT_TOKEN=yourtoken ``` -------------------------------- ### Logout from Docker IO Registry Example Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/tools/cli/package-management/command-reference/7.logout.md An example of how to log out from the docker.io registry. This command disconnects your current session from the specified registry. ```shell kcl registry logout docker.io ``` -------------------------------- ### Start KCL REST Service Source: https://github.com/kcl-lang/kcl-lang.io/blob/main/versioned_docs/version-0.12/reference/xlang-api/overview.md Initiate the KCL REST API service. This command starts a server that listens for incoming requests. ```shell kcl server ```