2023-04-06 10:54:58 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-03 12:39:36 +00:00
|
|
|
"errors"
|
2023-04-06 10:54:58 +00:00
|
|
|
"fmt"
|
2024-06-03 12:39:36 +00:00
|
|
|
"io/fs"
|
2024-04-19 11:48:04 +00:00
|
|
|
"os"
|
2023-04-06 10:54:58 +00:00
|
|
|
|
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"
|
2024-04-19 11:48:04 +00:00
|
|
|
"github.com/databricks/cli/libs/sync"
|
2023-04-06 10:54:58 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2024-07-24 13:02:19 +00:00
|
|
|
cmdio.LogString(ctx, "Deleting files...")
|
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-19 11:48:04 +00:00
|
|
|
err = deleteSnapshotFile(ctx, 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
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 11:48:04 +00:00
|
|
|
func deleteSnapshotFile(ctx context.Context, b *bundle.Bundle) error {
|
|
|
|
opts, err := GetSyncOptions(ctx, bundle.ReadOnly(b))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot get sync options: %w", err)
|
|
|
|
}
|
|
|
|
sp, err := sync.SnapshotPath(opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Remove(sp)
|
2024-06-03 12:39:36 +00:00
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2024-04-19 11:48:04 +00:00
|
|
|
return fmt.Errorf("failed to destroy sync snapshot file: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
func Delete() bundle.Mutator {
|
|
|
|
return &delete{}
|
|
|
|
}
|