databricks-cli/bundle/deploy/files/delete.go

61 lines
1.3 KiB
Go
Raw Normal View History

package files
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/sync"
"github.com/databricks/databricks-sdk-go/service/workspace"
)
type delete struct{}
func (m *delete) Name() string {
return "files.Delete"
}
func (m *delete) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
Move to a single prompt during bundle destroy (#1583) ## Changes Right now we ask users for two confirmations when destroying a bundle. One to destroy the resources and one to delete the files. This PR consolidates the two prompts into one. ## Tests Manually Destroying a bundle with no resources: ``` ➜ bundle-playground git:(master) ✗ cli bundle destroy All files and directories at the following location will be deleted: /Users/shreyas.goenka@databricks.com/.bundle/bundle-playground/default Would you like to proceed? [y/n]: y No resources to destroy Updating deployment state... Deleting files... Destroy complete! ``` Destroying a bundle with no remote state: ``` ➜ bundle-playground git:(master) ✗ cli bundle destroy No active deployment found to destroy! ``` When a user cancells a deployment: ``` ➜ bundle-playground git:(master) ✗ cli bundle destroy The following resources will be deleted: delete job job_1 delete job job_2 delete pipeline foo All files and directories at the following location will be deleted: /Users/shreyas.goenka@databricks.com/.bundle/bundle-playground/default Would you like to proceed? [y/n]: n Destroy cancelled! ``` When a user destroys resources: ``` ➜ bundle-playground git:(master) ✗ cli bundle destroy The following resources will be deleted: delete job job_1 delete job job_2 delete pipeline foo All files and directories at the following location will be deleted: /Users/shreyas.goenka@databricks.com/.bundle/bundle-playground/default Would you like to proceed? [y/n]: y Updating deployment state... Deleting files... Destroy complete! ```
2024-07-24 13:02:19 +00:00
cmdio.LogString(ctx, "Deleting files...")
err := b.WorkspaceClient().Workspace.Delete(ctx, workspace.Delete{
Path: b.Config.Workspace.RootPath,
Recursive: true,
})
if err != nil {
return diag.FromErr(err)
}
// Clean up sync snapshot file
err = deleteSnapshotFile(ctx, b)
if err != nil {
return diag.FromErr(err)
}
return nil
}
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)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to destroy sync snapshot file: %s", err)
}
return nil
}
func Delete() bundle.Mutator {
return &delete{}
}