Configuration and command-line behavior
Sofka builds each application session from command-line choices, Kubernetes context data, saved session state, and layered user settings. These inputs determine the first view, namespace scope, safety mode, diagnostics, and shell-facing behavior while keeping one-run choices separate from shared configuration.
Session inputs
The session starts with a small set of external inputs. The command line selects the launch target and can pin connection or safety behavior for the run. Kubeconfig supplies context and cluster data. Sofka state remembers choices such as the last namespace for each context.
| Input | Session role |
|---|---|
| Command-line options | Choose the context, kubeconfig, resource, namespace scope, connection checks, TLS behavior, and read or write mode. |
| Kubeconfig | Supplies the current context, named contexts, cluster identity, and the namespace attached to a context. |
| User configuration | Supplies defaults, aliases, display choices, safety rules, and feature settings. |
| Context overrides | Change effective settings for one cluster or one context. |
| Saved Sofka state | Restores context-specific choices such as namespace and sort selection without changing user configuration. |
The command line has priority when it names a resource or namespace. It can also force safety or connection behavior for the whole run. The session then applies effective settings for the selected cluster and context. This separation lets a user make a one-run choice without changing a shared file.
References: src/main.rs• L26-113 src/main.rs• L202-220 src/main.rs• L506-524 src/nsmem.rs• L1-9
Startup sequence
Startup turns the inputs into either an interactive terminal session or a headless command result. Sofka loads the base configuration before the first cluster request so it can start logging early. It resolves cluster and context overrides after it knows the selected target.
The main startup path follows this sequence:
- Handle completion, plugin adapter and validation, import, plugin-management, and release-check commands that do not need the terminal session.
- Apply an explicit
--kubeconfigpath to the process so all kubeconfig readers and shell-outs use the same source. - Load the base configuration and start application logging.
- Connect to the selected context, or open the context picker when the user asks for
ctxorcontextsor when interactive connection can fall back to context selection. - Resolve drop-ins and cluster or context overrides.
- Apply aliases, safety mode, display settings, saved state, and feature settings.
- Open the chosen resource and start automatic port-forwards when the session is connected.
An explicit context supplied with ctx or contexts is checked against the kubeconfig before the picker opens. The picker is interactive, so ctx and contexts cannot be combined with --check or --snapshot. An interactive connection error can lead to the context picker. --check and --snapshot instead report the error and exit because they have no screen in which to recover.
The diagram shows how launch inputs become a running session or a headless result.
flowchart TD
A[Command line] --> B{Shell-facing or validation command?}
B -->|Yes| C[Run command and exit]
B -->|No| D[Load base configuration]
D --> E[Start logging]
E --> F[Select kubeconfig and context]
F --> G{Interactive or headless run?}
G -->|Headless| H[Report result and exit]
G -->|Interactive| I[Resolve cluster and context settings]
I --> J[Restore saved session state]
J --> K[Open resource view]
References: src/main.rs• L150-200 src/main.rs• L243-329 src/main.rs• L373-534
Configuration sources
The configuration system provides a base file and optional layers. Sofka looks under $XDG_CONFIG_HOME/sofka, with ~/.config/sofka as the usual fallback. An empty XDG_CONFIG_HOME is treated as unset, so it does not create a relative configuration path. The same setting model works in TOML and YAML.
The effective configuration is built in this order:
- Base configuration.
- Files in
conf.d/, sorted by file name. - A cluster-level file under
clusters/<cluster>/. - A context-level file under
clusters/<cluster>/<context>/. - User and bundled plugin packages.
Cluster and context names map to safe directory names. A cluster layer applies to every context on that cluster. A context layer applies only to its named kubeconfig context. The loader reads override layers again during a context switch, so the active context can change its settings without a process restart.
Tables merge by key. Scalar values and arrays replace inherited values. Inline plugins can use name-based merging when a shared layer and a personal layer must combine entries. Each document is checked before it enters the effective configuration. Invalid base configuration falls back to defaults at startup. Invalid drop-ins and override layers are skipped with warnings. If an override makes the merged typed configuration invalid, Sofka keeps the valid base configuration instead of discarding the whole session.
See Configuration documents and migrations for file format, merge rules, and configuration evolution.
References: src/config.rs• L1-24 src/config.rs• L1361-1484 src/config.rs• L1486-1592 src/config.rs• L1595-1660 src/config.rs• L1762-1793
Launch selection
Launch selection determines the first context, namespace scope, and resource view. The positional resource and --resource are equivalent choices. If neither is present, Sofka uses default_resource and then falls back to pods.
The ctx and contexts resource names select the interactive context picker instead of a resource view. An explicit --context narrows that picker to a named kubeconfig context, and Sofka rejects the launch if that name does not exist.
Namespace selection is a policy rather than a single setting. The normal order is:
- An explicit
-nor--namespace, or-Afor all namespaces. - The kubeconfig namespace when
prefer_context_namespaceis enabled. - The saved namespace for the selected context.
default_namespacefrom effective configuration.- The Kubernetes namespace fallback.
Namespace picks are saved per kubeconfig context in the state directory. This state is separate from user configuration, so normal navigation does not rewrite the configuration file. A saved all-namespaces choice remains a valid scope.
References: src/main.rs• L202-239 src/main.rs• L578-597 src/main.rs• L1088-1107 src/nsmem.rs• L19-99
Safety mode
Safety mode controls whether the session can perform actions that change the cluster or run commands. Configuration can set readonly for all sessions or for a specific cluster or context. The --readonly and --write flags override that setting for the whole process, and the two flags cannot be used together.
The mode is fixed by the CLI when one of those flags is present. Without a CLI override, Sofka re-resolves the configured mode when the user changes context. This lets a production override stay read-only while a session follows the safety policy of each target context.
References: src/main.rs• L75-84 src/main.rs• L569-577 src/config.rs• L61-73
Runtime changes
The command palette can reload configuration during an interactive session. A successful reload applies settings that affect later views or context changes and refreshes active configuration warnings. A failed base reload keeps the last valid base configuration. Override errors remain visible as warnings and do not stop the application.
Not every setting has the same lifetime. Some values seed the session and remain under live user control after a reload. Context-scoped values are read again when the context changes. Saved navigation state has its own lifetime because it persists outside the configuration files.
Use :config to inspect selected sources and warnings when effective behavior is not clear. Use :reload after editing configuration or plugin packages.
References: src/config.rs• L1396-1415 src/config.rs• L1473-1483 src/config.rs• L1545-1577 src/main.rs• L449-451
Shell-facing commands
Sofka has command paths for scripts, shells, diagnostics, configuration import, plugin management, and release checks. completion prints a shell script without loading configuration or connecting to a cluster. info reports build, configuration, connection, discovery, logging, and directory state. plugin manages reviewed plugin packages and can produce machine-readable output. import converts another tool's configuration into Sofka configuration files, while check-update reports a newer release and an upgrade hint without installing it.
The explicit --kubeconfig path affects the whole process. This keeps Sofka's Kubernetes client, context inspection, completion queries, and kubectl-backed actions aligned on one kubeconfig source. The hidden plugin adapter and plugin validation paths also exit before normal session setup so they can use stdout for their own reports.
Completion can query the selected kubeconfig, context settings, Kubernetes discovery, namespaces, and the plugin catalog. These lookups are bounded and quiet. If cluster data is unavailable, the shell receives no completion error output.
See Shell completion for shell setup and supported completion values.
References: src/main.rs• L115-200 src/main.rs• L243-289 src/main.rs• L741-750 src/main.rs• L777-841 src/completion.rs• L15-107 src/plugin_cli.rs• L14-77
Headless operation
Headless modes let automation inspect or render a session without taking over a terminal. --check connects, performs discovery, checks namespace access, and prints a summary. --snapshot renders one resource frame without requiring a TTY. info --offline reports local state without attempting a cluster connection, while info can connect briefly to include live cluster and watch health data.
These modes use the same launch choices as an interactive session where possible. In particular, info resolves the same resource and namespace that the selected flags would open and probes that watch when a connection is available. It reports connection problems as part of the diagnostic output so the command can explain a broken setup instead of hiding useful local details behind an early failure. The deprecated top-level --info spelling keeps the offline diagnostic behavior.
References: src/main.rs• L86-106 src/main.rs• L880-1085 src/main.rs• L544-547 src/main.rs• L607-611 src/main.rs• L777-985
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to CLI launch precedence must stay aligned with the namespace and resource choices reported by
infoand used by completion. - Changes to kubeconfig selection must preserve the process-wide
KUBECONFIGcontract for client reads and kubectl-backed actions. - Changes to configuration merge order must keep base, drop-in, cluster, and context resolution consistent across startup, reload, context switches, and read-only completion lookup.
- Changes to
readonlyor--writebehavior must preserve the distinction between a session-wide CLI pin and a context-specific configuration value. - Changes to saved namespace state must keep the all-namespaces spelling compatible with disk state and namespace resolution.
- Changes to shell-facing output must preserve script-only stdout and diagnostic output on stderr where shell integration depends on it.
- Changes to the configuration directory rules must keep empty
XDG_CONFIG_HOMEvalues on the home-directory fallback path. - Changes to context-picker validation must keep
ctxandcontextsinteractive and must validate an explicit--contextagainst the selected kubeconfig. - Changes to
infomust keep its launch-resource and namespace report aligned with the session that the same flags would start.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| More than one supported base, cluster, or context config file exists at one level | That configuration level reports a conflict and does not provide a clean source | Keep one config.toml, config.yaml, or config.yml at each level |
| Base configuration is malformed or has a type error | Startup uses defaults; a failed reload keeps the last valid base | Check :config warnings and validate the edited file before reload |
| An override file is invalid or makes the merged config invalid | The bad override is skipped or effective settings fall back to the base | Fix the named override and reload; do not assume partial values were applied |
--readonly and --write are both supplied | Argument parsing rejects the launch | Select one session safety mode |
ctx or contexts is combined with --check or --snapshot | Argument validation rejects the launch because the picker needs an interactive session | Remove the headless flag or choose a concrete resource |
An explicit --context is not present in the kubeconfig during context-picker launch | The picker does not start and reports the missing context | Use a context name from the selected kubeconfig |
| Completion needs cluster data but the selected context cannot list namespaces or discover APIs | No dynamic candidates are returned, with no shell error | Verify kubeconfig, credentials, context selection, and API permissions |
| Completion invokes a slow or interactive credential helper | The completion request is bounded and returns no candidates | Use a non-interactive helper that completes within the request limit |
| The selected context cannot connect in a headless mode | --check or --snapshot exits with an error instead of opening a picker | Test the same context with sofka info or use interactive context selection |
Key files
| File | Why to read it |
|---|---|
| src/main.rs | Defines the CLI surface and turns launch inputs into interactive, diagnostic, or headless behavior. |
| src/config.rs | Defines the configuration model, source layers, effective merge behavior, reload behavior, and warning policy. |
| src/…/document.rs | Explains TOML and YAML document selection and parsing at each configuration level. |
| src/nsmem.rs | Shows how namespace choices persist per context and enter launch precedence. |
| src/completion.rs | Defines dynamic shell completion behavior and its bounded cluster lookups. |
| src/plugin_cli.rs | Defines shell-facing plugin management commands and their user-visible outcomes. |
| docs/configuration-documents-and-migrations.md | Child page for configuration formats, migration behavior, and layer evolution. |
| docs/shell-completion.md | Child page for shell setup and completion behavior. |