Shell completion
Shell completion gives users fast help for the sofka command line. It combines a shell-specific script with values from the local kubeconfig, the selected cluster, and local plugin state. See Configuration and command line for the wider command-line model and Configuration documents and migrations for configuration source rules.
Completion model
Completion has two parts. The completion command prints a registration script without contacting a cluster. Later, the shell calls sofka for each completion request, so the request can use the command line and current cluster data.
This diagram shows how shell input becomes candidates without starting the main TUI.
flowchart LR
A[Shell] -->|registration| B[sofka completion shell]
B --> C[Shell completion script]
A -->|word completion request| D[sofka worker]
D --> E[Command and local state]
D --> F[Kubeconfig and cluster API]
E --> G[Filtered candidates]
F --> G
G --> A
Script generation
The completion subcommand accepts one supported shell and writes only its registration script to standard output. It does not need a valid local configuration or a live cluster, which lets users install completion before they configure access. The command fails when the shell is missing or unsupported.
References: src/main.rs• L115-121 src/main.rs• L157-166 tests/completion.rs• L3-40
Completion requests
The generated script marks requests with SOFKA_COMPLETE. Sofka runs the request in a separate worker and returns candidates within a two-second limit. A request can fail closed with no candidates when configuration, cluster access, discovery, or a credential helper does not finish in time.
References: src/completion.rs• L15-24 src/completion.rs• L26-107 src/completion.rs• L205-230
Shell setup
The supported shells use their normal startup or evaluation mechanism. Run the command for the shell that you use, then keep it in that shell's startup file if you want completion in new sessions.
Supported shells
| Shell | Current-session command |
|---|---|
| Bash | source <(sofka completion bash) |
| Zsh | autoload -U compinit; compinit; source <(sofka completion zsh) |
| Fish | sofka completion fish | source |
| Elvish | eval (sofka completion elvish | slurp) |
| PowerShell | sofka completion powershell | Out-String | Invoke-Expression |
Keep sofka on PATH when the shell loads the script. The script is generated by the installed binary, so loading it from the binary keeps the command grammar aligned with that binary.
References: src/completion.rs• L19-24 tests/completion.rs• L11-25
Cluster-derived values
Dynamic completion uses the command line to select the kubeconfig and context before it reads cluster data. This means an explicit --kubeconfig or --context changes the candidates for the same shell request.
Kubeconfig values
Context completion reads the merged kubeconfig and offers its context names. It does not require a cluster connection. The selected context also determines which cluster supplies namespace and resource candidates.
Namespace values
Namespace completion lists namespaces from the Kubernetes API for the selected context. It uses the namespace endpoint directly and does not run resource discovery, so a request for --namespace stays narrower than a request for resource names.
Resource values
Resource completion combines three sources into one set of names:
- built-in context and resource aliases;
- aliases from the resolved read-only configuration;
- names discovered from the cluster, including plural names, lower-case kinds, qualified names, and short names.
The result supports built-in resources and custom resources without requiring a separate list in the shell configuration.
Candidate values
Sofka keeps only values that match the text already entered. It removes duplicates and control characters, then returns a stable sorted list. Free-text arguments do not receive cluster-derived candidates, while path arguments keep the shell's path completion behavior.
References: src/completion.rs• L132-242 src/completion.rs• L245-330 src/…/completion.rs• L3-39 tests/completion.rs• L91-174 tests/completion.rs• L211-270
Plugin completion
Plugin completion uses local state rather than downloading packages during a completion request. The describe and install commands use the cached selected catalog, while update and remove use the set of managed installations. Version candidates appear after an @; install candidates are limited to versions that the catalog allows for the current sofka version and platform.
References: src/completion.rs• L305-356 tests/completion.rs• L273-375
Platform handling
Completion must stop its worker and any helper it starts when the request limit expires. Sofka uses process-group cleanup on Unix and a Windows Job Object with parent handshaking on Windows. This keeps a slow credential helper or network request from remaining after the shell has stopped waiting.
References: src/completion.rs• L64-100 src/…/windows.rs• L12-67 tests/completion.rs• L377-398
Completion testing
The completion tests check both the script contract and the values returned during real shell requests. They cover all five supported shells, invalid shell input, merged and explicit kubeconfig selection, namespaces, discovered resources, plugin state, file paths, and stalled credential helpers. A Unix Bash test also loads the generated script and checks both separated and equals-form options.
The tests protect two important boundaries: script generation must stay usable without local configuration, and dynamic completion must not alter configuration while it reads context-specific aliases.
References: tests/completion.rs• L3-40 tests/completion.rs• L91-174 tests/completion.rs• L211-270 tests/completion.rs• L273-398 tests/completion.rs• L400-471
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the shell names accepted by the CLI aligned with the names emitted in the registration scripts.
- Keep the completion request protocol aligned with the generated script, including the
SOFKA_COMPLETEmarker and shell-specific word indexes. - Preserve the difference between kubeconfig-only values and cluster-derived values so completion can still work without a cluster.
- If resource discovery changes its candidate names, update custom-resource, short-name, and qualified-name behavior together.
- If worker lifetime or credential-helper behavior changes, review both Unix process-group cleanup and Windows descendant cleanup.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Missing or unsupported shell argument | No script is printed and the CLI exits with an error | The shell is a required enum argument and is tested for both missing and unknown values |
| Invalid kubeconfig or local configuration during script generation | Script generation still succeeds because it does not load those values | Script tests run with invalid configuration files |
| Unreachable cluster or failed discovery | Resource or namespace candidates are empty for that request | Candidate lookup is bounded and returns no values on read or timeout errors |
| Stalled credential helper | The shell request could hang without a request bound | The worker has a two-second limit and platform-specific descendant cleanup |
A generated script is loaded with a different sofka binary on PATH | The shell can ask one binary to use a registration contract from another | Generate and load the script from the same installed binary |
Key files
| File | Why to read it |
|---|---|
| src/main.rs | Defines the public completion command and its no-cluster script path |
| src/completion.rs | Describes the shell request protocol, candidate sources, filtering, and request bounds |
| src/…/completion.rs | Shows the cluster API values exposed to namespace and resource completion |
| src/…/windows.rs | Shows the Windows lifetime boundary for completion workers and descendants |
| tests/completion.rs | Defines the cross-shell, cluster-value, plugin, timeout, and generated-script guarantees |