Skip to content

Extract shared SelectProfile helper to eliminate duplicate profile pickers#4647

Draft
mihaimitrea-db wants to merge 21 commits intomainfrom
mihaimitrea-db/stack/auth_logout_deduplication
Draft

Extract shared SelectProfile helper to eliminate duplicate profile pickers#4647
mihaimitrea-db wants to merge 21 commits intomainfrom
mihaimitrea-db/stack/auth_logout_deduplication

Conversation

@mihaimitrea-db
Copy link
Collaborator

@mihaimitrea-db mihaimitrea-db commented Mar 3, 2026

🥞 Stacked PR

Use this link to review incremental changes.


Four places built nearly identical promptui.Select prompts for interactive profile selection (auth logout, auth token, cmd/root/auth.go, cmd/root/bundle.go). This PR extracts a reusable profile.SelectProfile function that accepts a declarative SelectConfig with label, profiles, and template strings, replacing all four implementations.

Changes

  • Add profile.SelectProfile in libs/databrickscfg/profile/select.go — a shared interactive profile picker that accepts a SelectConfig (label, profiles, template strings) and returns the selected profile name.
  • Replace the four inline promptui.Select implementations in cmd/auth/logout.go, cmd/auth/token.go, cmd/root/auth.go, and cmd/root/bundle.go with calls to SelectProfile.
  • Add AccountID to Profiles.SearchCaseInsensitive so all pickers support searching by account ID, not just name and host.
  • Extract writeConfigFile helper in libs/databrickscfg/ops.go to consolidate the repeated default-comment / backup / save sequence shared by SaveToProfile and DeleteProfile.
  • Switch auth login from os.Getenv to env.Get(ctx, ...) for DATABRICKS_CONFIG_FILE so it respects context-level env overrides, consistent with the rest of the codebase.

Why

The four profile pickers each duplicated the same prompt setup, searcher wiring, and result extraction. This made it easy for behavior to diverge (e.g., only the logout picker searched by account ID). A single shared helper keeps the UX consistent and reduces the surface area for future changes.

Tests

  • Existing unit and acceptance tests for auth logout, auth token, workspace/account profile selection, and bundle profile resolution continue to pass — the refactor is behavior-preserving.
  • The SelectProfile helper is exercised indirectly through all existing callers.

Implement the initial version of databricks auth logout which removes a profile from ~/.databrickscfg and clears associated OAuth tokens from the token cache.

This iteration supports explicit profile selection via --profile and a --force flag to skip the confirmation prompt. Interactive profile selection will be added in a follow-up.

Token cache cleanup is best-effort: the profile-keyed token is always removed, and the host-keyed token is removed only when no other profile references the same host.
Replace plain fmt.Sprintf confirmation prompt with a structured template using cmdio.RenderWithTemplate. The warning now uses color and bold formatting to clearly highlight the profile name, config path, and consequences before prompting for confirmation.
Resolve config path from the profiler instead of hardcoding fallbacks. Delete the profile before clearing the token cache so a config write failure does not leave tokens removed. Fix token cleanup for account and unified profiles by computing the correct OIDC cache key (host/oidc/accounts/<account_id>). Drop the nil profiler guard, add a success message on logout, and extract backupConfigFile in ops.go to remove duplication. Consolidate token cleanup tests into a table-driven test covering shared hosts, unique hosts, account, and unified profiles.
Merge shared-host token deletion verification into one main parametrized test by addding the hostBasedKey and isSharedKey parameters to each case. This replaces the TestLogoutTokenCacheCleanup test with an assertion: host-based keys are preserved when another profile shares the same host, and deleted otherwise.
Rewrite the test to use inline config seeds and explicit expected state. Add cases for deleting the last non-default profile, deleting a unified host profile with multiple keys, and deleting the DEFAULT section.
- Use profiler.GetPath() to resolve config path instead of hardcoding platform-specific defaults for the help text.
- Read DATABRICKS_CONFIG_FILE via env.Get(ctx, ...) instead of os.Getenv to respect context-level env overrides.
- Add abort message when user declines the confirmation prompt.
- Guard DeleteProfile against non-existent profiles to avoid creating unnecessary backup files.
- Add TestDeleteProfile_NotFound for the error path.
Cover four scenarios: profile ordering and comments are preserved after deletion, deleting the last non-default profile leaves an empty DEFAULT section, deleting the DEFAULT profile itself clears its keys and restores the default comment, and error paths for non-existent profiles and missing --profile in non-interactive mode.
By default, Authentication related commands. For more information regarding how
authentication for the Databricks CLI and SDKs work please refer to the documentation
linked below.

AWS: https://docs.databricks.com/dev-tools/auth/index.html
Azure: https://learn.microsoft.com/azure/databricks/dev-tools/auth
GCP: https://docs.gcp.databricks.com/dev-tools/auth/index.html

Usage:
  databricks auth [command]

Available Commands:
  describe    Describes the credentials and the source of those credentials, being used by the CLI to authenticate
  env         Get env
  login       Log into a Databricks workspace or account
  profiles    Lists profiles from ~/.databrickscfg
  token       Get authentication token

Flags:
      --account-id string              Databricks Account ID
      --experimental-is-unified-host   Flag to indicate if the host is a unified host
  -h, --help                           help for auth
      --host string                    Databricks Host
      --workspace-id string            Databricks Workspace ID

Global Flags:
      --debug            enable debug logging
  -o, --output type      output type: text or json (default text)
  -p, --profile string   ~/.databrickscfg profile
  -t, --target string    bundle target to use (if applicable)

Use "databricks auth [command] --help" for more information about a command. now only clears cached OAuth
tokens without removing the profile from ~/.databrickscfg. Pass
--delete to also remove the profile entry from the config file.
Existing acceptance tests that verify profile deletion now use --delete
since profile removal is opt-in. Two new acceptance tests verify token-only
logout: one for a unique host (both cache entries cleared) and one for a
shared host (host-keyed token preserved).
@eng-dev-ecosystem-bot
Copy link
Collaborator

eng-dev-ecosystem-bot commented Mar 3, 2026

Commit: 16de011

Run: 22665281247

Env ✅​pass 🙈​skip Time
✅​ aws linux 73 14 1:09
✅​ aws windows 73 14 0:50
✅​ aws-ucws linux 76 11 1:19
✅​ aws-ucws windows 76 11 1:06
✅​ azure linux 72 14 0:49
✅​ azure windows 72 14 0:34
✅​ azure-ucws linux 75 11 1:22
✅​ azure-ucws windows 75 11 0:58
✅​ gcp linux 73 14 1:07
✅​ gcp windows 73 14 1:05

Replace manual strings.TrimRight(host, /) with the SDK's
config.Config.CanonicalHostName(), which handles scheme
normalization, trailing slashes, and empty hosts consistently
with how the SDK itself computes token cache keys.
@mihaimitrea-db mihaimitrea-db force-pushed the mihaimitrea-db/stack/auth_logout_deduplication branch from c6d25f1 to c9512bf Compare March 4, 2026 09:06
@mihaimitrea-db mihaimitrea-db force-pushed the mihaimitrea-db/stack/auth_logout_deduplication branch from 8a5d258 to b40887a Compare March 4, 2026 09:45
When --profile is not specified in an interactive terminal, show a
searchable prompt listing all configured profiles. Profiles are sorted
alphabetically and displayed with their host or account ID. The picker
supports fuzzy search by name, host, or account ID.
Document the four interaction modes (explicit profile, interactive
picker, non-interactive error, and --force) in the command's long
help text.
…ckers

Four places built nearly identical promptui.Select prompts for
interactive profile selection (auth logout, auth token, root auth,
root bundle). Extract a reusable profile.SelectProfile function that
takes a declarative SelectConfig with label, profiles, and template
strings.

Also: add AccountID to Profiles.SearchCaseInsensitive so all pickers
support searching by account ID, and remove the redundant
not-found guard in DeleteProfile (ini.DeleteSection is already a
no-op for missing sections).
Extract writeConfigFile helper to consolidate the repeated
default-comment, backup, and save sequence shared by SaveToProfile
and DeleteProfile.

Also switch auth login from os.Getenv to env.Get(ctx, ...) for
DATABRICKS_CONFIG_FILE so it respects context-level env overrides,
consistent with the rest of the codebase.
@mihaimitrea-db mihaimitrea-db force-pushed the mihaimitrea-db/stack/auth_logout_deduplication branch from b40887a to 16de011 Compare March 4, 2026 10:25
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