2023-09-04 09:55:01 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
2025-01-14 08:34:55 +00:00
|
|
|
"context"
|
2023-09-04 09:55:01 +00:00
|
|
|
"fmt"
|
2024-03-18 16:23:39 +00:00
|
|
|
"slices"
|
2023-09-04 09:55:01 +00:00
|
|
|
|
2024-09-24 13:51:54 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/mutator/paths"
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-09-04 09:55:01 +00:00
|
|
|
)
|
|
|
|
|
2025-01-14 08:34:55 +00:00
|
|
|
func (t *translateContext) applyJobTranslations(ctx context.Context, v dyn.Value) (dyn.Value, error) {
|
2024-06-25 10:04:22 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
fallback, err := gatherFallbackPaths(v, "jobs")
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not translate job task paths if using Git source
|
|
|
|
var ignore []string
|
|
|
|
for key, job := range t.b.Config.Resources.Jobs {
|
|
|
|
if job.GitSource != nil {
|
|
|
|
ignore = append(ignore, key)
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2024-09-24 13:51:54 +00:00
|
|
|
return paths.VisitJobPaths(v, func(p dyn.Path, kind paths.PathKind, v dyn.Value) (dyn.Value, error) {
|
|
|
|
key := p[2].Key()
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2024-09-24 13:51:54 +00:00
|
|
|
// Skip path translation if the job is using git source.
|
|
|
|
if slices.Contains(ignore, key) {
|
|
|
|
return v, nil
|
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2024-09-24 13:51:54 +00:00
|
|
|
dir, err := v.Location().Directory()
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for job %s: %w", key, err)
|
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2025-01-14 08:34:55 +00:00
|
|
|
mode, err := getJobTranslateMode(kind)
|
2024-03-18 16:23:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
2024-09-24 13:51:54 +00:00
|
|
|
|
2025-01-14 08:34:55 +00:00
|
|
|
opts := translateOptions{
|
|
|
|
Mode: mode,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to rewrite the path relative to the directory of the configuration file where the value was defined.
|
|
|
|
nv, err := t.rewriteValue(ctx, p, v, dir, opts)
|
|
|
|
if err == nil {
|
|
|
|
return nv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we failed to rewrite the path, try to rewrite it relative to the fallback directory.
|
|
|
|
// We only do this for jobs and pipelines because of the comment in [gatherFallbackPaths].
|
|
|
|
if fallback[key] != "" {
|
|
|
|
nv, nerr := t.rewriteValue(ctx, p, v, fallback[key], opts)
|
|
|
|
if nerr == nil {
|
|
|
|
// TODO: Emit a warning that this path should be rewritten.
|
|
|
|
return nv, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dyn.InvalidValue, err
|
2024-09-24 13:51:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-01-14 08:34:55 +00:00
|
|
|
func getJobTranslateMode(kind paths.PathKind) (TranslateMode, error) {
|
2024-09-24 13:51:54 +00:00
|
|
|
switch kind {
|
|
|
|
case paths.PathKindLibrary:
|
2025-01-14 08:34:55 +00:00
|
|
|
return TranslateModeLocalRelative, nil
|
2024-09-24 13:51:54 +00:00
|
|
|
case paths.PathKindNotebook:
|
2025-01-14 08:34:55 +00:00
|
|
|
return TranslateModeNotebook, nil
|
2024-09-24 13:51:54 +00:00
|
|
|
case paths.PathKindWorkspaceFile:
|
2025-01-14 08:34:55 +00:00
|
|
|
return TranslateModeFile, nil
|
2024-09-24 13:51:54 +00:00
|
|
|
case paths.PathKindDirectory:
|
2025-01-14 08:34:55 +00:00
|
|
|
return TranslateModeDirectory, nil
|
2024-09-24 13:51:54 +00:00
|
|
|
case paths.PathKindWithPrefix:
|
2025-01-14 08:34:55 +00:00
|
|
|
return TranslateModeLocalRelativeWithPrefix, nil
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
|
|
|
|
2025-01-14 08:34:55 +00:00
|
|
|
return TranslateMode(0), fmt.Errorf("unsupported path kind: %d", kind)
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|