2023-10-04 13:23:13 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/libraries"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-03-11 21:59:36 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-10-04 13:23:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type expandPipelineGlobPaths struct{}
|
|
|
|
|
|
|
|
func ExpandPipelineGlobPaths() bundle.Mutator {
|
|
|
|
return &expandPipelineGlobPaths{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
func (m *expandPipelineGlobPaths) expandLibrary(v dyn.Value) ([]dyn.Value, error) {
|
|
|
|
// Probe for the path field in the library.
|
|
|
|
for _, p := range []dyn.Path{
|
|
|
|
dyn.NewPath(dyn.Key("notebook"), dyn.Key("path")),
|
|
|
|
dyn.NewPath(dyn.Key("file"), dyn.Key("path")),
|
|
|
|
} {
|
|
|
|
pv, err := dyn.GetByPath(v, p)
|
|
|
|
if dyn.IsNoSuchKeyError(err) {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-04 13:23:13 +00:00
|
|
|
if err != nil {
|
2024-03-11 21:59:36 +00:00
|
|
|
return nil, err
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
// If the path is empty or not a local path, return the original value.
|
|
|
|
path := pv.MustString()
|
|
|
|
if path == "" || !libraries.IsLocalPath(path) {
|
|
|
|
return []dyn.Value{v}, nil
|
|
|
|
}
|
2023-10-04 13:23:13 +00:00
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
dir, err := v.Location().Directory()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-04 13:23:13 +00:00
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
matches, err := filepath.Glob(filepath.Join(dir, path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-04 13:23:13 +00:00
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
// If there are no matches, return the original value.
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return []dyn.Value{v}, nil
|
|
|
|
}
|
2023-11-29 13:20:13 +00:00
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
// Emit a new value for each match.
|
|
|
|
var ev []dyn.Value
|
|
|
|
for _, match := range matches {
|
|
|
|
m, err := filepath.Rel(dir, match)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
2024-03-11 21:59:36 +00:00
|
|
|
nv, err := dyn.SetByPath(v, p, dyn.NewValue(m, pv.Location()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ev = append(ev, nv)
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
return ev, nil
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
// Neither of the library paths were found. This is likely an invalid node,
|
|
|
|
// but it isn't this mutator's job to enforce that. Return the original value.
|
|
|
|
return []dyn.Value{v}, nil
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
func (m *expandPipelineGlobPaths) expandSequence(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
|
|
|
s, ok := v.AsSequence()
|
|
|
|
if !ok {
|
|
|
|
return dyn.InvalidValue, fmt.Errorf("expected sequence, got %s", v.Kind())
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
var vs []dyn.Value
|
|
|
|
for _, sv := range s {
|
|
|
|
v, err := m.expandLibrary(sv)
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
2024-03-11 21:59:36 +00:00
|
|
|
|
|
|
|
vs = append(vs, v...)
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:59:36 +00:00
|
|
|
return dyn.NewValue(vs, v.Location()), nil
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *expandPipelineGlobPaths) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {
|
|
|
|
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
|
2024-03-11 21:59:36 +00:00
|
|
|
p := dyn.NewPattern(
|
|
|
|
dyn.Key("resources"),
|
|
|
|
dyn.Key("pipelines"),
|
|
|
|
dyn.AnyKey(),
|
|
|
|
dyn.Key("libraries"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Visit each pipeline's "libraries" field and expand any glob patterns.
|
|
|
|
return dyn.MapByPattern(v, p, m.expandSequence)
|
|
|
|
})
|
2024-03-25 14:18:47 +00:00
|
|
|
|
|
|
|
return diag.FromErr(err)
|
2023-10-04 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*expandPipelineGlobPaths) Name() string {
|
|
|
|
return "ExpandPipelineGlobPaths"
|
|
|
|
}
|