Skip to content

Commit

Permalink
Fix databricks configure to use DATABRICKS_CONFIG_FILE environment …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
kai-zhu-sonatype authored Jun 24, 2024
1 parent 8957f1e commit 2ec6abf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions cmd/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ The host must be specified with the --host flag or the DATABRICKS_HOST environme

// Save profile to config file.
return databrickscfg.SaveToProfile(ctx, &config.Config{
Profile: cfg.Profile,
Host: cfg.Host,
Token: cfg.Token,
ClusterID: cfg.ClusterID,
Profile: cfg.Profile,
Host: cfg.Host,
Token: cfg.Token,
ClusterID: cfg.ClusterID,
ConfigFile: cfg.ConfigFile,
})
}

Expand Down
10 changes: 9 additions & 1 deletion cmd/configure/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) {
//TODO: Replace with similar test code from go SDK, once we start using it directly
ctx := context.Background()
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)

inp := getTempFileWithContent(t, tempHomeDir, "token\n")
Expand All @@ -96,6 +97,13 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) {
_, err = os.Stat(cfgPath)
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)
assert.NoError(t, err)

Expand Down

0 comments on commit 2ec6abf

Please sign in to comment.