Skip to content

feat: discover contexts on login, select with ctx use#149

Merged
scotwells merged 5 commits intomainfrom
feat/context-discovery
Apr 16, 2026
Merged

feat: discover contexts on login, select with ctx use#149
scotwells merged 5 commits intomainfrom
feat/context-discovery

Conversation

@scotwells
Copy link
Copy Markdown
Contributor

@scotwells scotwells commented Apr 14, 2026

Summary

Logging in and picking a project should be one step, and datumctl should remember your choice so you don't have to repeat it on every command. This PR makes that the default. Switching to a different project — or to another account — takes one command, and drops you right where you left off.

First run: log in and pick a context

datumctl login handles authentication and context selection in one step. A browser opens, then the CLI fetches your orgs and projects and shows a picker:

✓ Authenticated as Jane Doe (jane@acme.com)

Discovering organizations and projects...

You have access to 2 organization(s):

  acme-corp (3 project(s))
  personal (1 project(s))

┃ Select a context to work in
┃ > acme-corp  (Acme Corp)
┃     infra
┃     web-app
┃     billing-api
┃   personal
┃     sandbox

✓ Context set to acme-corp/infra

If you only have one project, the picker is skipped. From there, resource commands (get, apply, create, etc.) automatically use the active context — datumctl get dnszones just works, no --project or --organization flags required. To list, inspect, or change your active context:

$ datumctl ctx
  DISPLAY NAME         NAME                       TYPE      CURRENT
  Acme Corp            acme-corp                  org
    Infrastructure     acme-corp/infra            project   *
    Web App            acme-corp/web-app          project
  Personal             personal                   org
    Sandbox            personal/sandbox           project

$ datumctl ctx use acme-corp/web-app
✓ Switched to acme-corp/web-app

$ datumctl whoami
User:         Jane Doe (jane@acme.com)
Context:      acme-corp/web-app
Organization: Acme Corp (acme-corp)
Project:      Web App (web-app)

Switching accounts without losing your place

Each login session remembers its last-used context, so switching users drops you right back where you left off:

$ datumctl auth list
  USER                 STATUS
  jane@acme.com        Active
  bob@acme.com

$ datumctl auth switch bob@acme.com
✓ Switched to Bob Smith (bob@acme.com)
  Context:  acme-corp/billing-api

Scripts, CI, and machine accounts

For non-interactive use, environment variables override the active context per-invocation:

DATUM_PROJECT=my-project datumctl get dnszones
DATUM_ORGANIZATION=my-org datumctl get projects

--project and --organization flags still work too. For machine-to-machine auth:

datumctl login --credentials ./my-key.json --hostname auth.datum.net

Tip

Run datumctl --help for the full command list.

Upgrading from earlier versions

datumctl auth login and datumctl auth logout have been removed — use datumctl login and datumctl logout instead. datumctl login supports all the same auth modes (PKCE, --no-browser for device flow, --credentials for machine accounts).

Pre-existing keyring credentials are bootstrapped into a v1beta1 session on first run, so previously authenticated users won't have to log in again. They will, however, want to run datumctl ctx use (or rely on DATUM_PROJECT/DATUM_ORGANIZATION) to pick a context.

Anyone with a kubeconfig generated by an earlier datumctl auth update-kubeconfig will need to re-run the command — the kubectl exec plugin args now reference session names rather than the legacy format.

🤖 Generated with Claude Code

@gianarb
Copy link
Copy Markdown
Collaborator

gianarb commented Apr 14, 2026

this looks good to me, do you want to get it out of draft so we can move it over and merge?

@scotwells scotwells marked this pull request as ready for review April 14, 2026 20:45
@scotwells scotwells requested a review from gianarb April 14, 2026 20:45
@scotwells scotwells force-pushed the feat/context-discovery branch from 459d18b to 7241d91 Compare April 15, 2026 21:19
@scotwells scotwells changed the base branch from feat/persistent-layer to main April 15, 2026 23:05
@scotwells scotwells force-pushed the feat/context-discovery branch 2 times, most recently from 073453d to 692ba86 Compare April 16, 2026 01:47
Replace the per-command --project/--organization flow with a context
system. `datumctl login` now authenticates and picks a default org or
project in one step, and subsequent commands use it automatically.

New top-level commands:
  - datumctl login / logout / whoami
  - datumctl ctx (list, use, --refresh)

Auth group:
  - auth list and auth switch are session-aware; switching users
    restores each session's last-used context
  - auth login and auth logout removed; use the top-level commands

login supports all previous auth modes: PKCE, device flow
(--no-browser) for headless environments, and machine accounts
(--credentials FILE).

Config moves to a v1beta1 format with session-keyed credentials.
Pre-existing keyring entries are bootstrapped on first run so
previously-authenticated users don't have to log in again. Env vars
(DATUM_PROJECT / DATUM_ORGANIZATION) and the --project /
--organization flags continue to override the active context
per-invocation for scripts and CI.

The kubectl exec plugin args now reference session names, so existing
kubeconfigs produced by 'datumctl auth update-kubeconfig' need to be
regenerated.
@scotwells scotwells force-pushed the feat/context-discovery branch from 9f060e2 to 8a3a899 Compare April 16, 2026 14:44
@scotwells scotwells mentioned this pull request Apr 16, 2026
5 tasks
scotwells and others added 3 commits April 16, 2026 11:27
Distinguish org vs project contexts in the post-login and ctx-use
confirmation by adding a ContextDescription helper that produces
"org <name> (<id>)" or "project <name> in <org> (<ref>)" output.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Only print the auth URL and --no-browser hint when the browser fails
to open, keeping the happy-path output clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@gianarb gianarb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty nice!! It works with the various tests I made and ctx --refresh migrates my configuration as well 👍

@scotwells scotwells merged commit 22a2bb3 into main Apr 16, 2026
2 checks passed
@scotwells scotwells deleted the feat/context-discovery branch April 16, 2026 17:34
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.

2 participants