Skip to content

Commit b77aa73

Browse files
committed
section from config logic
1 parent 8ddc65e commit b77aa73

3 files changed

Lines changed: 58 additions & 28 deletions

File tree

cmd/auth/login.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ depends on the existing profiles you have set in your configuration file
140140
// Prior to v0.233.0, the login command would overwrite the entire profile configuration,
141141
// causing loss of cluster_id and other settings. Now we first retrieve the existing
142142
// cluster_id before saving the new configuration to ensure it's preserved.
143-
cfg.ClusterID, err = getClusterID(ctx, profileName, defaultConfigPath)
143+
cfg.ClusterID, err = getClusterID(ctx, profileName)
144144
if err != nil {
145145
return err
146146
}
@@ -254,20 +254,28 @@ func getProfileName(authArguments *auth.AuthArguments) string {
254254
return split[0]
255255
}
256256

257-
func getClusterID(ctx context.Context, profileName, configPath string) (string, error) {
258-
configFile, err := config.LoadFile(configPath)
257+
func getClusterID(ctx context.Context, profileName string) (string, error) {
258+
file, err := profile.DefaultProfiler.Get(ctx)
259259
if err != nil {
260-
return "", err
261-
}
262-
263-
if !configFile.HasSection(profileName) {
264-
return "", nil
260+
// If no configuration file exists, return empty string (no error)
261+
if errors.Is(err, profile.ErrNoConfiguration) {
262+
return "", nil
263+
}
264+
return "", fmt.Errorf("cannot load Databricks config file: %w", err)
265265
}
266266

267-
section, err := configFile.GetSection(profileName)
268-
if err != nil {
269-
return "", err
267+
for _, v := range file.Sections() {
268+
if v.Name() != profileName {
269+
continue
270+
}
271+
all := v.KeysHash()
272+
_, ok := all["host"]
273+
if !ok {
274+
// invalid profile, skip
275+
continue
276+
}
277+
return all["cluster_id"], nil
270278
}
271279

272-
return section.Key("cluster_id").String(), nil
280+
return "", nil
273281
}

cmd/auth/login_test.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,52 @@ func TestSetAccountId(t *testing.T) {
8787

8888
func TestLoginGetClusterID(t *testing.T) {
8989
testCases := []struct {
90-
name string
91-
profile string
92-
expected string
90+
name string
91+
profile string
92+
configFileEnv string
93+
expected string
9394
}{
9495
{
95-
name: "existing cluster profile",
96-
profile: "cluster-profile",
97-
expected: "cluster-from-config",
96+
name: "cluster profile",
97+
profile: "cluster-profile",
98+
configFileEnv: "./testdata/.databrickscfg",
99+
expected: "cluster-from-config",
98100
},
99101
{
100-
name: "empty profile",
101-
profile: "no-profile",
102-
expected: "",
102+
name: "empty profile",
103+
profile: "no-profile",
104+
configFileEnv: "./testdata/.databrickscfg",
105+
expected: "",
103106
},
104107
{
105-
name: "account profile",
106-
profile: "account-profile",
107-
expected: "",
108+
name: "account profile",
109+
profile: "account-profile",
110+
configFileEnv: "./testdata/.databrickscfg",
111+
expected: "",
112+
},
113+
{
114+
name: "config doesn't exist",
115+
profile: "any-profile",
116+
configFileEnv: "./nonexistent/.databrickscfg",
117+
expected: "",
118+
},
119+
{
120+
name: "invalid profile (missing host)",
121+
profile: "invalid-profile",
122+
configFileEnv: "./testdata/.databrickscfg",
123+
expected: "",
108124
},
109125
}
110126

111-
t.Setenv("DATABRICKS_CONFIG_FILE", "./testdata/.databrickscfg")
112-
113127
for _, tc := range testCases {
114128
t.Run(tc.name, func(t *testing.T) {
115-
clusterID, err := getClusterID(context.Background(), tc.profile, "./testdata/.databrickscfg")
129+
t.Setenv("DATABRICKS_CONFIG_FILE", tc.configFileEnv)
130+
131+
clusterID, err := getClusterID(context.Background(), tc.profile)
116132
require.NoError(t, err)
117-
assert.Equal(t, tc.expected, clusterID)
133+
assert.Equal(t, tc.expected, clusterID,
134+
"Test case '%s' failed: expected cluster ID '%s', but got '%s' (profile: %s, config file: %s)",
135+
tc.name, tc.expected, clusterID, tc.profile, tc.configFileEnv)
118136
})
119137
}
120138
}

cmd/auth/testdata/.databrickscfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ account_id = id-from-profile
1111
[cluster-profile]
1212
host = https://www.host2.com
1313
cluster_id = cluster-from-config
14+
15+
[invalid-profile]
16+
# This profile is missing the required 'host' field
17+
cluster_id = some-cluster-id

0 commit comments

Comments
 (0)