Skip to content

Commit f121e12

Browse files
simonfaltumclaude
andauthored
Add databricks auth switch command for setting the default profile (#4651)
## Why There is no way to set which profile is the active default without renaming it to `[DEFAULT]`. Users with multiple profiles need a simple way to switch between them. ## Changes Before: no mechanism to designate a default profile. Users had to rename sections in `~/.databrickscfg` or always pass `--profile`. Now: `databricks auth switch` lets users select a named profile as the default. The choice is stored in a `[__settings__]` section with a `default_profile` key. `auth profiles` shows a `(Default)` marker, and `auth describe` shows the resolved default name. ### Why `[__settings__]` instead of a top-level key? INI files place keys that appear before any section header into the `[DEFAULT]` section. The CLI already uses `[DEFAULT]` as a regular profile section (with `host`, `token`, etc.), so adding `default_profile` there would mix settings with profile credentials, and the profiles command would try to interpret `[DEFAULT]` as a profile containing that key. A dedicated `[__settings__]` section avoids this collision and is silently ignored by older SDKs (no `host` key means it is skipped in profile iteration). ### Implementation - **`libs/databrickscfg/ops.go`**: `GetDefaultProfile`/`GetDefaultProfileFrom` with fallback resolution (explicit setting, single-profile auto-default, legacy DEFAULT section). `SetDefaultProfile` to write the setting. Shared `backupAndSaveConfigFile` and `resolveConfigFilePath` helpers to deduplicate existing code. - **`cmd/auth/switch.go`**: new command with `--profile` flag (non-interactive) and interactive profile picker showing the current default. - **`cmd/auth/profiles.go`**: `(Default)` marker in output and `default` field in JSON. - **`cmd/auth/describe.go`**: shows resolved default profile name, e.g. `profile: default (my-workspace)`. - **`cmd/auth/login.go` + `token.go`**: auto-set default when creating the very first profile. Resolution wiring (making the CLI actually use `default_profile` when no `--profile` is given) is out of scope. That will be a follow-up change touching `cmd/root/auth.go` and the bundle config path. ## Test plan - [x] Unit tests for `GetDefaultProfile`, `GetDefaultProfileFrom`, `SetDefaultProfile` (table-driven, covering: explicit setting, single-profile fallback, DEFAULT fallback, no file, round-trip with `SaveToProfile`) - [x] Unit tests for `auth switch` command (with `--profile`, profile not found, non-interactive error, settings section written correctly) - [x] Unit tests for `hasNoProfiles` (fresh machine / ErrNoConfiguration, empty file, existing profiles) - [x] Unit test for `(Default)` marker in `auth profiles` - [x] `GetDefaultProfile` does not create the config file as a side effect (asserted in test) - [x] `make checks` and `make lintfull` pass - [x] Manual: `databricks auth switch --profile <name>`, verify `~/.databrickscfg` has `[__settings__]` section - [x] Manual: `databricks auth profiles` shows `(Default)` marker - [x] Manual: `databricks auth switch` interactive picker shows current default --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c8d825c commit f121e12

File tree

29 files changed

+1169
-9
lines changed

29 files changed

+1169
-9
lines changed

acceptance/cmd/auth/describe/default-profile/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
=== Describe without --profile (should use default)
3+
4+
>>> [CLI] auth describe
5+
Host: [DATABRICKS_URL]
6+
User: [USERNAME]
7+
Authenticated with: pat
8+
-----
9+
Current configuration:
10+
✓ host: [DATABRICKS_URL] (from DATABRICKS_HOST environment variable)
11+
✓ token: ******** (from DATABRICKS_TOKEN environment variable)
12+
✓ profile: my-workspace
13+
✓ databricks_cli_path: [CLI]
14+
✓ auth_type: pat
15+
✓ rate_limit: [NUMID] (from DATABRICKS_RATE_LIMIT environment variable)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sethome "./home"
2+
3+
# Create a config with two profiles and an explicit default.
4+
cat > "./home/.databrickscfg" <<EOF
5+
[DEFAULT]
6+
7+
[my-workspace]
8+
host = $DATABRICKS_HOST
9+
token = $DATABRICKS_TOKEN
10+
11+
[other-workspace]
12+
host = https://other.cloud.databricks.com
13+
token = other-token
14+
15+
[__settings__]
16+
default_profile = my-workspace
17+
EOF
18+
19+
title "Describe without --profile (should use default)\n"
20+
trace $CLI auth describe
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ignore = [
2+
"home"
3+
]

acceptance/cmd/auth/login/nominal/out.databrickscfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[test]
55
host = [DATABRICKS_URL]
66
auth_type = databricks-cli
7+
8+
[__settings__]
9+
default_profile = test

acceptance/cmd/auth/login/nominal/output.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
Profile test was successfully saved
44

55
>>> [CLI] auth profiles
6-
Name Host Valid
7-
test [DATABRICKS_URL] YES
6+
Name Host Valid
7+
test (Default) [DATABRICKS_URL] YES

acceptance/cmd/auth/login/with-scopes/out.databrickscfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
host = [DATABRICKS_URL]
66
scopes = jobs,pipelines,clusters
77
auth_type = databricks-cli
8+
9+
[__settings__]
10+
default_profile = scoped-test
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
2+
[DEFAULT]
3+
4+
[__settings__]

acceptance/cmd/auth/logout/delete-clears-default/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
=== Initial settings section
3+
[__settings__]
4+
default_profile = workspace-a
5+
6+
=== Delete the default profile
7+
>>> [CLI] auth logout --profile workspace-a --force --delete
8+
Deleted profile "workspace-a" with no tokens to clear.
9+
10+
=== Settings after deleting default — default_profile should be cleared
11+
[__settings__]
12+
13+
=== Delete a non-default profile
14+
>>> [CLI] auth logout --profile workspace-b --force --delete
15+
Deleted profile "workspace-b" with no tokens to clear.
16+
17+
=== Settings after deleting non-default — section should still be present but empty
18+
[__settings__]

0 commit comments

Comments
 (0)