nu_plugin_kcl/README.md

1 line
21 KiB
Markdown
Raw Normal View History

# nu_plugin_kcl\n\nA powerful [Nushell](https://nushell.sh/) plugin for [KCL (Kubernetes Configuration Language)](https://www.kcl-lang.io/) workflows, providing seamless integration between Nushell's data processing capabilities and KCL's configuration management system.\n\n## Overview\n\nThis plugin wraps the KCL CLI to provide native Nushell commands for running, formatting, and validating KCL configurations. Perfect for Infrastructure as Code (IaC), Kubernetes manifests, and configuration management workflows where you need to combine structured data processing with declarative configuration.\n\n## Installing\n\n> [!CAUTION]\n> Require to have [KCL](https://www.kcl-lang.io/) CLI wrapper\n> use [KCL installation documentation](https://www.kcl-lang.io/docs/user_docs/getting-started/install)\n\nClone this repository\n\n> [!WARNING]\n> **nu_plugin_kcl** has dependencies to nushell source via local path in Cargo.toml\n> Nushell and plugins require to be **sync** with same **version**\n\nClone [Nushell](https://nushell.sh/) alongside this plugin or change dependencies in [Cargo.toml](Cargo.toml)\n\nThis plugin is also included as submodule in [nushell-plugins](https://repo.jesusperez.pro/jesus/nushell-plugins)\nas part of plugins collection for [Provisioning project](https://rlung.librecloud.online/jesus/provisioning)\n\nBuild from source\n\n```nushell\n> cd nu_plugin_kcl\n> cargo install --path .\n```\n\n### Nushell\n\nIn a [Nushell](https://nushell.sh/)\n\n```nushell\n> plugin add ~/.cargo/bin/nu_plugin_kcl\n```\n\n## Commands\n\n### `kcl-run`\n\nExecute KCL files and return their output with support for variables, output formats, and settings.\n\n```nushell\n> kcl-run <file> [--format] [--output] [--define] [--setting]\n```\n\n**Parameters:**\n\n- **file** `<path>`: KCL file to execute\n\n**Flags:**\n\n- **--format** `-f` `<string>`: Output format (yaml/json)\n- **--output** `-o` `<path>`: Output file path\n- **--define** `-D` `<string>`: Variables to define (key=value)\n- **--setting** `-Y` `<string>`: Setting files to include\n\n**Example:**\n\n```nushell\n> kcl-run myfile.k -D foo=bar -f json\n{\n "foo": "bar"\n}\n```\n\n### `kcl-format`\n\nFormat KCL files according to standard KCL formatting rules.\n\n```nushell\n> kcl-format <file>\n```\n\n**Parameters:**\n\n- **file** `<path>`: KCL file to format\n\n**Example:**\n\n```nushell\n> kcl-format myfile.k\n✅ File formatted: myfile.k\n```\n\n### `kcl-validate`\n\nValidate all KCL files in a directory for syntax and semantic correctness.\n\n```nushell\n> kcl-validate [directory]\n```\n\n**Parameters:**\n\n- **directory** `<path>`: Directory to validate (defaults to current directory)\n\n**Example:**\n\n```nushell\n> kcl-validate ./project_dir\n✅ All 3 files are valid\n\n✅ ./project_dir/main.k\n✅ ./project_dir/vars.k\n✅ ./project_dir/other.k\n```\n\n## KCL Configuration Language\n\nKCL is a constraint-based record and functional programming language hosted by CNCF. It provides powerful features for configuration management:\n\n### Basic Syntax\n\n```kcl\n# Simple configuration\nname = "my-app"\nversion = "1.0.0"\n\n# Schema definition\nschema Config:\n name: str\n version: str\n replicas: int = 3\n\n# Configuration instance\nconfig: Config = {\n name = name\n version = version\n replicas = 5\n}\n```\n\n### Advanced Features\n\n```kcl\n# Constraints and validation\nschema Service:\n name: str\n port: int\n\n check:\n 1 <= port <= 65535, "port must be between 1 and 65535"\n len(name) > 0, "name cannot be empty"\n\n# Conditional logic\nconfig = {\n env = "production"\n database = {\n host = "prod-db.example.com" if env == "production" else "localhost"\n port = 5432\n ssl = True if env == "production" else False\n }\n}\n\n# List comprehensions and filters\nservices = [\n {name = "web-${i}", port = 8000 + i} for i in range(3)\n] + [\n {name = "worker-${i}", port = 9000 + i} for i in range(2)\n]\n\n# Filtering\nweb_services = [s for s in services if "web" in s.name]\n```\n\n## Usage Examples\