mirror of https://github.com/databricks/cli.git
added resolvedbfs path func
This commit is contained in:
parent
db953acb2f
commit
492382caa1
11
cmd/fs/ls.go
11
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
Loading…
Reference in New Issue