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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2024-07-16 08:57:04 +00:00
|
|
|
|
|
|
|
// We can skip calling build mutator if there is no build command
|
|
|
|
// But we still need to expand glob references in files source path.
|
|
|
|
diags := expandGlobReference(artifact)
|
|
|
|
return diags
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 08:57:04 +00:00
|
|
|
diags := bundle.Apply(ctx, b, getBuildMutator(artifact.Type, m.name))
|
|
|
|
if diags.HasError() {
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to expand glob reference after build mutator is applied because
|
|
|
|
// if we do it before, any files that are generated by build command will
|
|
|
|
// not be included into artifact.Files and thus will not be uploaded.
|
|
|
|
d := expandGlobReference(artifact)
|
|
|
|
return diags.Extend(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandGlobReference(artifact *config.Artifact) diag.Diagnostics {
|
|
|
|
var diags diag.Diagnostics
|
|
|
|
|
|
|
|
// 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 diags.Extend(diag.Errorf("unable to find files for %s: %v", f.Source, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return diags.Extend(diag.Errorf("no files found for %s", f.Source))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
files = append(files, config.ArtifactFile{
|
|
|
|
Source: match,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact.Files = files
|
|
|
|
return diags
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|