2023-04-06 10:54:58 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-04-06 10:54:58 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
type delete struct{}
|
|
|
|
|
|
|
|
func (m *delete) Name() string {
|
|
|
|
return "files.Delete"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *delete) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-04-06 10:54:58 +00:00
|
|
|
// Do not delete files if terraform destroy was not consented
|
|
|
|
if !b.Plan.IsEmpty && !b.Plan.ConfirmApply {
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 14:55:06 +00:00
|
|
|
cmdio.LogString(ctx, "Starting deletion of remote bundle files")
|
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("Bundle remote directory is %s", b.Config.Workspace.RootPath))
|
2023-04-06 10:54:58 +00:00
|
|
|
|
2023-04-18 14:55:06 +00:00
|
|
|
red := color.New(color.FgRed).SprintFunc()
|
2023-04-06 10:54:58 +00:00
|
|
|
if !b.AutoApprove {
|
2023-08-15 14:50:20 +00:00
|
|
|
proceed, err := cmdio.AskYesOrNo(ctx, fmt.Sprintf("\n%s and all files in it will be %s Proceed?", b.Config.Workspace.RootPath, red("deleted permanently!")))
|
2023-04-06 10:54:58 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
if !proceed {
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := b.WorkspaceClient().Workspace.Delete(ctx, workspace.Delete{
|
2023-04-12 14:54:36 +00:00
|
|
|
Path: b.Config.Workspace.RootPath,
|
2023-04-06 10:54:58 +00:00
|
|
|
Recursive: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:57:01 +00:00
|
|
|
// Clean up sync snapshot file
|
2024-04-18 15:13:16 +00:00
|
|
|
sync, err := GetSync(ctx, bundle.ReadOnly(b))
|
2023-04-11 14:57:01 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-04-11 14:57:01 +00:00
|
|
|
}
|
|
|
|
err = sync.DestroySnapshot(ctx)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-04-11 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 14:55:06 +00:00
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("Deleted snapshot file at %s", sync.SnapshotPath()))
|
|
|
|
cmdio.LogString(ctx, "Successfully deleted files!")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Delete() bundle.Mutator {
|
|
|
|
return &delete{}
|
|
|
|
}
|