Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include [DEFAULT] section header when writing ~/.databrickscfg #464

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions libs/databrickscfg/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

const fileMode = 0o600

const defaultComment = "The profile defined in the DEFAULT section is be used as fallback when no profile is explicitly specified."
pietern marked this conversation as resolved.
Show resolved Hide resolved

func loadOrCreateConfigFile(filename string) (*config.File, error) {
if filename == "" {
filename = "~/.databrickscfg"
Expand Down Expand Up @@ -104,6 +106,12 @@ func SaveToProfile(ctx context.Context, cfg *config.Config) error {
key.SetValue(attr.GetString(cfg))
}

// Add a comment to the default section if it's empty.
section = configFile.Section(ini.DefaultSection)
if len(section.Keys()) == 0 && section.Comment == "" {
section.Comment = defaultComment
}

orig, backupErr := os.ReadFile(configFile.Path())
if len(orig) > 0 && backupErr == nil {
log.Infof(ctx, "Backing up in %s.bak", configFile.Path())
Expand All @@ -120,3 +128,9 @@ func SaveToProfile(ctx context.Context, cfg *config.Config) error {
}
return configFile.SaveTo(configFile.Path())
}

func init() {
// We document databrickscfg files with a [DEFAULT] header and wish to keep it that way.
// This, however, does mean we emit a [DEFAULT] section even if it's empty.
ini.DefaultHeader = true
}
37 changes: 36 additions & 1 deletion libs/databrickscfg/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package databrickscfg

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadOrCreate(t *testing.T) {
Expand Down Expand Up @@ -129,7 +131,7 @@ func TestSaveToProfile_ErrorOnMatch(t *testing.T) {
assert.Error(t, err)
}

func TestSaveToProfile_NewFile(t *testing.T) {
func TestSaveToProfile_NewFileWithoutDefault(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "databrickscfg")

Expand All @@ -141,6 +143,39 @@ func TestSaveToProfile_NewFile(t *testing.T) {
})
assert.NoError(t, err)
assert.NoFileExists(t, path+".bak")

contents, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t,
`; The profile defined in the DEFAULT section is be used as fallback when no profile is explicitly specified.
pietern marked this conversation as resolved.
Show resolved Hide resolved
[DEFAULT]

[abc]
host = https://foo
token = xyz
`, string(contents))
}

func TestSaveToProfile_NewFileWithDefault(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "databrickscfg")

err := SaveToProfile(ctx, &config.Config{
ConfigFile: path,
Profile: "DEFAULT",
Host: "https://foo",
Token: "xyz",
})
assert.NoError(t, err)
assert.NoFileExists(t, path+".bak")

contents, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t,
`[DEFAULT]
host = https://foo
token = xyz
`, string(contents))
}

func TestSaveToProfile_ClearingPreviousProfile(t *testing.T) {
Expand Down