Fix `databricks configure` to use DATABRICKS_CONFIG_FILE environment variable if exists as config file (#1325)

## Changes
added `ConfigFile: cfg.ConfigFile` for `databrickscfg.SaveToProfile` in
`cmd/configure/configure.go` to save the file in a specified path when
the value is not empty

## Tests
`TestConfigFileFromEnvNoInteractive` in
`cmd/configure/configure_test.go` sets a different config file path by
`DATABRICKS_CONFIG_FILE`, after execution, the overwrite config file is
generated, and the default path has no file.
This commit is contained in:
Kai Zhu 2024-06-24 06:56:49 -04:00 committed by GitHub
parent 8957f1e7cf
commit 2ec6abf74e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View File

@ -143,6 +143,7 @@ The host must be specified with the --host flag or the DATABRICKS_HOST environme
Host: cfg.Host, Host: cfg.Host,
Token: cfg.Token, Token: cfg.Token,
ClusterID: cfg.ClusterID, ClusterID: cfg.ClusterID,
ConfigFile: cfg.ConfigFile,
}) })
} }

View File

@ -78,7 +78,8 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) {
//TODO: Replace with similar test code from go SDK, once we start using it directly //TODO: Replace with similar test code from go SDK, once we start using it directly
ctx := context.Background() ctx := context.Background()
tempHomeDir := setup(t) tempHomeDir := setup(t)
cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") defaultCfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
cfgPath := filepath.Join(tempHomeDir, "overwrite-databricks-cfg")
t.Setenv("DATABRICKS_CONFIG_FILE", cfgPath) t.Setenv("DATABRICKS_CONFIG_FILE", cfgPath)
inp := getTempFileWithContent(t, tempHomeDir, "token\n") inp := getTempFileWithContent(t, tempHomeDir, "token\n")
@ -96,6 +97,13 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) {
_, err = os.Stat(cfgPath) _, err = os.Stat(cfgPath)
assert.NoError(t, err) assert.NoError(t, err)
_, err = os.Stat(defaultCfgPath)
if runtime.GOOS == "windows" {
assert.ErrorContains(t, err, "cannot find the file specified")
} else {
assert.ErrorContains(t, err, "no such file or directory")
}
cfg, err := ini.Load(cfgPath) cfg, err := ini.Load(cfgPath)
assert.NoError(t, err) assert.NoError(t, err)