@@ -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 )) {
0 commit comments