2023-09-04 09:55:01 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-09-04 09:55:01 +00:00
|
|
|
)
|
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
type artifactRewritePattern struct {
|
|
|
|
pattern dyn.Pattern
|
|
|
|
fn rewriteFunc
|
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
func (t *translateContext) artifactRewritePatterns() []artifactRewritePattern {
|
2024-03-18 16:23:39 +00:00
|
|
|
// Base pattern to match all artifacts.
|
|
|
|
base := dyn.NewPattern(
|
|
|
|
dyn.Key("artifacts"),
|
|
|
|
dyn.AnyKey(),
|
|
|
|
)
|
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
// Compile list of configuration paths to rewrite.
|
|
|
|
return []artifactRewritePattern{
|
2024-03-18 16:23:39 +00:00
|
|
|
{
|
|
|
|
base.Append(dyn.Key("path")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateNoOp,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
2024-06-25 10:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *translateContext) applyArtifactTranslations(v dyn.Value) (dyn.Value, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, rewritePattern := range t.artifactRewritePatterns() {
|
|
|
|
v, err = dyn.MapByPattern(v, rewritePattern.pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
2024-03-18 16:23:39 +00:00
|
|
|
key := p[1].Key()
|
|
|
|
dir, err := v.Location().Directory()
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for artifact %s: %w", key, err)
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
return t.rewriteRelativeTo(p, v, rewritePattern.fn, dir, "")
|
2024-03-18 16:23:39 +00:00
|
|
|
})
|
2023-09-04 09:55:01 +00:00
|
|
|
if err != nil {
|
2024-03-18 16:23:39 +00:00
|
|
|
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
|
|
|
}
|