added resolvedbfs path func

This commit is contained in:
Shreyas Goenka 2023-06-05 01:34:28 +02:00
parent db953acb2f
commit 492382caa1
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 53 additions and 9 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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")
}