2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2022-11-30 13:40:41 +00:00
|
|
|
"os"
|
2022-11-18 09:57:31 +00:00
|
|
|
"path/filepath"
|
2022-11-23 14:20:03 +00:00
|
|
|
"sync"
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle/config"
|
2022-11-24 20:41:57 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
2022-11-18 09:57:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bundle struct {
|
|
|
|
Config config.Root
|
2022-11-23 14:20:03 +00:00
|
|
|
|
|
|
|
// Store a pointer to the workspace client.
|
|
|
|
// It can be initialized on demand after loading the configuration.
|
|
|
|
clientOnce sync.Once
|
2022-11-24 20:41:57 +00:00
|
|
|
client *databricks.WorkspaceClient
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load(path string) (*Bundle, error) {
|
|
|
|
bundle := &Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := bundle.Config.Load(filepath.Join(path, config.FileName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bundle, nil
|
|
|
|
}
|
2022-11-21 14:39:53 +00:00
|
|
|
|
|
|
|
func LoadFromRoot() (*Bundle, error) {
|
|
|
|
root, err := getRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Load(root)
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:41:57 +00:00
|
|
|
func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient {
|
2022-11-23 14:20:03 +00:00
|
|
|
b.clientOnce.Do(func() {
|
2022-11-24 20:41:57 +00:00
|
|
|
var err error
|
|
|
|
b.client, err = b.Config.Workspace.Client()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-23 14:20:03 +00:00
|
|
|
})
|
|
|
|
return b.client
|
|
|
|
}
|
2022-11-30 13:40:41 +00:00
|
|
|
|
|
|
|
var cacheDirName = filepath.Join(".databricks", "bundle")
|
|
|
|
|
|
|
|
// CacheDir returns directory to use for temporary files for this bundle.
|
|
|
|
// Scoped to the bundle's environment.
|
|
|
|
func (b *Bundle) CacheDir() (string, error) {
|
|
|
|
if b.Config.Bundle.Environment == "" {
|
|
|
|
panic("environment not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make directory if it doesn't exist yet.
|
|
|
|
dir := filepath.Join(b.Config.Path, cacheDirName, b.Config.Bundle.Environment)
|
|
|
|
err := os.MkdirAll(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|