2023-09-04 09:55:01 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-18 16:23:39 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-09-04 09:55:01 +00:00
|
|
|
)
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
func (m *translatePaths) applyPipelineTranslations(b *bundle.Bundle, v dyn.Value) (dyn.Value, error) {
|
|
|
|
var fallback = make(map[string]string)
|
|
|
|
var err error
|
2023-09-04 09:55:01 +00:00
|
|
|
|
|
|
|
for key, pipeline := range b.Config.Resources.Pipelines {
|
|
|
|
dir, err := pipeline.ConfigFileDirectory()
|
|
|
|
if err != nil {
|
2024-03-18 16:23:39 +00:00
|
|
|
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for pipeline %s: %w", key, err)
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
// If we cannot resolve the relative path using the [dyn.Value] location itself,
|
|
|
|
// use the pipeline's location as fallback. This is necessary for backwards compatibility.
|
|
|
|
fallback[key] = dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base pattern to match all libraries in all pipelines.
|
|
|
|
base := dyn.NewPattern(
|
|
|
|
dyn.Key("resources"),
|
|
|
|
dyn.Key("pipelines"),
|
|
|
|
dyn.AnyKey(),
|
|
|
|
dyn.Key("libraries"),
|
|
|
|
dyn.AnyIndex(),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, t := range []struct {
|
|
|
|
pattern dyn.Pattern
|
|
|
|
fn rewriteFunc
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("notebook"), dyn.Key("path")),
|
|
|
|
translateNotebookPath,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("file"), dyn.Key("path")),
|
|
|
|
translateFilePath,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
v, err = dyn.MapByPattern(v, t.pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
|
|
|
key := p[2].Key()
|
|
|
|
dir, err := v.Location().Directory()
|
2023-09-04 09:55:01 +00:00
|
|
|
if err != nil {
|
2024-03-18 16:23:39 +00:00
|
|
|
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for pipeline %s: %w", key, err)
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
|
|
|
return m.rewriteRelativeTo(b, p, v, t.fn, dir, fallback[key])
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
return v, nil
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|