Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 0 additions & 12 deletions bundle/config/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package config

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

"github.com/databricks/cli/bundle/env"
Expand Down Expand Up @@ -209,13 +207,3 @@ func (w *Workspace) Client(ctx context.Context) (*databricks.WorkspaceClient, er

return databricks.NewWorkspaceClient((*databricks.Config)(cfg))
}

func init() {
arg0 := os.Args[0]

// Configure DATABRICKS_CLI_PATH only if our caller intends to use this specific version of this binary.
// Otherwise, if it is equal to its basename, processes can find it in $PATH.
if arg0 != filepath.Base(arg0) {
os.Setenv("DATABRICKS_CLI_PATH", arg0)
}
}
16 changes: 10 additions & 6 deletions libs/databrickscfg/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"os"
"strings"
"sync"

"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/log"
Expand Down Expand Up @@ -357,7 +358,16 @@ func AuthCredentialKeys() []string {
return keys
}

// 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 is process-global, so it is enabled on first write instead of
// at import time to keep this package free of import side effects.
var enableDefaultHeader = sync.OnceFunc(func() {
ini.DefaultHeader = true
})

func writeConfigFile(ctx context.Context, configFile *config.File) error {
enableDefaultHeader()
section := configFile.Section(ini.DefaultSection)
if len(section.Keys()) == 0 && section.Comment == "" {
section.Comment = defaultComment
Expand Down Expand Up @@ -499,9 +509,3 @@ func ValidateConfigAndProfileHost(cfg *config.Config, profile string) error {

return nil
}

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
}
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"os"
"path/filepath"

"github.com/databricks/cli/cmd"
"github.com/databricks/cli/cmd/root"
Expand All @@ -13,6 +14,16 @@ import (
)

func main() {
// Configure DATABRICKS_CLI_PATH only if our caller intends to use this specific version of this binary.
// Otherwise, if it is equal to its basename, processes can find it in $PATH.
// This runs in main rather than in a package init so that importing CLI
// packages (e.g. from test binaries or generators) does not mutate the
// process environment.
arg0 := os.Args[0]
if arg0 != filepath.Base(arg0) {
os.Setenv("DATABRICKS_CLI_PATH", arg0)
}

ctx := context.Background()
err := root.Execute(ctx, cmd.New(ctx))
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"io/fs"
"os"
"path/filepath"
"testing"

"github.com/databricks/cli/cmd"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/mod/module"
)

Expand All @@ -26,6 +28,14 @@ func TestCommandsDontUseUnderscoreInName(t *testing.T) {
}
}

func TestImportDoesNotSetCliPathEnv(t *testing.T) {
// Test binaries run by their absolute path, which is exactly the condition
// under which main exports DATABRICKS_CLI_PATH; an import-time export would
// therefore have set it to this test binary's path by now.
require.NotEqual(t, filepath.Base(os.Args[0]), os.Args[0])
assert.NotEqual(t, os.Args[0], os.Getenv("DATABRICKS_CLI_PATH"))
}

func TestFilePath(t *testing.T) {
// To import this repository as a library, all files must match the
// file path constraints made by Go. This test ensures that all files
Expand Down
Loading