Allow setting the token cache directory without modifying HOME env via the DATABRICKS_TOKEN_CACHE_DIR env var

This commit is contained in:
Ben Phegan 2024-03-26 15:32:04 +11:00
parent fd8dbff631
commit 74f1989006
No known key found for this signature in database
GPG Key ID: A14E0006BF2E3E19
2 changed files with 17 additions and 0 deletions

View File

@ -74,6 +74,13 @@ func (c *TokenCache) Lookup(key string) (*oauth2.Token, error) {
} }
func (c *TokenCache) location() (string, error) { func (c *TokenCache) location() (string, error) {
// Allow users to override the location of the token cache file
tokenCacheDirName, exists := os.LookupEnv("DATABRICKS_TOKEN_CACHE_DIR")
if exists {
return filepath.Join(tokenCacheDirName, tokenCacheFile), nil
}
// Otherwise, default to using the home directory
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
return "", fmt.Errorf("home: %w", err) return "", fmt.Errorf("home: %w", err)

View File

@ -103,3 +103,13 @@ func TestStoreOnDev(t *testing.T) {
// macOS: read-only file system // macOS: read-only file system
assert.Error(t, err) assert.Error(t, err)
} }
func TestOverrideTokenCacheLocationUsingEnvVar(t *testing.T) {
t.Setenv("DATABRICKS_TOKEN_CACHE_DIR", os.TempDir())
c := &TokenCache{}
err := c.Store("x", &oauth2.Token{
AccessToken: "abc",
})
require.NoError(t, err)
assert.Contains(t, c.fileLocation, os.TempDir())
}