Skip to content

Commit

Permalink
first look for databricks.yaml before falling back to bundle.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
fjakobs committed Jul 17, 2023
1 parent a7a109a commit 9e58857
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 11 deletions.
6 changes: 5 additions & 1 deletion bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ type Bundle struct {

func Load(path string) (*Bundle, error) {
bundle := &Bundle{}
err := bundle.Config.Load(filepath.Join(path, config.FileName))
configFile, err := config.FileNames.FindInPath(path)
if err != nil {
return nil, err
}
err = bundle.Config.Load(configFile)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions bundle/config/mutator/process_root_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func (m *processRootIncludes) Apply(ctx context.Context, b *bundle.Bundle) error
var out []bundle.Mutator

// Map with files we've already seen to avoid loading them twice.
var seen = map[string]bool{
config.FileName: true,
var seen = map[string]bool{}

for _, file := range config.FileNames {
seen[file] = true
}

// Maintain list of files in order of files being loaded.
Expand Down
32 changes: 29 additions & 3 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,31 @@ import (
"github.com/imdario/mergo"
)

// FileName is the name of bundle configuration file.
const FileName = "bundle.yml"
type ConfigFileNames [4]string

// FileNames is the name of bundle configuration file.
var FileNames = ConfigFileNames{"databricks.yaml", "databricks.yml", "bundle.yaml", "bundle.yml"}

func (c ConfigFileNames) FindInPath(path string) (string, error) {
result := ""
var err error = nil

for _, file := range c {
filePath := filepath.Join(path, file)
_, err = os.Stat(filePath)
if err == nil {
if result != "" {
return "", fmt.Errorf("multiple bundle configuration files found in %s", path)
}
result = filePath
}
}

if result == "" {
return "", err
}
return result, nil
}

type Root struct {
// Path contains the directory path to the root of the bundle.
Expand Down Expand Up @@ -62,7 +85,10 @@ func Load(path string) (*Root, error) {

// If we were given a directory, assume this is the bundle root.
if stat.IsDir() {
path = filepath.Join(path, FileName)
path, err = FileNames.FindInPath(path)
if err != nil {
return nil, err
}
}

if err := r.Load(path); err != nil {
Expand Down
52 changes: 52 additions & 0 deletions bundle/config/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package config

import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/databricks/cli/bundle/config/variable"
Expand Down Expand Up @@ -163,3 +166,52 @@ func TestRootMergeEnvironmentWithMode(t *testing.T) {
require.NoError(t, root.MergeEnvironment(env))
assert.Equal(t, Development, root.Bundle.Mode)
}

func TestConfigFileNames_FindInPath(t *testing.T) {
testCases := []struct {
name string
files []string
expected string
err string
}{
{
name: "file found",
files: []string{"databricks.yaml"},
expected: "BASE/databricks.yaml",
err: "",
},
{
name: "multiple files found",
files: []string{"databricks.yaml", "bundle.yml"},
expected: "",
err: "multiple bundle configuration files found",
},
{
name: "file not found",
files: []string{},
expected: "",
err: "no such file or directory",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
projectDir := t.TempDir()
for _, file := range tc.files {
f1, _ := os.Create(filepath.Join(projectDir, file))
f1.Close()
}

cfn := ConfigFileNames{"databricks.yaml", "databricks.yml", "bundle.yaml", "bundle.yml"}
result, err := cfn.FindInPath(projectDir)

if result != strings.Replace(tc.expected, "BASE", projectDir, 1) {
t.Errorf("expected %s, but got %s", tc.expected, result)
}

if err != nil && !strings.Contains(err.Error(), tc.err) {
t.Errorf("expected error %v, but got %v", tc.err, err)
}
})
}
}
12 changes: 8 additions & 4 deletions bundle/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ func getRootWithTraversal() (string, error) {
if err != nil {
return "", err
}
path, err := folders.FindDirWithLeaf(wd, config.FileName)
if err != nil {
return "", fmt.Errorf(`unable to locate bundle root: %s not found`, config.FileName)

for _, file := range config.FileNames {
path, err := folders.FindDirWithLeaf(wd, file)
if err == nil {
return path, nil
}
}
return path, nil

return "", fmt.Errorf(`unable to locate bundle root: %s not found`, config.FileNames[0])
}

// mustGetRoot returns a bundle root or an error if one cannot be found.
Expand Down
2 changes: 1 addition & 1 deletion bundle/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRootLookup(t *testing.T) {
chdir(t, t.TempDir())

// Create bundle.yml file.
f, err := os.Create(config.FileName)
f, err := os.Create(config.FileNames[0])
require.NoError(t, err)
defer f.Close()

Expand Down

0 comments on commit 9e58857

Please sign in to comment.