Skip to content

feat: restore last used ctx and ns#376

Open
pasteley wants to merge 1 commit intosbstp:masterfrom
pasteley:last-used-ctx
Open

feat: restore last used ctx and ns#376
pasteley wants to merge 1 commit intosbstp:masterfrom
pasteley:last-used-ctx

Conversation

@pasteley
Copy link

Add support for restoring the last-used context across shell sessions:

Closes: #88

What's added

  • kubie ctx --last-used cli argument: attempts to restore the most recently used context from kubie state.
  • behavior.track_last_used config option: when enabled, kubie records the last successfully entered context into the state file on every successful context entry.

Motiviation

Automatically re-enter context used most recently (without interacting with the context selector) on new terminal sessions (e.g. zsh init).

This PR provides:

  • explicit restore via --last-used (safe to call from shell init)
  • opt-in recording via behavior.track_last_context (keeps default behavior unchanged)

How it works

Recording

When behavior.track_last_used: true, kubie updates state on each successful kubie ctx ... entry:

  • state.last_context = <resolved context name>

This happens regardless of whether the context was chosen via selector or passed positionally.

Restoring

When kubie ctx --last-used is called:

  • kubie reads state.last_context
  • if it exists and the context is still installed → enter it
  • if it is missing/invalid → no-op (exit 0, no selector UI), making it safe for shell init

kubie ctx --last-used <context> behaves like kubie ctx <context> (explicit context wins); the flag only affects whether restore-from-state is attempted.

Typical usage (in shell init)

if [[ -o interactive ]] && [[ -z "${KUBIE_ACTIVE}" ]]; then
  kubie ctx --last-used >/dev/null 2>&1
fi

Signed-off-by: pasteley <ceasebeing@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set last context as default context

1 participant