From 876e8ea44f97f9ae98fc23323c7f8dae0ee393c5 Mon Sep 17 00:00:00 2001 From: Abhishek Mukherjee <34828+abhishekmukherg@users.noreply.github.com> Date: Mon, 14 Jul 2025 16:32:00 -0700 Subject: [PATCH 1/2] Add Nushell switch command --- hack/switch/switch.nu | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 hack/switch/switch.nu diff --git a/hack/switch/switch.nu b/hack/switch/switch.nu new file mode 100644 index 000000000..53ad81a62 --- /dev/null +++ b/hack/switch/switch.nu @@ -0,0 +1,70 @@ +const default_executable_path = "switcher" + +export def --env --wrapped main [ + ...args +]: nothing -> nothing { + let result = (switch-inner ...$args) + if $result.exit_code? != null { + return + } + + $env.KUBECONFIG = $result.kubeconfig + print $"switched to context ($result.context)" +} + +def --wrapped switch-inner [ + --executable-path: path + ...args +]: nothing -> record { + let opts = [] + + let executable_path = ($executable_path | default $default_executable_path) + + let response = (do { + ^$executable_path ...$args + } | complete) + if ($response.exit_code != 0 or ($response.stdout | str trim) == "") { + print $response.stdout + return {exit_code: $response.exit_code} + } + + let resp_text = ($response.stdout | str trim) + let prefix = "__ " + + if not ($resp_text | str starts-with $prefix) { + print $resp_text + return {exit_code: 0} + } + + # Remove prefix + let resp_text = ($resp_text | str replace $prefix "") + + # Parse out kubeconfig path and context + let parts = ($resp_text | split row ",") + if ($parts | length) != 2 { + error make { + msg: $"got an unexpected output from switcher: ($resp_text)" + } + } + let kubeconfig_path = ($parts | get 0) + let selected_context = ($parts | get 1) + + if $kubeconfig_path == "" { + error make { msg: $resp_text } + } + + if $selected_context == "" { + error make { msg: $resp_text } + } + + # Cleanup old temporary kubeconfig file + let switch_tmp_directory = $"($env.HOME)/.kube/.switch_tmp/config" + if ($env.KUBECONFIG? != null and ($env.KUBECONFIG | str contains $switch_tmp_directory)) { + rm $env.KUBECONFIG + } + + return { + kubeconfig: $kubeconfig_path + context: $selected_context + } +} From 8f82219da4af4d89547c38eafaaeb0cfceef1b96 Mon Sep 17 00:00:00 2001 From: Abhishek Mukherjee <34828+abhishekmukherg@users.noreply.github.com> Date: Mon, 17 Aug 2026 14:48:30 -0700 Subject: [PATCH 2/2] Fix Nushell type mismatch in switch-inner Nushell 0.114 checks record types structurally and rejects a record that omits declared optional fields, so returning {kubeconfig, context} failed against the record annotation. Drop the return type annotation; the $result.exit_code? check in main still works on a record lacking that field. Co-Authored-By: Claude Opus 5 --- hack/switch/switch.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/switch/switch.nu b/hack/switch/switch.nu index 53ad81a62..db83dc782 100644 --- a/hack/switch/switch.nu +++ b/hack/switch/switch.nu @@ -15,7 +15,7 @@ export def --env --wrapped main [ def --wrapped switch-inner [ --executable-path: path ...args -]: nothing -> record { +] { let opts = [] let executable_path = ($executable_path | default $default_executable_path)