Make `databricks configure` save only explicit fields (#973)

## Changes
Save only explicit fields to the config file
This applies to two commands: `configure` and `auth login`. 
The latter only pulls env vars in the case of the `--configure-cluster`
flag

## Tests
Manual, plus additional unit test for the `configure` command
This commit is contained in:
Ilia Babanov 2023-11-10 15:03:57 +01:00 committed by GitHub
parent f208853626
commit e82a49b4e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 3 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)