chore: update code to latest dependencies and Nushell

This commit is contained in:
Jesús Pérez 2025-09-20 15:48:32 +01:00
parent 0287045638
commit 1e6df05931
4 changed files with 933 additions and 176 deletions

273
Cargo.lock generated
View File

@ -131,6 +131,15 @@ dependencies = [
"alloc-stdlib", "alloc-stdlib",
] ]
[[package]]
name = "buf-trait"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21eaafc770e8c073d6c3facafe7617e774305d4954aa6351b9c452eb37ee17b4"
dependencies = [
"zerocopy",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.19.0" version = "3.19.0"
@ -151,9 +160,27 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
[[package]] [[package]]
name = "bytesize" name = "bytesize"
version = "1.3.3" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e93abca9e28e0a1b9877922aacb20576e05d4679ffa78c3d6dc22a26a216659" checksum = "f5c434ae3cf0089ca203e9019ebe529c47ff45cefe8af7c85ecb734ef541822f"
[[package]]
name = "byteyarn"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b93e51d26468a15ea59f8525e0c13dc405db43e644a0b1e6d44346c72cf4cf7b"
dependencies = [
"buf-trait",
]
[[package]]
name = "castaway"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a"
dependencies = [
"rustversion",
]
[[package]] [[package]]
name = "cc" name = "cc"
@ -254,31 +281,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]] [[package]]
name = "crossterm" name = "crossterm"
version = "0.28.1" version = "0.28.1"
@ -374,9 +376,9 @@ dependencies = [
[[package]] [[package]]
name = "fancy-regex" name = "fancy-regex"
version = "0.14.0" version = "0.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e24cb5a94bcae1e5408b0effca5cd7172ea3c5755049c5f3af4cd283a165298" checksum = "998b056554fbe42e03ae0e152895cd1a7e1002aec800fdc6635d20270260c46f"
dependencies = [ dependencies = [
"bit-set", "bit-set",
"regex-automata", "regex-automata",
@ -483,9 +485,9 @@ dependencies = [
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.9.0" version = "2.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown",
@ -559,6 +561,18 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "lean_string"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "962df00ba70ac8d5ca5c064e17e5c3d090c087fd8d21aa45096c716b169da514"
dependencies = [
"castaway",
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.174" version = "0.2.174"
@ -755,7 +769,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-cmd-base" name = "nu-cmd-base"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"miette", "miette",
@ -767,11 +781,12 @@ dependencies = [
[[package]] [[package]]
name = "nu-cmd-lang" name = "nu-cmd-lang"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"itertools 0.14.0", "itertools 0.14.0",
"nu-cmd-base", "nu-cmd-base",
"nu-engine", "nu-engine",
"nu-experimental",
"nu-parser", "nu-parser",
"nu-protocol", "nu-protocol",
"nu-utils", "nu-utils",
@ -780,7 +795,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-derive-value" name = "nu-derive-value"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error2", "proc-macro-error2",
@ -791,22 +806,32 @@ dependencies = [
[[package]] [[package]]
name = "nu-engine" name = "nu-engine"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"fancy-regex",
"log", "log",
"nu-experimental",
"nu-glob", "nu-glob",
"nu-path", "nu-path",
"nu-protocol", "nu-protocol",
"nu-utils", "nu-utils",
] ]
[[package]]
name = "nu-experimental"
version = "0.107.1"
dependencies = [
"itertools 0.14.0",
"thiserror 2.0.12",
]
[[package]] [[package]]
name = "nu-glob" name = "nu-glob"
version = "0.105.2" version = "0.107.1"
[[package]] [[package]]
name = "nu-parser" name = "nu-parser"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"bytesize", "bytesize",
"chrono", "chrono",
@ -822,7 +847,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-path" name = "nu-path"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"dirs", "dirs",
"omnipath", "omnipath",
@ -832,7 +857,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-plugin" name = "nu-plugin"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"log", "log",
"nix", "nix",
@ -846,7 +871,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-plugin-core" name = "nu-plugin-core"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"interprocess", "interprocess",
"log", "log",
@ -860,7 +885,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-plugin-engine" name = "nu-plugin-engine"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"log", "log",
"nu-engine", "nu-engine",
@ -875,7 +900,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-plugin-protocol" name = "nu-plugin-protocol"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"nu-protocol", "nu-protocol",
"nu-utils", "nu-utils",
@ -887,7 +912,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-plugin-test-support" name = "nu-plugin-test-support"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"nu-ansi-term", "nu-ansi-term",
"nu-cmd-lang", "nu-cmd-lang",
@ -903,7 +928,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-protocol" name = "nu-protocol"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"brotli", "brotli",
"bytes", "bytes",
@ -920,6 +945,7 @@ dependencies = [
"miette", "miette",
"nix", "nix",
"nu-derive-value", "nu-derive-value",
"nu-experimental",
"nu-glob", "nu-glob",
"nu-path", "nu-path",
"nu-system", "nu-system",
@ -940,7 +966,7 @@ dependencies = [
[[package]] [[package]]
name = "nu-system" name = "nu-system"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"chrono", "chrono",
"itertools 0.14.0", "itertools 0.14.0",
@ -958,13 +984,16 @@ dependencies = [
[[package]] [[package]]
name = "nu-utils" name = "nu-utils"
version = "0.105.2" version = "0.107.1"
dependencies = [ dependencies = [
"byteyarn",
"crossterm", "crossterm",
"crossterm_winapi", "crossterm_winapi",
"fancy-regex", "fancy-regex",
"lean_string",
"log", "log",
"lscolors", "lscolors",
"memchr",
"nix", "nix",
"num-format", "num-format",
"serde", "serde",
@ -1019,6 +1048,25 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "objc2-core-foundation"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c10c2894a6fed806ade6027bcd50662746363a9589d3ec9d9bef30a4e4bc166"
dependencies = [
"bitflags",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71c1c64d6120e51cd86033f67176b1cb66780c2efe34dec55176f77befd93c0a"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]] [[package]]
name = "omnipath" name = "omnipath"
version = "0.1.6" version = "0.1.6"
@ -1175,26 +1223,6 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "recvmsg" name = "recvmsg"
version = "1.0.0" version = "1.0.0"
@ -1381,9 +1409,9 @@ dependencies = [
[[package]] [[package]]
name = "shadow-rs" name = "shadow-rs"
version = "1.2.0" version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6fd27df794ced2ef39872879c93a9f87c012607318af8621cd56d2c3a8b3a2" checksum = "b8aa5c0570cd9654158bd39f0f8caba24edbc058313946e89f4648b1de1ecf49"
dependencies = [ dependencies = [
"const_format", "const_format",
"is_debug", "is_debug",
@ -1510,16 +1538,16 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.33.1" version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fc858248ea01b66f19d8e8a6d55f41deaf91e9d495246fd01368d99935c6c01" checksum = "252800745060e7b9ffb7b2badbd8b31cfa4aa2e61af879d0a3bf2a317c20217d"
dependencies = [ dependencies = [
"core-foundation-sys",
"libc", "libc",
"memchr", "memchr",
"ntapi", "ntapi",
"rayon", "objc2-core-foundation",
"windows 0.57.0", "objc2-io-kit",
"windows 0.61.3",
] ]
[[package]] [[package]]
@ -1852,12 +1880,24 @@ dependencies = [
[[package]] [[package]]
name = "windows" name = "windows"
version = "0.57.0" version = "0.61.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893"
dependencies = [ dependencies = [
"windows-core 0.57.0", "windows-collections",
"windows-targets 0.52.6", "windows-core 0.61.2",
"windows-future",
"windows-link",
"windows-numerics",
]
[[package]]
name = "windows-collections"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8"
dependencies = [
"windows-core 0.61.2",
] ]
[[package]] [[package]]
@ -1872,18 +1912,6 @@ dependencies = [
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
[[package]]
name = "windows-core"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"
dependencies = [
"windows-implement 0.57.0",
"windows-interface 0.57.0",
"windows-result 0.1.2",
"windows-targets 0.52.6",
]
[[package]] [[package]]
name = "windows-core" name = "windows-core"
version = "0.61.2" version = "0.61.2"
@ -1898,21 +1926,21 @@ dependencies = [
] ]
[[package]] [[package]]
name = "windows-implement" name = "windows-future"
version = "0.56.0" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b" checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e"
dependencies = [ dependencies = [
"proc-macro2", "windows-core 0.61.2",
"quote", "windows-link",
"syn", "windows-threading",
] ]
[[package]] [[package]]
name = "windows-implement" name = "windows-implement"
version = "0.57.0" version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1941,17 +1969,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "windows-interface"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "windows-interface" name = "windows-interface"
version = "0.59.1" version = "0.59.1"
@ -1969,6 +1986,16 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a"
[[package]]
name = "windows-numerics"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1"
dependencies = [
"windows-core 0.61.2",
"windows-link",
]
[[package]] [[package]]
name = "windows-result" name = "windows-result"
version = "0.1.2" version = "0.1.2"
@ -2079,6 +2106,15 @@ dependencies = [
"windows_x86_64_msvc 0.53.0", "windows_x86_64_msvc 0.53.0",
] ]
[[package]]
name = "windows-threading"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6"
dependencies = [
"windows-link",
]
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.48.5" version = "0.48.5"
@ -2225,3 +2261,24 @@ checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"byteorder",
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@ -10,14 +10,14 @@ license = "MIT"
[dependencies] [dependencies]
# for local development, you can use a path dependency # for local development, you can use a path dependency
nu-plugin = { path = "../nushell/crates/nu-plugin" } nu-plugin = { version = "0.107.1", path = "../nushell/crates/nu-plugin" }
nu-protocol = { path = "../nushell/crates/nu-protocol", features = ["plugin"] } nu-protocol = { version = "0.107.1", path = "../nushell/crates/nu-protocol", features = ["plugin"] }
#nu-plugin = "0.104.0" #nu-plugin = "0.104.0"
#nu-protocol = { version = "0.104.0", features = ["plugin"] } #nu-protocol = { version = "0.107.1", version = "0.107.1", features = ["plugin"] }
anyhow = "1.0" anyhow = "1.0"
tempfile = "3" tempfile = "3"
[dev-dependencies] [dev-dependencies]
nu-plugin-test-support = { path = "../nushell/crates/nu-plugin-test-support" } nu-plugin-test-support = { version = "0.107.1", path = "../nushell/crates/nu-plugin-test-support" }
#nu-plugin-test-support = { version = "0.104.0" } #nu-plugin-test-support = { version = "0.107.1", version = "0.107.1" }

820
README.md
View File

@ -1,34 +1,36 @@
# nu_plugin_kcl # nu_plugin_kcl
A [Nushell](https://nushell.sh/) plugin to use with [KCL](https://www.kcl-lang.io/) CLI wrapper A 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.
## Overview
This 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.
## Installing ## Installing
> [!CAUTION] > [!CAUTION]
> Require to have [KCL](https://www.kcl-lang.io/) CLI wrapper > Require to have [KCL](https://www.kcl-lang.io/) CLI wrapper
> use [KLC installation documentation](https://www.kcl-lang.io/docs/user_docs/getting-started/install) > use [KCL installation documentation](https://www.kcl-lang.io/docs/user_docs/getting-started/install)
Clone this repository Clone this repository
> [!WARNING] > [!WARNING]
> **nu_plugin_kcl** has dependencies to nushell source via local path in Cargo.toml > **nu_plugin_kcl** has dependencies to nushell source via local path in Cargo.toml
> Nushell and plugins require to be **sync** with same **version** > Nushell and plugins require to be **sync** with same **version**
Clone [Nushell](https://nushell.sh/) to plugin to use [Tera templates](https://keats.github.io/tera/docs/) or change dependecies in [Cargo.toml](Cargo.toml) Clone [Nushell](https://nushell.sh/) alongside this plugin or change dependencies in [Cargo.toml](Cargo.toml)
This plugin is also included as submodule in [nushell-plugins](https://repo.jesusperez.pro/jesus/nushell-plugins) This plugin is also included as submodule in [nushell-plugins](https://repo.jesusperez.pro/jesus/nushell-plugins)
as part of plugins collection for [Provisioning project](https://rlung.librecloud.online/jesus/provisioning) as part of plugins collection for [Provisioning project](https://rlung.librecloud.online/jesus/provisioning)
Is used in Build from source
Build from source
```nushell ```nushell
> cd nu_plugin_tcl > cd nu_plugin_kcl
> cargo install --path . > cargo install --path .
``` ```
### Nushell ### Nushell
In a [Nushell](https://nushell.sh/) In a [Nushell](https://nushell.sh/)
@ -36,70 +38,768 @@ In a [Nushell](https://nushell.sh/)
> plugin add ~/.cargo/bin/nu_plugin_kcl > plugin add ~/.cargo/bin/nu_plugin_kcl
``` ```
## Exec KCL files ## Commands
Exec [KCL files](https://www.kcl-lang.io/docs/user_docs/getting-started/kcl-quick-start) and return result in [YAML](https://en.wikipedia.org/wiki/YAML) ### `kcl-run`
Execute KCL files and return their output with support for variables, output formats, and settings.
```nushell ```nushell
> kcl-exec <file> (work_dir) > kcl-run <file> [--format] [--output] [--define] [--setting]
``` ```
Flags: **Parameters:**
- **-h**, **--help**: Display the help message for this command - **file** `<path>`: KCL file to execute
Parameters: **Flags:**
- file <path>: KCL file to execute - **--format** `-f` `<string>`: Output format (yaml/json)
- work_dir <directory>: Work directory (optional) - **--output** `-o` `<path>`: Output file path
- **--define** `-D` `<string>`: Variables to define (key=value)
- **--setting** `-Y` `<string>`: Setting files to include
### Examples: **Example:**
Execute the KCL file './src/myfile.k'
```nushell ```nushell
> kcl-exec ./src/myfile.k > kcl-run myfile.k -D foo=bar -f json
{
"foo": "bar"
}
``` ```
## Validate KCL files ### `kcl-format`
Validate [KCL files](https://www.kcl-lang.io/docs/user_docs/getting-started/kcl-quick-start) Format KCL files according to standard KCL formatting rules.
```nushell ```nushell
> kcl-validate (dir) > kcl-format <file>
``` ```
Flags: **Parameters:**
-**h**, **--help**: Display the help message for this command - **file** `<path>`: KCL file to format
Parameters: **Example:**
- dir <directory>: Directory to validate (optional)
### Examples
Validate all KCL files in the directory './project_dir'.
```nushell
> kcl-validate ./project_dir
✅ All 3 files are valid
✅ ./project_dir/main.k
```
## Format KCL files
Format [KCL files](https://www.kcl-lang.io/docs/user_docs/getting-started/kcl-quick-start)
```nushell
> kcl-format (dir)
```
Flags:
- **-h**, **--help**: Display the help message for this command
Parameters:
- file <path>: KCL file to format
### Examples
Format the KCL file 'myfile.k'.
```nushell ```nushell
> kcl-format myfile.k > kcl-format myfile.k
✅ File formatted: myfile.k ✅ File formatted: myfile.k
``` ```
### `kcl-validate`
Validate all KCL files in a directory for syntax and semantic correctness.
```nushell
> kcl-validate [directory]
```
**Parameters:**
- **directory** `<path>`: Directory to validate (defaults to current directory)
**Example:**
```nushell
> kcl-validate ./project_dir
✅ All 3 files are valid
✅ ./project_dir/main.k
✅ ./project_dir/vars.k
✅ ./project_dir/other.k
```
## KCL Configuration Language
KCL is a constraint-based record and functional programming language hosted by CNCF. It provides powerful features for configuration management:
### Basic Syntax
```kcl
# Simple configuration
name = "my-app"
version = "1.0.0"
# Schema definition
schema Config:
name: str
version: str
replicas: int = 3
# Configuration instance
config: Config = {
name = name
version = version
replicas = 5
}
```
### Advanced Features
```kcl
# Constraints and validation
schema Service:
name: str
port: int
check:
1 <= port <= 65535, "port must be between 1 and 65535"
len(name) > 0, "name cannot be empty"
# Conditional logic
config = {
env = "production"
database = {
host = "prod-db.example.com" if env == "production" else "localhost"
port = 5432
ssl = True if env == "production" else False
}
}
# List comprehensions and filters
services = [
{name = "web-${i}", port = 8000 + i} for i in range(3)
] + [
{name = "worker-${i}", port = 9000 + i} for i in range(2)
]
# Filtering
web_services = [s for s in services if "web" in s.name]
```
## Usage Examples
### Basic Configuration Management
**app-config.k**
```kcl
# Application configuration schema
schema AppConfig:
name: str
version: str
environment: str
database: DatabaseConfig
server: ServerConfig
schema DatabaseConfig:
host: str
port: int = 5432
name: str
ssl: bool = True
schema ServerConfig:
host: str = "0.0.0.0"
port: int = 8080
workers: int = 4
# Configuration instance
config: AppConfig = {
name = "my-microservice"
version = "1.2.3"
environment = "production"
database = {
host = "db.example.com"
name = "myapp_prod"
ssl = True
}
server = {
port = 8080
workers = 8
}
}
```
**Usage:**
```nushell
> kcl-run app-config.k -f yaml
name: my-microservice
version: 1.2.3
environment: production
database:
host: db.example.com
port: 5432
name: myapp_prod
ssl: true
server:
host: 0.0.0.0
port: 8080
workers: 8
```
### Dynamic Configuration with Variables
**kubernetes-deployment.k**
```kcl
# Kubernetes deployment template
schema Deployment:
apiVersion: str = "apps/v1"
kind: str = "Deployment"
metadata: {
name: str
namespace?: str
}
spec: {
replicas: int
selector: {
matchLabels: {str:}
}
template: {
metadata: {
labels: {str:}
}
spec: {
containers: [Container]
}
}
}
schema Container:
name: str
image: str
ports?: [{
containerPort: int
protocol?: str = "TCP"
}]
env?: [{
name: str
value: str
}]
# Variables with defaults
app_name = option("app_name") or "my-app"
app_version = option("app_version") or "latest"
replicas = option("replicas") or 3
namespace = option("namespace") or "default"
# Generate deployment
deployment: Deployment = {
metadata = {
name = app_name
namespace = namespace
}
spec = {
replicas = replicas
selector.matchLabels = {"app": app_name}
template = {
metadata.labels = {"app": app_name}
spec.containers = [{
name = app_name
image = "${app_name}:${app_version}"
ports = [{containerPort = 8080}]
env = [
{name = "APP_NAME", value = app_name}
{name = "APP_VERSION", value = app_version}
]
}]
}
}
}
```
**Usage:**
```nushell
# Basic deployment
> kcl-run kubernetes-deployment.k -D app_name=web-service -D app_version=v1.2.3 -f yaml
# Multiple environment deployment
> ["dev", "staging", "prod"] | each { |env|
kcl-run kubernetes-deployment.k -D app_name=web-service -D app_version=v1.2.3 -D namespace=$env -f yaml
| save $"manifests/($env)/deployment.yaml"
}
```
### Multi-Environment Configuration
**base-config.k**
```kcl
# Base configuration shared across environments
schema BaseConfig:
app: {
name: str
version: str
}
features: {
auth: bool = True
metrics: bool = True
logging: bool = True
}
base: BaseConfig = {
app = {
name = "my-service"
version = "1.0.0"
}
features = {
auth = True
metrics = True
logging = True
}
}
```
**environments/dev.k**
```kcl
import base
# Development environment overrides
config = base.base | {
environment = "development"
database = {
host = "localhost"
port = 5432
name = "myapp_dev"
ssl = False
}
server = {
host = "localhost"
port = 3000
debug = True
}
features = base.base.features | {
auth = False # Disable auth in dev
}
}
```
**environments/prod.k**
```kcl
import base
# Production environment configuration
config = base.base | {
environment = "production"
database = {
host = "prod-db.example.com"
port = 5432
name = "myapp_prod"
ssl = True
pool_size = 20
}
server = {
host = "0.0.0.0"
port = 8080
workers = 16
debug = False
}
security = {
rate_limit = 1000
cors_origins = ["https://myapp.com"]
}
}
```
**Usage:**
```nushell
# Generate configurations for all environments
> ["dev", "staging", "prod"] | each { |env|
let config = (kcl-run $"environments/($env).k" -f json | from json)
$config | to yaml | save $"configs/($env).yaml"
print $"Generated config for ($env)"
}
```
## Workflow Examples
### Infrastructure as Code Pipeline
```nushell
# Complete IaC workflow with KCL
def deploy-infrastructure [environment: string] {
print $"🚀 Deploying infrastructure for ($environment)"
# 1. Validate all KCL files
print "📋 Validating KCL configurations..."
let validation = (kcl-validate ./infrastructure)
print $validation
# 2. Generate environment-specific configs
print $"⚙️ Generating ($environment) configuration..."
let config = (kcl-run $"infrastructure/($environment).k" -f yaml)
$config | save $"output/($environment)/config.yaml"
# 3. Generate Kubernetes manifests
print "🎯 Generating Kubernetes manifests..."
ls infrastructure/kubernetes/*.k
| each { |manifest|
let name = ($manifest.name | path basename | str replace '.k' '')
kcl-run $manifest.name -D environment=$environment -f yaml
| save $"output/($environment)/k8s/($name).yaml"
}
# 4. Validate generated YAML
print "✅ Validating generated manifests..."
ls $"output/($environment)/k8s/*.yaml"
| each { |file|
# You could add kubectl validation here
print $"Validated: ($file.name)"
}
print $"✨ Infrastructure deployment for ($environment) complete!"
}
# Usage
> deploy-infrastructure "staging"
```
### Configuration Validation and Testing
```nushell
# Comprehensive validation workflow
def validate-all-configs [] {
print "🔍 Starting comprehensive validation..."
# 1. Validate KCL syntax
print "📝 Validating KCL syntax..."
let kcl_validation = (kcl-validate .)
print $kcl_validation
# 2. Test all environment configurations
print "🌍 Testing environment configurations..."
let environments = ["dev", "staging", "prod"]
let results = ($environments | each { |env|
try {
let config = (kcl-run $"environments/($env).k" -f json | from json)
{
environment: $env
status: "✅ valid"
config_keys: ($config | columns | length)
}
} catch { |err|
{
environment: $env
status: "❌ invalid"
error: $err.msg
}
}
})
$results | table
# 3. Check for required configuration keys
print "🔑 Checking required configuration keys..."
let required_keys = ["app", "database", "server"]
$environments | each { |env|
let config = (kcl-run $"environments/($env).k" -f json | from json)
let missing = ($required_keys | where $it not-in ($config | columns))
if ($missing | length) > 0 {
print $"⚠️ ($env): Missing keys: ($missing | str join ', ')"
} else {
print $"✅ ($env): All required keys present"
}
}
}
# Schema validation helper
def validate-schema [config_file: string, schema_file: string] {
try {
kcl-run $config_file -Y $schema_file -f json | from json
print $"✅ ($config_file) validates against schema"
} catch { |err|
print $"❌ ($config_file) schema validation failed: ($err.msg)"
}
}
```
### Continuous Integration Integration
```nushell
# CI/CD pipeline integration
def ci-kcl-pipeline [] {
print "🏗️ Starting KCL CI pipeline..."
# 1. Format check
print "📐 Checking code formatting..."
let unformatted = (ls **/*.k | each { |file|
let original = (open $file.name)
kcl-format $file.name
let formatted = (open $file.name)
if $original != $formatted {
$file.name
}
} | compact)
if ($unformatted | length) > 0 {
print $"❌ Unformatted files found: ($unformatted | str join ', ')"
exit 1
}
print "✅ All files properly formatted"
# 2. Validation
print "🔍 Validating configurations..."
let validation = (kcl-validate .)
if "❌" in $validation {
print "❌ Validation failed"
print $validation
exit 1
}
print "✅ All configurations valid"
# 3. Generate and test configurations
print "⚙️ Testing configuration generation..."
["dev", "staging", "prod"] | each { |env|
try {
kcl-run $"environments/($env).k" -f json | from json | ignore
print $"✅ ($env) configuration generates successfully"
} catch { |err|
print $"❌ ($env) configuration failed: ($err.msg)"
exit 1
}
}
print "🎉 KCL CI pipeline completed successfully!"
}
```
### Configuration Management and Templating
```nushell
# Generate application configurations from templates
def generate-app-config [app_name: string, version: string, environment: string] {
let template = "templates/app-template.k"
let settings = $"settings/($environment).yaml"
kcl-run $template -D app_name=$app_name -D version=$version -Y $settings -f yaml
| save $"configs/($app_name)-($environment).yaml"
print $"Generated config for ($app_name) v($version) in ($environment)"
}
# Bulk configuration generation
def generate-all-configs [] {
let apps = [
{name: "web-service", version: "1.2.3"}
{name: "api-service", version: "2.1.0"}
{name: "worker-service", version: "1.5.2"}
]
let environments = ["dev", "staging", "prod"]
$apps | each { |app|
$environments | each { |env|
generate-app-config $app.name $app.version $env
}
}
}
# Configuration diff and comparison
def compare-configs [env1: string, env2: string] {
print $"Comparing ($env1) vs ($env2) configurations..."
let config1 = (kcl-run $"environments/($env1).k" -f json | from json)
let config2 = (kcl-run $"environments/($env2).k" -f json | from json)
# Find differences in configuration keys
let keys1 = ($config1 | columns)
let keys2 = ($config2 | columns)
let only_in_env1 = ($keys1 | where $it not-in $keys2)
let only_in_env2 = ($keys2 | where $it not-in $keys1)
if ($only_in_env1 | length) > 0 {
print $"Keys only in ($env1): ($only_in_env1 | str join ', ')"
}
if ($only_in_env2 | length) > 0 {
print $"Keys only in ($env2): ($only_in_env2 | str join ', ')"
}
print "Configuration comparison complete"
}
```
### Advanced KCL Patterns
```nushell
# Modular configuration management
def build-modular-config [base_config: string, modules: list<string>, output: string] {
# Combine base configuration with modules
let module_imports = ($modules | each { |m| $"-Y ($m)" } | str join " ")
kcl-run $base_config $module_imports -f yaml | save $output
print $"Built modular configuration: ($output)"
}
# Configuration validation with custom rules
def validate-with-rules [config_file: string, rules_file: string] {
try {
kcl-run $config_file -Y $rules_file
print $"✅ ($config_file) passes all validation rules"
true
} catch { |err|
print $"❌ ($config_file) validation failed: ($err.msg)"
false
}
}
# Generate documentation from KCL schemas
def generate-config-docs [schema_dir: string] {
ls $"($schema_dir)/*.k"
| each { |schema|
let schema_name = ($schema.name | path basename | str replace '.k' '')
print $"## ($schema_name | str title-case)"
print ""
# Extract schema documentation (would need KCL introspection)
# This is a simplified example
open $schema.name | lines | each { |line|
if ($line | str starts-with "# ") {
$line | str replace "# " ""
}
} | str join "\n"
print ""
}
}
```
## Integration with Nushell Data Processing
Leverage Nushell's powerful data manipulation with KCL configuration management:
```nushell
# Process and generate configurations from CSV data
> open services.csv
| each { |service|
kcl-run service-template.k -D name=$service.name -D port=$service.port -f yaml
| save $"configs/($service.name).yaml"
}
# Combine multiple data sources for configuration
> let infrastructure = (sys)
> let settings = (open settings.json)
> {
infrastructure: $infrastructure,
settings: $settings,
timestamp: (date now | format date "%Y-%m-%d %H:%M:%S")
} | to json | save runtime-config.json
> kcl-run dynamic-config.k -Y runtime-config.json
# Batch process and validate configurations
> ls configs/*.k
| par-each { |config|
let result = (kcl-validate $config.name)
{
file: $config.name,
valid: ("✅" in $result),
result: $result
}
}
| where valid == false
```
## Error Handling
The plugin provides detailed error messages for common issues:
```nushell
# Syntax errors in KCL files
> kcl-run broken-config.k
Error: Error executing KCL
╭─[calling kcl-run]
│ KCL syntax error: unexpected token at line 5
# Missing files
> kcl-run nonexistent.k
Error: Error executing KCL
╭─[calling kcl-run]
│ File not found: nonexistent.k
# Validation errors
> kcl-validate invalid-project/
❌ Validation failed in invalid-project/
- main.k: schema validation error
- config.k: undefined variable 'missing_var'
```
## Features
- ✅ **KCL Execution** - Run KCL files with variable substitution and output formatting
- ✅ **Code Formatting** - Automatic KCL code formatting according to standards
- ✅ **Project Validation** - Comprehensive validation of KCL projects and files
- ✅ **Variable Support** - Dynamic configuration through command-line variables
- ✅ **Multiple Formats** - Support for YAML and JSON output formats
- ✅ **Settings Integration** - Include external setting files in KCL execution
- ✅ **Error Reporting** - Detailed error messages with context
- ✅ **Nushell Integration** - Seamless data flow between Nushell and KCL
## Use Cases
- **Infrastructure as Code**: Manage Kubernetes, Terraform, and cloud configurations
- **Configuration Management**: Environment-specific application configurations
- **Policy as Code**: Define and enforce organizational policies
- **CI/CD Pipelines**: Validate and generate configurations in automated workflows
- **Multi-Environment Deployments**: Consistent configurations across environments
- **Schema Validation**: Ensure configuration correctness with KCL schemas
- **Template Generation**: Dynamic configuration templates with variable substitution
- **Compliance Management**: Configuration compliance checking and reporting
## Best Practices
### 1. Organize KCL Projects
```
project/
├── schemas/ # Reusable schemas
│ ├── app.k
│ ├── database.k
│ └── server.k
├── environments/ # Environment-specific configs
│ ├── dev.k
│ ├── staging.k
│ └── prod.k
├── templates/ # Configuration templates
│ └── service.k
├── settings/ # External settings
│ └── common.yaml
└── output/ # Generated configurations
├── dev/
├── staging/
└── prod/
```
### 2. Use Schema-Driven Development
```kcl
# Define schemas first
schema AppConfig:
name: str
version: str
replicas: int
check:
1 <= replicas <= 100, "replicas must be between 1 and 100"
len(name) > 0, "name cannot be empty"
# Then implement configurations
config: AppConfig = {
name = "my-app"
version = "1.0.0"
replicas = 3
}
```
### 3. Create Reusable Functions
```nushell
# KCL helper functions
def kcl-dev [config: string] {
kcl-run $config -D environment=dev -f yaml
}
def kcl-prod [config: string] {
kcl-run $config -D environment=prod -Y settings/prod.yaml -f yaml
}
# Validation helpers
def validate-project [dir: string = "."] {
kcl-validate $dir
}
def format-all-kcl [] {
ls **/*.k | each { |file| kcl-format $file.name }
}
```
## Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
## License
This project is licensed under the MIT License.
## Related Projects
- [KCL](https://www.kcl-lang.io/) - Kubernetes Configuration Language
- [Nushell](https://nushell.sh/) - A new type of shell
- [nu_plugin_tera](../nu_plugin_tera/) - Tera templating plugin for Nushell
- [nu_plugin_fluent](../nu_plugin_fluent/) - Fluent localization plugin for Nushell

View File

@ -76,7 +76,7 @@ impl SimplePluginCommand for KclRun {
"Execute KCL files using the CLI wrapper" "Execute KCL files using the CLI wrapper"
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example<'_>> {
vec![Example { vec![Example {
example: "kcl-run myfile.k -D foo=bar -f json", example: "kcl-run myfile.k -D foo=bar -f json",
description: "Run 'myfile.k' with variable 'foo=bar' and output as JSON.", description: "Run 'myfile.k' with variable 'foo=bar' and output as JSON.",
@ -160,7 +160,7 @@ impl SimplePluginCommand for KclFormat {
} }
} }
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example<'_>> {
vec![Example { vec![Example {
example: "kcl-format myfile.k", example: "kcl-format myfile.k",
description: "Format the KCL file 'myfile.k'.", description: "Format the KCL file 'myfile.k'.",
@ -210,7 +210,7 @@ impl SimplePluginCommand for KclValidate {
.with_label(e.to_string(), call.head)), .with_label(e.to_string(), call.head)),
} }
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example<'_>> {
vec![Example { vec![Example {
example: "kcl-validate ./project_dir", example: "kcl-validate ./project_dir",
description: "Validate all KCL files in the directory './project_dir'.", description: "Validate all KCL files in the directory './project_dir'.",