diff --git a/cmd/auth/login.go b/cmd/auth/login.go index 28e0025d..8c6d52fc 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -106,8 +106,13 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command { } if profileName != "" { - cfg.Profile = profileName - err = databrickscfg.SaveToProfile(ctx, &cfg) + err = databrickscfg.SaveToProfile(ctx, &config.Config{ + Profile: profileName, + Host: cfg.Host, + AuthType: cfg.AuthType, + AccountID: cfg.AccountID, + ClusterID: cfg.ClusterID, + }) if err != nil { return err } diff --git a/cmd/configure/configure.go b/cmd/configure/configure.go index 33ab918e..55ede538 100644 --- a/cmd/configure/configure.go +++ b/cmd/configure/configure.go @@ -160,7 +160,11 @@ func newConfigureCommand() *cobra.Command { cfg.DatabricksCliPath = "" // Save profile to config file. - return databrickscfg.SaveToProfile(ctx, &cfg) + return databrickscfg.SaveToProfile(ctx, &config.Config{ + Profile: cfg.Profile, + Host: cfg.Host, + Token: cfg.Token, + }) } return cmd diff --git a/cmd/configure/configure_test.go b/cmd/configure/configure_test.go index cf0505ed..259c83ad 100644 --- a/cmd/configure/configure_test.go +++ b/cmd/configure/configure_test.go @@ -106,6 +106,72 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) { assertKeyValueInSection(t, defaultSection, "token", "token") } +func TestEnvVarsConfigureNoInteractive(t *testing.T) { + ctx := context.Background() + tempHomeDir := setup(t) + cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") + inp := getTempFileWithContent(t, tempHomeDir, "token\n") + defer inp.Close() + oldStdin := os.Stdin + t.Cleanup(func() { os.Stdin = oldStdin }) + os.Stdin = inp + + t.Setenv("DATABRICKS_HOST", "https://host") + t.Setenv("DATABRICKS_AUTH_TYPE", "metadata-service") + t.Setenv("DATABRICKS_METADATA_SERVICE_URL", "https://metadata") + + cmd := cmd.New(ctx) + cmd.SetArgs([]string{"configure", "--token"}) + + err := cmd.ExecuteContext(ctx) + assert.NoError(t, err) + + _, err = os.Stat(cfgPath) + assert.NoError(t, err) + + cfg, err := ini.Load(cfgPath) + assert.NoError(t, err) + + defaultSection, err := cfg.GetSection("DEFAULT") + assert.NoError(t, err) + + assertKeyValueInSection(t, defaultSection, "host", "https://host") + assertKeyValueInSection(t, defaultSection, "token", "token") + + // We should only save host and token for a profile, other env variables should not be saved + _, err = defaultSection.GetKey("auth_type") + assert.NotNil(t, err) + _, err = defaultSection.GetKey("metadata_service_url") + assert.NotNil(t, err) +} + +func TestEnvVarsConfigureNoArgsNoInteractive(t *testing.T) { + ctx := context.Background() + tempHomeDir := setup(t) + cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") + + t.Setenv("DATABRICKS_HOST", "https://host") + t.Setenv("DATABRICKS_TOKEN", "secret") + + cmd := cmd.New(ctx) + cmd.SetArgs([]string{"configure"}) + + err := cmd.ExecuteContext(ctx) + assert.NoError(t, err) + + _, err = os.Stat(cfgPath) + assert.NoError(t, err) + + cfg, err := ini.Load(cfgPath) + assert.NoError(t, err) + + defaultSection, err := cfg.GetSection("DEFAULT") + assert.NoError(t, err) + + assertKeyValueInSection(t, defaultSection, "host", "https://host") + assertKeyValueInSection(t, defaultSection, "token", "secret") +} + func TestCustomProfileConfigureNoInteractive(t *testing.T) { ctx := context.Background() tempHomeDir := setup(t)