2022-11-30 13:15:22 +00:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-07-25 11:35:08 +00:00
|
|
|
"path/filepath"
|
2022-11-30 13:15:22 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-04-12 16:00:42 +00:00
|
|
|
"github.com/databricks/cli/bundle/config"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2022-11-30 13:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BuildAll() bundle.Mutator {
|
|
|
|
return &all{
|
|
|
|
name: "Build",
|
|
|
|
fn: buildArtifactByName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type build struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildArtifactByName(name string) (bundle.Mutator, error) {
|
|
|
|
return &build{name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *build) Name() string {
|
|
|
|
return fmt.Sprintf("artifacts.Build(%s)", m.name)
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *build) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2022-11-30 13:15:22 +00:00
|
|
|
artifact, ok := b.Config.Artifacts[m.name]
|
|
|
|
if !ok {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("artifact doesn't exist: %s", m.name)
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:00:42 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand any glob reference in files source path
|
|
|
|
files := make([]config.ArtifactFile, 0, len(artifact.Files))
|
|
|
|
for _, f := range artifact.Files {
|
|
|
|
matches, err := filepath.Glob(f.Source)
|
|
|
|
if err != nil {
|
|
|
|
return diag.Errorf("unable to find files for %s: %v", f.Source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return diag.Errorf("no files found for %s", f.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
files = append(files, config.ArtifactFile{
|
|
|
|
Source: match,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact.Files = files
|
|
|
|
|
2023-07-26 10:07:26 +00:00
|
|
|
// Skip building if build command is not specified or infered
|
|
|
|
if artifact.BuildCommand == "" {
|
|
|
|
// If no build command was specified or infered and there is no
|
|
|
|
// artifact output files specified, artifact is misconfigured
|
|
|
|
if len(artifact.Files) == 0 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("misconfigured artifact: please specify 'build' or 'files' property")
|
2023-07-26 10:07:26 +00:00
|
|
|
}
|
2023-07-25 11:35:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If artifact path is not provided, use bundle root dir
|
|
|
|
if artifact.Path == "" {
|
2024-03-27 09:03:24 +00:00
|
|
|
artifact.Path = b.RootPath
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !filepath.IsAbs(artifact.Path) {
|
2024-03-04 20:28:15 +00:00
|
|
|
dirPath := filepath.Dir(artifact.ConfigFilePath)
|
|
|
|
artifact.Path = filepath.Join(dirPath, artifact.Path)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bundle.Apply(ctx, b, getBuildMutator(artifact.Type, m.name))
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|