2022-11-30 13:15:22 +00:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-03-04 20:28:15 +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-03-04 20:28:15 +00:00
|
|
|
"github.com/databricks/cli/bundle/config"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-07-25 11:35:08 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
2022-11-30 13:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func UploadAll() bundle.Mutator {
|
|
|
|
return &all{
|
|
|
|
name: "Upload",
|
|
|
|
fn: uploadArtifactByName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:35:08 +00:00
|
|
|
func CleanUp() bundle.Mutator {
|
|
|
|
return &cleanUp{}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:15:22 +00:00
|
|
|
type upload struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadArtifactByName(name string) (bundle.Mutator, error) {
|
|
|
|
return &upload{name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *upload) Name() string {
|
|
|
|
return fmt.Sprintf("artifacts.Upload(%s)", m.name)
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *upload) 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-25 11:35:08 +00:00
|
|
|
if len(artifact.Files) == 0 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("artifact source is not configured: %s", m.name)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:28:15 +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 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("unable to find files for %s: %v", f.Source, err)
|
2024-03-04 20:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("no files found for %s", f.Source)
|
2024-03-04 20:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
files = append(files, config.ArtifactFile{
|
|
|
|
Source: match,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact.Files = files
|
2023-07-25 11:35:08 +00:00
|
|
|
return bundle.Apply(ctx, b, getUploadMutator(artifact.Type, m.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
type cleanUp struct{}
|
|
|
|
|
|
|
|
func (m *cleanUp) Name() string {
|
|
|
|
return "artifacts.CleanUp"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *cleanUp) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-07-25 11:35:08 +00:00
|
|
|
uploadPath, err := getUploadBasePath(b)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-07-25 11:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.WorkspaceClient().Workspace.Delete(ctx, workspace.Delete{
|
|
|
|
Path: uploadPath,
|
|
|
|
Recursive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
err = b.WorkspaceClient().Workspace.MkdirsByPath(ctx, uploadPath)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("unable to create directory for %s: %v", uploadPath, err)
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-11-30 13:15:22 +00:00
|
|
|
}
|