2022-11-30 13:15:22 +00:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
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-08-07 14:47:03 +00:00
|
|
|
var mutators []bundle.Mutator
|
|
|
|
|
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.
|
2024-08-07 14:47:03 +00:00
|
|
|
} else {
|
|
|
|
mutators = append(mutators, getBuildMutator(artifact.Type, m.name))
|
2024-07-16 08:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2024-08-07 14:47:03 +00:00
|
|
|
mutators = append(mutators, &expandGlobs{name: m.name})
|
|
|
|
return bundle.Apply(ctx, b, bundle.Seq(mutators...))
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|