From 492382caa1eacdd6dcad2c12e09ff1501e46c3a9 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 5 Jun 2023 01:34:28 +0200 Subject: [PATCH] added resolvedbfs path func --- cmd/fs/ls.go | 11 ++--------- libs/filer/dbfs_client.go | 16 ++++++++++++++++ libs/filer/dbfs_client_test.go | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 libs/filer/dbfs_client_test.go diff --git a/cmd/fs/ls.go b/cmd/fs/ls.go index 828610d99..76b51ce7e 100644 --- a/cmd/fs/ls.go +++ b/cmd/fs/ls.go @@ -1,8 +1,6 @@ package fs import ( - "fmt" - "net/url" "sort" "github.com/databricks/cli/cmd/root" @@ -33,17 +31,12 @@ var lsCmd = &cobra.Command{ ctx := cmd.Context() w := root.WorkspaceClient(ctx) - fileUri, err := url.Parse(args[0]) + path, err := filer.ResolveDbfsPath(args[0]) if err != nil { return err } - // Only dbfs file scheme is supported - if fileUri.Scheme != filer.DbfsScheme { - return fmt.Errorf("expected dbfs path (with the dbfs:/ prefix): %s", args[0]) - } - - f, err := filer.NewDbfsClient(w, fileUri.Path) + f, err := filer.NewDbfsClient(w, path) if err != nil { return err } diff --git a/libs/filer/dbfs_client.go b/libs/filer/dbfs_client.go index 293ea3996..efdcdf31c 100644 --- a/libs/filer/dbfs_client.go +++ b/libs/filer/dbfs_client.go @@ -3,9 +3,11 @@ package filer import ( "context" "errors" + "fmt" "io" "io/fs" "net/http" + "net/url" "path" "sort" "time" @@ -272,3 +274,17 @@ func (w *DbfsClient) Stat(ctx context.Context, name string) (fs.FileInfo, error) return dbfsFileInfo{*info}, nil } + +func ResolveDbfsPath(path string) (string, error) { + fileUri, err := url.Parse(path) + if err != nil { + return "", err + } + + // Only dbfs file scheme is supported + if fileUri.Scheme != DbfsScheme { + return "", fmt.Errorf("expected dbfs path (with the dbfs:/ prefix): %s", path) + } + + return fileUri.Path, nil +} diff --git a/libs/filer/dbfs_client_test.go b/libs/filer/dbfs_client_test.go new file mode 100644 index 000000000..7aca0383d --- /dev/null +++ b/libs/filer/dbfs_client_test.go @@ -0,0 +1,35 @@ +package filer + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResolveDbfsPath(t *testing.T) { + path, err := ResolveDbfsPath("dbfs:/") + assert.NoError(t, err) + assert.Equal(t, "/", path) + + path, err = ResolveDbfsPath("dbfs:/abc") + assert.NoError(t, err) + assert.Equal(t, "/abc", path) + + path, err = ResolveDbfsPath("dbfs:/a/b/c") + assert.NoError(t, err) + assert.Equal(t, "/a/b/c", path) + + path, err = ResolveDbfsPath("dbfs:/a/b/.") + assert.NoError(t, err) + assert.Equal(t, "/a/b/.", path) + + path, err = ResolveDbfsPath("dbfs:/a/../c") + assert.NoError(t, err) + assert.Equal(t, "/a/../c", path) + + _, err = ResolveDbfsPath("dbf:/a/b/c") + assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): dbf:/a/b/c") + + _, err = ResolveDbfsPath("/a/b/c") + assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): /a/b/c") +}