github.com/databricks/cli@v0.203.0/bundle/deploy/files/delete.go (about)

     1  package files
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/databricks/cli/bundle"
     8  	"github.com/databricks/cli/libs/cmdio"
     9  	"github.com/databricks/databricks-sdk-go/service/workspace"
    10  	"github.com/fatih/color"
    11  )
    12  
    13  type delete struct{}
    14  
    15  func (m *delete) Name() string {
    16  	return "files.Delete"
    17  }
    18  
    19  func (m *delete) Apply(ctx context.Context, b *bundle.Bundle) error {
    20  	// Do not delete files if terraform destroy was not consented
    21  	if !b.Plan.IsEmpty && !b.Plan.ConfirmApply {
    22  		return nil
    23  	}
    24  
    25  	cmdio.LogString(ctx, "Starting deletion of remote bundle files")
    26  	cmdio.LogString(ctx, fmt.Sprintf("Bundle remote directory is %s", b.Config.Workspace.RootPath))
    27  
    28  	red := color.New(color.FgRed).SprintFunc()
    29  	if !b.AutoApprove {
    30  		proceed, err := cmdio.Ask(ctx, fmt.Sprintf("\n%s and all files in it will be %s Proceed?", b.Config.Workspace.RootPath, red("deleted permanently!")))
    31  		if err != nil {
    32  			return err
    33  		}
    34  		if !proceed {
    35  			return nil
    36  		}
    37  	}
    38  
    39  	err := b.WorkspaceClient().Workspace.Delete(ctx, workspace.Delete{
    40  		Path:      b.Config.Workspace.RootPath,
    41  		Recursive: true,
    42  	})
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	// Clean up sync snapshot file
    48  	sync, err := getSync(ctx, b)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	err = sync.DestroySnapshot(ctx)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	cmdio.LogString(ctx, fmt.Sprintf("Deleted snapshot file at %s", sync.SnapshotPath()))
    58  	cmdio.LogString(ctx, "Successfully deleted files!")
    59  	return nil
    60  }
    61  
    62  func Delete() bundle.Mutator {
    63  	return &delete{}
    64  }