Skip to content

Commit cb3c326

Browse files
Extract shared SelectProfile helper to eliminate duplicate profile pickers (#4647)
## 🥞 Stacked PR Use this [link](https://github.com/databricks/cli/pull/4647/files/e5a1d8842ee9a458d4ffb3cea672a0f88077356f..51f14b5c7ea409cffdc78a3025a70de5c674fa9a) to review incremental changes. - [stack/auth_logout](#4613) [[Files changed](https://github.com/databricks/cli/pull/4613/files)] - [stack/auth_logout_profile_picker](#4616) [[Files changed](https://github.com/databricks/cli/pull/4616/files/c9e8d79e276d6c33faa1e859ca20cc5136f9efb7..e5a1d8842ee9a458d4ffb3cea672a0f88077356f)] - [**stack/auth_logout_deduplication**](#4647) [[Files changed](https://github.com/databricks/cli/pull/4647/files/e5a1d8842ee9a458d4ffb3cea672a0f88077356f..51f14b5c7ea409cffdc78a3025a70de5c674fa9a)] --------- 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`. ## 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. --------- Co-authored-by: simon <simon.faltum@databricks.com>
1 parent 70f2d7e commit cb3c326

File tree

38 files changed

+1313
-87
lines changed

38 files changed

+1313
-87
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
; Dev workspace
5+
[dev]
6+
host = https://dev.cloud.databricks.com
7+
auth_type = databricks-cli

acceptance/cmd/auth/logout/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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
=== Initial config
3+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
4+
[DEFAULT]
5+
host = https://default.cloud.databricks.com
6+
auth_type = databricks-cli
7+
8+
; Dev workspace
9+
[dev]
10+
host = https://dev.cloud.databricks.com
11+
auth_type = databricks-cli
12+
13+
=== Delete the DEFAULT profile
14+
>>> [CLI] auth logout --profile DEFAULT --force --delete
15+
Logged out of and deleted profile "DEFAULT".
16+
17+
=== Backup file should exist
18+
OK: Backup file exists
19+
20+
=== Config after logout — empty DEFAULT with comment should remain at the top of the file
21+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
22+
[DEFAULT]
23+
24+
; Dev workspace
25+
[dev]
26+
host = https://dev.cloud.databricks.com
27+
auth_type = databricks-cli
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
sethome "./home"
2+
3+
cat > "./home/.databrickscfg" <<'EOF'
4+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
5+
[DEFAULT]
6+
host = https://default.cloud.databricks.com
7+
auth_type = databricks-cli
8+
9+
; Dev workspace
10+
[dev]
11+
host = https://dev.cloud.databricks.com
12+
auth_type = databricks-cli
13+
EOF
14+
15+
title "Initial config\n"
16+
cat "./home/.databrickscfg"
17+
18+
title "Delete the DEFAULT profile"
19+
trace $CLI auth logout --profile DEFAULT --force --delete
20+
21+
title "Backup file should exist\n"
22+
assert_backup_exists
23+
24+
title "Config after logout — empty DEFAULT with comment should remain at the top of the file\n"
25+
cat "./home/.databrickscfg"
26+
27+
cp "./home/.databrickscfg" "./out.databrickscfg"

acceptance/cmd/auth/logout/delete-pat-token-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
=== Initial config
3+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
4+
[DEFAULT]
5+
6+
[dev]
7+
host = https://dev.cloud.databricks.com
8+
token = dev-pat-token
9+
10+
=== Logout without --delete — should report no changes for non-U2M profile
11+
>>> [CLI] auth logout --profile dev --force
12+
No tokens to clear for profile "dev". Use --delete to remove it from the config file.
13+
14+
=== Config after logout — profile should be unchanged
15+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
16+
[DEFAULT]
17+
18+
[dev]
19+
host = https://dev.cloud.databricks.com
20+
token = dev-pat-token
21+
22+
=== Logout with --delete — should delete the profile
23+
>>> [CLI] auth logout --profile dev --force --delete
24+
Deleted profile "dev" with no tokens to clear.
25+
26+
=== Backup file should exist
27+
OK: Backup file exists
28+
29+
=== Config after delete — profile should be removed
30+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
31+
[DEFAULT]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
sethome "./home"
2+
3+
cat > "./home/.databrickscfg" <<'EOF'
4+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
5+
[DEFAULT]
6+
7+
[dev]
8+
host = https://dev.cloud.databricks.com
9+
token = dev-pat-token
10+
EOF
11+
12+
title "Initial config\n"
13+
cat "./home/.databrickscfg"
14+
15+
title "Logout without --delete — should report no changes for non-U2M profile"
16+
trace $CLI auth logout --profile dev --force
17+
18+
title "Config after logout — profile should be unchanged\n"
19+
cat "./home/.databrickscfg"
20+
21+
title "Logout with --delete — should delete the profile"
22+
trace $CLI auth logout --profile dev --force --delete
23+
24+
title "Backup file should exist\n"
25+
assert_backup_exists
26+
27+
title "Config after delete — profile should be removed\n"
28+
cat "./home/.databrickscfg"

acceptance/cmd/auth/logout/error-cases/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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
=== Logout of non-existent profile
3+
Error: profile "nonexistent" not found. Available profiles: dev
4+
5+
Exit code: 1
6+
7+
=== Logout without --profile in non-interactive mode
8+
Error: the command is being run in a non-interactive environment, please specify a profile to log out of using --profile
9+
10+
Exit code: 1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sethome "./home"
2+
3+
cat > "./home/.databrickscfg" <<'EOF'
4+
; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified.
5+
[DEFAULT]
6+
7+
[dev]
8+
host = https://dev.cloud.databricks.com
9+
auth_type = databricks-cli
10+
EOF
11+
12+
title "Logout of non-existent profile\n"
13+
errcode $CLI auth logout --profile nonexistent --force
14+
15+
title "Logout without --profile in non-interactive mode\n"
16+
errcode $CLI auth logout --force

0 commit comments

Comments
 (0)