From 2ec6abf74ebde3752467849f0d8e3093b35fed66 Mon Sep 17 00:00:00 2001 From: Kai Zhu <87322035+kai-zhu-sonatype@users.noreply.github.com> Date: Mon, 24 Jun 2024 06:56:49 -0400 Subject: [PATCH] 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. --- cmd/configure/configure.go | 9 +++++---- cmd/configure/configure_test.go | 10 +++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/configure/configure.go b/cmd/configure/configure.go index 1e94ddae..895a5902 100644 --- a/cmd/configure/configure.go +++ b/cmd/configure/configure.go @@ -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, }) } diff --git a/cmd/configure/configure_test.go b/cmd/configure/configure_test.go index 259c83ad..a127fe57 100644 --- a/cmd/configure/configure_test.go +++ b/cmd/configure/configure_test.go @@ -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") @@ -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)