2525public class CliTokenSource implements TokenSource {
2626 private static final Logger LOG = LoggerFactory .getLogger (CliTokenSource .class );
2727
28- private List <String > cmd ;
28+ private static final String UNKNOWN_PROFILE_FLAG = "unknown flag: --profile" ;
29+ private static final String UNKNOWN_FORCE_REFRESH_FLAG = "unknown flag: --force-refresh" ;
30+
31+ // forceCmd is tried before profileCmd when non-null. If the CLI rejects
32+ // --force-refresh or --profile, execution falls through to profileCmd.
33+ private List <String > forceCmd ;
34+
35+ private List <String > profileCmd ;
2936 private String tokenTypeField ;
3037 private String accessTokenField ;
3138 private String expiryField ;
3239 private Environment env ;
33- // fallbackCmd is tried when the primary command fails with "unknown flag: --profile",
34- // indicating the CLI is too old to support --profile. Can be removed once support
35- // for CLI versions predating --profile is dropped.
36- // See: https://github.com/databricks/databricks-sdk-go/pull/1497
40+ // fallbackCmd is tried when profileCmd fails with "unknown flag: --profile",
41+ // indicating the CLI is too old to support --profile.
3742 private List <String > fallbackCmd ;
3843
3944 /**
@@ -58,7 +63,7 @@ public CliTokenSource(
5863 String accessTokenField ,
5964 String expiryField ,
6065 Environment env ) {
61- this (cmd , tokenTypeField , accessTokenField , expiryField , env , null );
66+ this (cmd , tokenTypeField , accessTokenField , expiryField , env , null , null );
6267 }
6368
6469 public CliTokenSource (
@@ -68,14 +73,27 @@ public CliTokenSource(
6873 String expiryField ,
6974 Environment env ,
7075 List <String > fallbackCmd ) {
76+ this (cmd , tokenTypeField , accessTokenField , expiryField , env , fallbackCmd , null );
77+ }
78+
79+ public CliTokenSource (
80+ List <String > cmd ,
81+ String tokenTypeField ,
82+ String accessTokenField ,
83+ String expiryField ,
84+ Environment env ,
85+ List <String > fallbackCmd ,
86+ List <String > forceCmd ) {
7187 super ();
72- this .cmd = OSUtils .get (env ).getCliExecutableCommand (cmd );
88+ this .profileCmd = OSUtils .get (env ).getCliExecutableCommand (cmd );
7389 this .tokenTypeField = tokenTypeField ;
7490 this .accessTokenField = accessTokenField ;
7591 this .expiryField = expiryField ;
7692 this .env = env ;
7793 this .fallbackCmd =
7894 fallbackCmd != null ? OSUtils .get (env ).getCliExecutableCommand (fallbackCmd ) : null ;
95+ this .forceCmd =
96+ forceCmd != null ? OSUtils .get (env ).getCliExecutableCommand (forceCmd ) : null ;
7997 }
8098
8199 /**
@@ -153,18 +171,22 @@ private Token execCliCommand(List<String> cmdToRun) throws IOException {
153171 }
154172 }
155173
156- @ Override
157- public Token getToken () {
174+ private String getErrorText (IOException e ) {
175+ return e instanceof CliCommandException
176+ ? ((CliCommandException ) e ).getFullOutput ()
177+ : e .getMessage ();
178+ }
179+
180+ private boolean isUnknownFlagError (String errorText , String flag ) {
181+ return errorText != null && errorText .contains (flag );
182+ }
183+
184+ private Token execProfileCmdWithFallback () {
158185 try {
159- return execCliCommand (this .cmd );
186+ return execCliCommand (this .profileCmd );
160187 } catch (IOException e ) {
161- String textToCheck =
162- e instanceof CliCommandException
163- ? ((CliCommandException ) e ).getFullOutput ()
164- : e .getMessage ();
165- if (fallbackCmd != null
166- && textToCheck != null
167- && textToCheck .contains ("unknown flag: --profile" )) {
188+ String textToCheck = getErrorText (e );
189+ if (fallbackCmd != null && isUnknownFlagError (textToCheck , UNKNOWN_PROFILE_FLAG )) {
168190 LOG .warn (
169191 "Databricks CLI does not support --profile flag. Falling back to --host. "
170192 + "Please upgrade your CLI to the latest version." );
@@ -177,4 +199,26 @@ public Token getToken() {
177199 throw new DatabricksException (e .getMessage (), e );
178200 }
179201 }
202+
203+ @ Override
204+ public Token getToken () {
205+ if (forceCmd == null ) {
206+ return execProfileCmdWithFallback ();
207+ }
208+
209+ try {
210+ return execCliCommand (this .forceCmd );
211+ } catch (IOException e ) {
212+ String textToCheck = getErrorText (e );
213+ if (isUnknownFlagError (textToCheck , UNKNOWN_FORCE_REFRESH_FLAG )
214+ || isUnknownFlagError (textToCheck , UNKNOWN_PROFILE_FLAG )) {
215+ LOG .warn (
216+ "Databricks CLI does not support --force-refresh flag. "
217+ + "Falling back to regular token fetch. "
218+ + "Please upgrade your CLI to the latest version." );
219+ return execProfileCmdWithFallback ();
220+ }
221+ throw new DatabricksException (e .getMessage (), e );
222+ }
223+ }
180224}
0 commit comments