Add fs rm command for dbfs

This commit is contained in:
Shreyas Goenka 2023-06-05 03:07:19 +02:00
parent 3dc47709d0
commit 54a6602aae
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 184 additions and 0 deletions

38
cmd/fs/rm.go Normal file
View File

@ -0,0 +1,38 @@
package fs
import (
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/databricks-sdk-go/service/files"
"github.com/spf13/cobra"
)
var rmCmd = &cobra.Command{
Use: "rm <dir-name>",
Short: "Remove files and directories from dbfs.",
Long: `Remove files and directories from dbfs.`,
Args: cobra.ExactArgs(1),
PreRunE: root.MustWorkspaceClient,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
w := root.WorkspaceClient(ctx)
path, err := filer.ResolveDbfsPath(args[0])
if err != nil {
return err
}
return w.Dbfs.Delete(ctx, files.Delete{
Path: path,
Recursive: recursive,
})
},
}
var recursive bool
func init() {
rmCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "Allow deletion of non-empty directories.")
fsCmd.AddCommand(rmCmd)
}

146
internal/rm_test.go Normal file
View File

@ -0,0 +1,146 @@
package internal
import (
"context"
"io/fs"
"path"
"strings"
"testing"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/databricks-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFsRmForFile(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
ctx := context.Background()
w, err := databricks.NewWorkspaceClient()
require.NoError(t, err)
tmpDir := temporaryDbfsDir(t, w)
f, err := filer.NewDbfsClient(w, tmpDir)
require.NoError(t, err)
// create file to delete
err = f.Write(ctx, "hello.txt", strings.NewReader("abc"))
require.NoError(t, err)
// check file was created
info, err := f.Stat(ctx, "hello.txt")
require.NoError(t, err)
require.Equal(t, "hello.txt", info.Name())
require.Equal(t, info.IsDir(), false)
// Run rm command
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", "dbfs:"+path.Join(tmpDir, "hello.txt"))
assert.Equal(t, "", stderr.String())
assert.Equal(t, "", stdout.String())
// assert file was deleted
_, err = f.Stat(ctx, "hello.txt")
assert.ErrorIs(t, err, fs.ErrNotExist)
}
func TestFsRmForEmptyDirectory(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
ctx := context.Background()
w, err := databricks.NewWorkspaceClient()
require.NoError(t, err)
tmpDir := temporaryDbfsDir(t, w)
f, err := filer.NewDbfsClient(w, tmpDir)
require.NoError(t, err)
// create directory to delete
err = f.Mkdir(ctx, "avacado")
require.NoError(t, err)
// check directory was created
info, err := f.Stat(ctx, "avacado")
require.NoError(t, err)
require.Equal(t, "avacado", info.Name())
require.Equal(t, info.IsDir(), true)
// Run rm command
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", "dbfs:"+path.Join(tmpDir, "avacado"))
assert.Equal(t, "", stderr.String())
assert.Equal(t, "", stdout.String())
// assert directory was deleted
_, err = f.Stat(ctx, "avacado")
assert.ErrorIs(t, err, fs.ErrNotExist)
}
func TestFsRmForNonEmptyDirectory(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
ctx := context.Background()
w, err := databricks.NewWorkspaceClient()
require.NoError(t, err)
tmpDir := temporaryDbfsDir(t, w)
f, err := filer.NewDbfsClient(w, tmpDir)
require.NoError(t, err)
// create file in dir
err = f.Write(ctx, "avacado/guacamole", strings.NewReader("abc"), filer.CreateParentDirectories)
require.NoError(t, err)
// check file was created
info, err := f.Stat(ctx, "avacado/guacamole")
require.NoError(t, err)
require.Equal(t, "guacamole", info.Name())
require.Equal(t, info.IsDir(), false)
// Run rm command
_, _, err = RequireErrorRun(t, "fs", "rm", "dbfs:"+path.Join(tmpDir, "avacado"))
assert.ErrorContains(t, err, "Non-recursive delete of non-empty directory")
}
func TestFsRmForNonExistantFile(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
// No error is returned on command run
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", "dbfs:/does-not-exist")
assert.Equal(t, "", stderr.String())
assert.Equal(t, "", stdout.String())
}
func TestFsRmForNonEmptyDirectoryWithRecursiveFlag(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
ctx := context.Background()
w, err := databricks.NewWorkspaceClient()
require.NoError(t, err)
tmpDir := temporaryDbfsDir(t, w)
f, err := filer.NewDbfsClient(w, tmpDir)
require.NoError(t, err)
// create file in dir
err = f.Write(ctx, "avacado/guacamole", strings.NewReader("abc"), filer.CreateParentDirectories)
require.NoError(t, err)
// check file was created
info, err := f.Stat(ctx, "avacado/guacamole")
require.NoError(t, err)
require.Equal(t, "guacamole", info.Name())
require.Equal(t, info.IsDir(), false)
// Run rm command
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", "dbfs:"+path.Join(tmpDir, "avacado"), "--recursive")
assert.Equal(t, "", stderr.String())
assert.Equal(t, "", stdout.String())
// assert directory was deleted
_, err = f.Stat(ctx, "avacado")
assert.ErrorIs(t, err, fs.ErrNotExist)
}