-
Notifications
You must be signed in to change notification settings - Fork 154
Add fs rm command for dbfs #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b5f5f11
Add fs ls command for dbfs
shreyas-goenka a4879c0
added integration tests
shreyas-goenka 2706beb
lint
shreyas-goenka 6b667b5
added test for ls on flie
shreyas-goenka db953ac
Merge remote-tracking branch 'origin' into fs-ls
shreyas-goenka 492382c
added resolvedbfs path func
shreyas-goenka 3dc4770
manually check for dbfs prefix
shreyas-goenka 54a6602
Add fs rm command for dbfs
shreyas-goenka 19a2051
comments
shreyas-goenka 8050dc5
move sort outside
shreyas-goenka fc7fbbe
-
shreyas-goenka b048c86
address comments
shreyas-goenka 265e517
address comments 2
shreyas-goenka 0882609
added preallocation of size
shreyas-goenka 9160a6e
initialize dbfs client at root
shreyas-goenka 4b8f574
Merge branch 'fs-ls' into fs-rm
shreyas-goenka 25aa5b6
-
shreyas-goenka 8b4f22c
Merge remote-tracking branch 'origin' into fs-rm
shreyas-goenka b64b7ab
-
shreyas-goenka 5619d0f
-
shreyas-goenka da652c9
-
shreyas-goenka 188ed17
Merge remote-tracking branch 'origin' into fs-rm
shreyas-goenka dd9442e
Merge remote-tracking branch 'origin' into fs-rm
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package fs | ||
|
|
||
| import ( | ||
| "github.com/databricks/cli/cmd/root" | ||
| "github.com/databricks/databricks-sdk-go/service/files" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var rmCmd = &cobra.Command{ | ||
| Use: "rm PATH", | ||
| 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 := resolveDbfsPath(args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return w.Dbfs.Delete(ctx, files.Delete{ | ||
| Path: path, | ||
| Recursive: recursive, | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prompted me to do #442 so we can use the filer here as well for symmetry. Can be a follow up to this PR. |
||
| }, | ||
| } | ||
|
|
||
| var recursive bool | ||
|
|
||
| func init() { | ||
| rmCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "Recursively delete a non-empty directory.") | ||
| fsCmd.AddCommand(rmCmd) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 TestFsRmForNonExistentFile(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) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.