Fix "bundle init" when run from Databricks

This commit is contained in:
Fabian Jakobs 2024-09-03 14:46:52 +02:00
parent ca6332a5a4
commit 662234fb97
No known key found for this signature in database
2 changed files with 64 additions and 6 deletions

View File

@ -2,12 +2,16 @@ package template
import (
"context"
"encoding/base64"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/workspace"
)
// Interface representing a file to be materialized from a template into a project
@ -53,6 +57,10 @@ type copyFile struct {
srcPath string
}
func (f *copyFile) isNotebook() bool {
return strings.HasSuffix(f.DstPath().relPath, ".ipynb")
}
func (f *copyFile) DstPath() *destinationPath {
return f.dstPath
}
@ -68,13 +76,22 @@ func (f *copyFile) PersistToDisk() error {
return err
}
defer srcFile.Close()
dstFile, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, f.perm)
if err != nil {
if runsOnDatabricks() && f.isNotebook() {
content, err := io.ReadAll(srcFile)
if err != nil {
return err
}
return writeNotebook(path, content)
} else {
dstFile, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, f.perm)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
}
type inMemoryFile struct {
@ -86,6 +103,10 @@ type inMemoryFile struct {
perm fs.FileMode
}
func (f *inMemoryFile) isNotebook() bool {
return strings.HasSuffix(f.DstPath().relPath, ".ipynb")
}
func (f *inMemoryFile) DstPath() *destinationPath {
return f.dstPath
}
@ -97,5 +118,35 @@ func (f *inMemoryFile) PersistToDisk() error {
if err != nil {
return err
}
return os.WriteFile(path, f.content, f.perm)
if runsOnDatabricks() && f.isNotebook() {
return writeNotebook(path, f.content)
} else {
return os.WriteFile(path, f.content, f.perm)
}
}
func runsOnDatabricks() bool {
return os.Getenv("DATABRICKS_RUNTIME_VERSION") != ""
}
func writeNotebook(path string, content []byte) error {
if !strings.HasPrefix(path, "/Workspace/") {
return os.WriteFile(path, content, 0644)
} else {
w, err := databricks.NewWorkspaceClient()
if err != nil {
return err
}
ctx := context.Background()
err = w.Workspace.Import(ctx, workspace.Import{
Format: "AUTO",
Overwrite: false,
Path: path,
Content: base64.StdEncoding.EncodeToString(content),
})
return err
}
}

View File

@ -40,6 +40,13 @@ func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir st
return err
}
if outputDir == "" {
outputDir, err = os.Getwd()
if err != nil {
return err
}
}
templatePath := filepath.Join(templateRoot, templateDirName)
libraryPath := filepath.Join(templateRoot, libraryDirName)
schemaPath := filepath.Join(templateRoot, schemaFileName)