databricks-cli/bundle/artifacts/prepare.go

58 lines
1.2 KiB
Go

package artifacts
import (
"context"
"fmt"
"path/filepath"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
)
func PrepareAll() bundle.Mutator {
return &all{
name: "Prepare",
fn: prepareArtifactByName,
}
}
type prepare struct {
name string
}
func prepareArtifactByName(name string) (bundle.Mutator, error) {
return &prepare{name}, nil
}
func (m *prepare) Name() string {
return fmt.Sprintf("artifacts.Prepare(%s)", m.name)
}
func (m *prepare) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
artifact, ok := b.Config.Artifacts[m.name]
if !ok {
return diag.Errorf("artifact doesn't exist: %s", m.name)
}
// Check if source paths are absolute, if not, make them absolute
for k := range artifact.Files {
f := &artifact.Files[k]
if !filepath.IsAbs(f.Source) {
dirPath := filepath.Dir(artifact.ConfigFilePath)
f.Source = filepath.Join(dirPath, f.Source)
}
}
// If artifact path is not provided, use bundle root dir
if artifact.Path == "" {
artifact.Path = b.RootPath
}
if !filepath.IsAbs(artifact.Path) {
dirPath := filepath.Dir(artifact.ConfigFilePath)
artifact.Path = filepath.Join(dirPath, artifact.Path)
}
return bundle.Apply(ctx, b, getPrepareMutator(artifact.Type, m.name))
}