2023-04-05 14:02:17 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-12 12:25:00 +00:00
|
|
|
"errors"
|
2023-04-05 14:02:17 +00:00
|
|
|
"fmt"
|
2023-09-04 09:55:01 +00:00
|
|
|
"net/url"
|
2023-04-05 14:02:17 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-03-18 16:23:39 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/notebook"
|
2023-04-05 14:02:17 +00:00
|
|
|
)
|
|
|
|
|
2023-07-12 12:25:00 +00:00
|
|
|
type ErrIsNotebook struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrIsNotebook) Error() string {
|
|
|
|
return fmt.Sprintf("file at %s is a notebook", err.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrIsNotNotebook struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrIsNotNotebook) Error() string {
|
|
|
|
return fmt.Sprintf("file at %s is not a notebook", err.path)
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:02:17 +00:00
|
|
|
type translatePaths struct {
|
2023-04-12 14:17:13 +00:00
|
|
|
seen map[string]string
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TranslatePaths converts paths to local notebook files into paths in the workspace file system.
|
|
|
|
func TranslatePaths() bundle.Mutator {
|
|
|
|
return &translatePaths{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *translatePaths) Name() string {
|
|
|
|
return "TranslatePaths"
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:55:01 +00:00
|
|
|
type rewriteFunc func(literal, localFullPath, localRelPath, remotePath string) (string, error)
|
|
|
|
|
|
|
|
// rewritePath converts a given relative path from the loaded config to a new path based on the passed rewriting function
|
2023-04-12 14:17:13 +00:00
|
|
|
//
|
|
|
|
// It takes these arguments:
|
|
|
|
// - The argument `dir` is the directory relative to which the given relative path is.
|
|
|
|
// - The given relative path is both passed and written back through `*p`.
|
|
|
|
// - The argument `fn` is a function that performs the actual rewriting logic.
|
|
|
|
// This logic is different between regular files or notebooks.
|
|
|
|
//
|
|
|
|
// The function returns an error if it is impossible to rewrite the given relative path.
|
|
|
|
func (m *translatePaths) rewritePath(
|
|
|
|
dir string,
|
|
|
|
b *bundle.Bundle,
|
|
|
|
p *string,
|
2023-09-04 09:55:01 +00:00
|
|
|
fn rewriteFunc,
|
2023-04-12 14:17:13 +00:00
|
|
|
) error {
|
2023-04-05 14:02:17 +00:00
|
|
|
// We assume absolute paths point to a location in the workspace
|
2023-04-12 14:17:13 +00:00
|
|
|
if path.IsAbs(filepath.ToSlash(*p)) {
|
2023-04-05 14:02:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:55:01 +00:00
|
|
|
url, err := url.Parse(*p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file path has scheme, it's a full path and we don't need to transform it
|
|
|
|
if url.Scheme != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
// Local path is relative to the directory the resource was defined in.
|
|
|
|
localPath := filepath.Join(dir, filepath.FromSlash(*p))
|
|
|
|
if interp, ok := m.seen[localPath]; ok {
|
2023-04-05 14:02:17 +00:00
|
|
|
*p = interp
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
// Remote path must be relative to the bundle root.
|
2024-03-27 09:03:24 +00:00
|
|
|
localRelPath, err := filepath.Rel(b.RootPath, localPath)
|
2023-04-12 14:17:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-04 09:55:01 +00:00
|
|
|
if strings.HasPrefix(localRelPath, "..") {
|
2023-04-12 14:17:13 +00:00
|
|
|
return fmt.Errorf("path %s is not contained in bundle root path", localPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix remote path with its remote root path.
|
2023-11-15 13:37:26 +00:00
|
|
|
remotePath := path.Join(b.Config.Workspace.FilePath, filepath.ToSlash(localRelPath))
|
2023-04-12 14:17:13 +00:00
|
|
|
|
2023-04-05 14:02:17 +00:00
|
|
|
// Convert local path into workspace path via specified function.
|
2023-09-04 09:55:01 +00:00
|
|
|
interp, err := fn(*p, localPath, localRelPath, filepath.ToSlash(remotePath))
|
2023-04-05 14:02:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = interp
|
2023-04-12 14:17:13 +00:00
|
|
|
m.seen[localPath] = interp
|
2023-04-05 14:02:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:55:01 +00:00
|
|
|
func translateNotebookPath(literal, localFullPath, localRelPath, remotePath string) (string, error) {
|
|
|
|
nb, _, err := notebook.Detect(localFullPath)
|
2023-04-05 14:02:17 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", fmt.Errorf("notebook %s not found", literal)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-04 09:55:01 +00:00
|
|
|
return "", fmt.Errorf("unable to determine if %s is a notebook: %w", localFullPath, err)
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
if !nb {
|
2023-09-04 09:55:01 +00:00
|
|
|
return "", ErrIsNotNotebook{localFullPath}
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upon import, notebooks are stripped of their extension.
|
2023-09-04 09:55:01 +00:00
|
|
|
return strings.TrimSuffix(remotePath, filepath.Ext(localFullPath)), nil
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:55:01 +00:00
|
|
|
func translateFilePath(literal, localFullPath, localRelPath, remotePath string) (string, error) {
|
|
|
|
nb, _, err := notebook.Detect(localFullPath)
|
2023-04-05 14:02:17 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", fmt.Errorf("file %s not found", literal)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-04 09:55:01 +00:00
|
|
|
return "", fmt.Errorf("unable to determine if %s is not a notebook: %w", localFullPath, err)
|
2023-07-12 12:25:00 +00:00
|
|
|
}
|
|
|
|
if nb {
|
2023-09-04 09:55:01 +00:00
|
|
|
return "", ErrIsNotebook{localFullPath}
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
2023-04-12 14:17:13 +00:00
|
|
|
return remotePath, nil
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 20:00:09 +00:00
|
|
|
func translateDirectoryPath(literal, localFullPath, localRelPath, remotePath string) (string, error) {
|
|
|
|
info, err := os.Stat(localFullPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return "", fmt.Errorf("%s is not a directory", localFullPath)
|
|
|
|
}
|
|
|
|
return remotePath, nil
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:55:01 +00:00
|
|
|
func translateNoOp(literal, localFullPath, localRelPath, remotePath string) (string, error) {
|
|
|
|
return localRelPath, nil
|
|
|
|
}
|
2023-04-05 14:02:17 +00:00
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
func (m *translatePaths) rewriteValue(b *bundle.Bundle, p dyn.Path, v dyn.Value, fn rewriteFunc, dir string) (dyn.Value, error) {
|
|
|
|
out := v.MustString()
|
|
|
|
err := m.rewritePath(dir, b, &out, fn)
|
|
|
|
if err != nil {
|
|
|
|
if target := (&ErrIsNotebook{}); errors.As(err, target) {
|
|
|
|
return dyn.InvalidValue, fmt.Errorf(`expected a file for "%s" but got a notebook: %w`, p, target)
|
|
|
|
}
|
|
|
|
if target := (&ErrIsNotNotebook{}); errors.As(err, target) {
|
|
|
|
return dyn.InvalidValue, fmt.Errorf(`expected a notebook for "%s" but got a file: %w`, p, target)
|
|
|
|
}
|
|
|
|
return dyn.InvalidValue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dyn.NewValue(out, v.Location()), nil
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
func (m *translatePaths) rewriteRelativeTo(b *bundle.Bundle, p dyn.Path, v dyn.Value, fn rewriteFunc, dir, fallback string) (dyn.Value, error) {
|
|
|
|
nv, err := m.rewriteValue(b, p, v, fn, dir)
|
|
|
|
if err == nil {
|
|
|
|
return nv, nil
|
|
|
|
}
|
2023-04-05 14:02:17 +00:00
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
// If we failed to rewrite the path, try to rewrite it relative to the fallback directory.
|
|
|
|
if fallback != "" {
|
|
|
|
nv, nerr := m.rewriteValue(b, p, v, fn, fallback)
|
|
|
|
if nerr == nil {
|
|
|
|
// TODO: Emit a warning that this path should be rewritten.
|
|
|
|
return nv, nil
|
2023-04-05 14:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
return dyn.InvalidValue, err
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *translatePaths) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-04-05 14:02:17 +00:00
|
|
|
m.seen = make(map[string]string)
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
|
2024-03-18 16:23:39 +00:00
|
|
|
var err error
|
|
|
|
for _, fn := range []func(*bundle.Bundle, dyn.Value) (dyn.Value, error){
|
|
|
|
m.applyJobTranslations,
|
|
|
|
m.applyPipelineTranslations,
|
|
|
|
m.applyArtifactTranslations,
|
|
|
|
} {
|
|
|
|
v, err = fn(b, v)
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
|
|
|
}
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
return v, nil
|
|
|
|
})
|
2024-03-25 14:18:47 +00:00
|
|
|
|
|
|
|
return diag.FromErr(err)
|
2023-04-05 14:02:17 +00:00
|
|
|
}
|