Skip to content

Commit c417dc7

Browse files
committed
Extract resolveProfile to simplify loadFromConfig
Move all profile resolution logic (explicit, __settings__, DEFAULT fallback) into a single resolveProfile method that returns the profile name and whether it is a silent fallback. This replaces the two-boolean pattern (hasExplicitProfile, hasDefaultProfileSetting) with a clearer isFallback flag, and keeps __settings__ knowledge out of loadFromConfig entirely. Also improves error messages when __settings__ is used as a profile name: now says "reserved section name" instead of the generic "has no __settings__ profile configured". Co-authored-by: Isaac
1 parent 5c59dec commit c417dc7

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/ConfigLoader.java

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,37 +93,22 @@ static void loadFromConfig(DatabricksConfig cfg) throws IllegalAccessException {
9393
INIConfiguration ini = parseDatabricksCfg(configFile, isDefaultConfig);
9494
if (ini == null) return;
9595

96-
String profile = cfg.getProfile();
97-
boolean hasExplicitProfile = !isNullOrEmpty(profile);
98-
boolean hasDefaultProfileSetting = false;
99-
if (!hasExplicitProfile) {
100-
SubnodeConfiguration settings = ini.getSection(SETTINGS_SECTION);
101-
if (settings != null && !settings.isEmpty()) {
102-
String defaultProfile = settings.getString("default_profile");
103-
if (defaultProfile != null) {
104-
defaultProfile = defaultProfile.trim();
105-
}
106-
if (!isNullOrEmpty(defaultProfile)) {
107-
profile = defaultProfile;
108-
hasDefaultProfileSetting = true;
109-
}
110-
}
111-
}
112-
if (!hasExplicitProfile && !hasDefaultProfileSetting) {
113-
profile = "DEFAULT";
114-
}
115-
SubnodeConfiguration section =
116-
SETTINGS_SECTION.equals(profile) ? null : ini.getSection(profile);
96+
String[] resolved = resolveProfile(cfg.getProfile(), ini, configFile.toString());
97+
String profile = resolved[0];
98+
boolean isFallback = "true".equals(resolved[1]);
99+
100+
SubnodeConfiguration section = ini.getSection(profile);
117101
boolean sectionNotPresent = section == null || section.isEmpty();
118-
if (sectionNotPresent && !hasExplicitProfile && !hasDefaultProfileSetting) {
119-
LOG.info("{} has no {} profile configured", configFile, profile);
120-
return;
121-
}
122102
if (sectionNotPresent) {
103+
if (isFallback) {
104+
LOG.info("{} has no {} profile configured", configFile, profile);
105+
return;
106+
}
123107
String msg = String.format("resolve: %s has no %s profile configured", configFile, profile);
124108
throw new DatabricksException(msg);
125109
}
126-
if (hasDefaultProfileSetting) {
110+
111+
if (!isFallback) {
127112
cfg.setProfile(profile);
128113
}
129114

@@ -136,6 +121,52 @@ static void loadFromConfig(DatabricksConfig cfg) throws IllegalAccessException {
136121
}
137122
}
138123

124+
/**
125+
* Resolves which profile to use from the config file.
126+
*
127+
* <p>Resolution order:
128+
*
129+
* <ol>
130+
* <li>Explicit profile (flag, env var, or programmatic config) with isFallback=false
131+
* <li>{@code [__settings__].default_profile} with isFallback=false
132+
* <li>{@code "DEFAULT"} with isFallback=true
133+
* </ol>
134+
*
135+
* @return a two-element array: [profileName, "true"/"false" for isFallback]
136+
* @throws DatabricksException if the resolved profile is the reserved __settings__ section
137+
*/
138+
static String[] resolveProfile(
139+
String requestedProfile, INIConfiguration ini, String configFile) {
140+
if (!isNullOrEmpty(requestedProfile)) {
141+
if (SETTINGS_SECTION.equals(requestedProfile)) {
142+
throw new DatabricksException(
143+
String.format(
144+
"%s: %s is a reserved section name and cannot be used as a profile",
145+
configFile, SETTINGS_SECTION));
146+
}
147+
return new String[] {requestedProfile, "false"};
148+
}
149+
150+
SubnodeConfiguration settings = ini.getSection(SETTINGS_SECTION);
151+
if (settings != null && !settings.isEmpty()) {
152+
String defaultProfile = settings.getString("default_profile");
153+
if (defaultProfile != null) {
154+
defaultProfile = defaultProfile.trim();
155+
}
156+
if (!isNullOrEmpty(defaultProfile)) {
157+
if (SETTINGS_SECTION.equals(defaultProfile)) {
158+
throw new DatabricksException(
159+
String.format(
160+
"%s: %s is a reserved section name and cannot be used as a profile",
161+
configFile, SETTINGS_SECTION));
162+
}
163+
return new String[] {defaultProfile, "false"};
164+
}
165+
}
166+
167+
return new String[] {"DEFAULT", "true"};
168+
}
169+
139170
private static INIConfiguration parseDatabricksCfg(String configFile, boolean isDefaultConfig) {
140171
INIConfiguration iniConfig = new INIConfiguration();
141172
try (FileReader reader = new FileReader(configFile)) {

databricks-sdk-java/src/test/java/com/databricks/sdk/DefaultProfileTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testSettingsSelfReferenceIsRejected() {
8787
config.authenticate();
8888
});
8989
assertTrue(
90-
ex.getMessage().contains("has no __settings__ profile configured"),
90+
ex.getMessage().contains("reserved section name"),
9191
"Error should reject __settings__ as a profile target: " + ex.getMessage());
9292
}
9393

@@ -122,7 +122,7 @@ public void testExplicitSettingsSectionProfileIsRejected() {
122122
config.authenticate();
123123
});
124124
assertTrue(
125-
ex.getMessage().contains("has no __settings__ profile configured"),
125+
ex.getMessage().contains("reserved section name"),
126126
"Error should reject __settings__ as a profile target: " + ex.getMessage());
127127
}
128128

0 commit comments

Comments
 (0)