2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-09-11 08:18:43 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/bundle/env"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/folders"
|
2022-11-18 09:57:31 +00:00
|
|
|
)
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
// getRootEnv returns the value of the bundle root environment variable
|
2023-01-27 15:57:39 +00:00
|
|
|
// if it set and is a directory. If the environment variable is set but
|
|
|
|
// is not a directory, it returns an error. If the environment variable is
|
|
|
|
// not set, it returns an empty string.
|
2023-09-11 08:18:43 +00:00
|
|
|
func getRootEnv(ctx context.Context) (string, error) {
|
|
|
|
path, ok := env.Root(ctx)
|
2023-01-27 15:57:39 +00:00
|
|
|
if !ok {
|
|
|
|
return "", nil
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2023-01-27 15:57:39 +00:00
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err == nil && !stat.IsDir() {
|
|
|
|
err = fmt.Errorf("not a directory")
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-11 08:18:43 +00:00
|
|
|
return "", fmt.Errorf(`invalid bundle root %s="%s": %w`, env.RootVariable, path, err)
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRootWithTraversal returns the bundle root by traversing the filesystem
|
|
|
|
// from the working directory to the root looking for a configuration file.
|
|
|
|
func getRootWithTraversal() (string, error) {
|
2022-11-18 09:57:31 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-07-18 10:16:34 +00:00
|
|
|
|
|
|
|
for _, file := range config.FileNames {
|
|
|
|
path, err := folders.FindDirWithLeaf(wd, file)
|
|
|
|
if err == nil {
|
|
|
|
return path, nil
|
|
|
|
}
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
2023-07-18 10:16:34 +00:00
|
|
|
|
|
|
|
return "", fmt.Errorf(`unable to locate bundle root: %s not found`, config.FileNames[0])
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mustGetRoot returns a bundle root or an error if one cannot be found.
|
2023-09-11 08:18:43 +00:00
|
|
|
func mustGetRoot(ctx context.Context) (string, error) {
|
|
|
|
path, err := getRootEnv(ctx)
|
2023-01-27 15:57:39 +00:00
|
|
|
if path != "" || err != nil {
|
|
|
|
return path, err
|
|
|
|
}
|
|
|
|
return getRootWithTraversal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// tryGetRoot returns a bundle root or an empty string if one cannot be found.
|
2023-09-11 08:18:43 +00:00
|
|
|
func tryGetRoot(ctx context.Context) (string, error) {
|
2023-01-27 15:57:39 +00:00
|
|
|
// Note: an invalid value in the environment variable is still an error.
|
2023-09-11 08:18:43 +00:00
|
|
|
path, err := getRootEnv(ctx)
|
2023-01-27 15:57:39 +00:00
|
|
|
if path != "" || err != nil {
|
|
|
|
return path, err
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2023-01-27 15:57:39 +00:00
|
|
|
// Note: traversal failing means the bundle root cannot be found.
|
|
|
|
path, _ = getRootWithTraversal()
|
2022-11-18 09:57:31 +00:00
|
|
|
return path, nil
|
|
|
|
}
|