mirror of https://github.com/databricks/cli.git
Add --absolute flag for ls command (#508)
tested manually --------- Co-authored-by: Pieter Noordhuis <pieter.noordhuis@databricks.com>
This commit is contained in:
parent
1f5cdfda29
commit
54148ffedf
14
cmd/fs/ls.go
14
cmd/fs/ls.go
|
@ -2,6 +2,7 @@ package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -17,14 +18,19 @@ type jsonDirEntry struct {
|
||||||
ModTime time.Time `json:"last_modified"`
|
ModTime time.Time `json:"last_modified"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func toJsonDirEntry(f fs.DirEntry) (*jsonDirEntry, error) {
|
func toJsonDirEntry(f fs.DirEntry, baseDir string, isAbsolute bool) (*jsonDirEntry, error) {
|
||||||
info, err := f.Info()
|
info, err := f.Info()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name := f.Name()
|
||||||
|
if isAbsolute {
|
||||||
|
name = path.Join(baseDir, name)
|
||||||
|
}
|
||||||
|
|
||||||
return &jsonDirEntry{
|
return &jsonDirEntry{
|
||||||
Name: f.Name(),
|
Name: name,
|
||||||
IsDir: f.IsDir(),
|
IsDir: f.IsDir(),
|
||||||
Size: info.Size(),
|
Size: info.Size(),
|
||||||
ModTime: info.ModTime(),
|
ModTime: info.ModTime(),
|
||||||
|
@ -54,7 +60,7 @@ var lsCmd = &cobra.Command{
|
||||||
|
|
||||||
jsonDirEntries := make([]jsonDirEntry, len(entries))
|
jsonDirEntries := make([]jsonDirEntry, len(entries))
|
||||||
for i, entry := range entries {
|
for i, entry := range entries {
|
||||||
jsonDirEntry, err := toJsonDirEntry(entry)
|
jsonDirEntry, err := toJsonDirEntry(entry, args[0], lsAbsolute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -79,8 +85,10 @@ var lsCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
var longMode bool
|
var longMode bool
|
||||||
|
var lsAbsolute bool
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
lsCmd.Flags().BoolVarP(&longMode, "long", "l", false, "Displays full information including size, file type and modification time since Epoch in milliseconds.")
|
lsCmd.Flags().BoolVarP(&longMode, "long", "l", false, "Displays full information including size, file type and modification time since Epoch in milliseconds.")
|
||||||
|
lsCmd.Flags().BoolVar(&lsAbsolute, "absolute", false, "Displays absolute paths.")
|
||||||
fsCmd.AddCommand(lsCmd)
|
fsCmd.AddCommand(lsCmd)
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ func TestFsLsForDbfs(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// assert on ls output
|
// assert on ls output
|
||||||
|
assert.Len(t, parsedStdout, 2)
|
||||||
assert.Equal(t, "a", parsedStdout[0]["name"])
|
assert.Equal(t, "a", parsedStdout[0]["name"])
|
||||||
assert.Equal(t, true, parsedStdout[0]["is_directory"])
|
assert.Equal(t, true, parsedStdout[0]["is_directory"])
|
||||||
assert.Equal(t, float64(0), parsedStdout[0]["size"])
|
assert.Equal(t, float64(0), parsedStdout[0]["size"])
|
||||||
|
@ -50,6 +51,42 @@ func TestFsLsForDbfs(t *testing.T) {
|
||||||
assert.Equal(t, float64(3), parsedStdout[1]["size"])
|
assert.Equal(t, float64(3), parsedStdout[1]["size"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFsLsForDbfsWithAbsolutePaths(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)
|
||||||
|
|
||||||
|
err = f.Mkdir(ctx, "a")
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = f.Write(ctx, "a/hello.txt", strings.NewReader("abc"), filer.CreateParentDirectories)
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = f.Write(ctx, "bye.txt", strings.NewReader("def"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "ls", "dbfs:"+tmpDir, "--output=json", "--absolute")
|
||||||
|
assert.Equal(t, "", stderr.String())
|
||||||
|
var parsedStdout []map[string]any
|
||||||
|
err = json.Unmarshal(stdout.Bytes(), &parsedStdout)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// assert on ls output
|
||||||
|
assert.Len(t, parsedStdout, 2)
|
||||||
|
assert.Equal(t, path.Join("dbfs:", tmpDir, "a"), parsedStdout[0]["name"])
|
||||||
|
assert.Equal(t, true, parsedStdout[0]["is_directory"])
|
||||||
|
assert.Equal(t, float64(0), parsedStdout[0]["size"])
|
||||||
|
|
||||||
|
assert.Equal(t, path.Join("dbfs:", tmpDir, "bye.txt"), parsedStdout[1]["name"])
|
||||||
|
assert.Equal(t, false, parsedStdout[1]["is_directory"])
|
||||||
|
assert.Equal(t, float64(3), parsedStdout[1]["size"])
|
||||||
|
}
|
||||||
|
|
||||||
func TestFsLsForDbfsOnFile(t *testing.T) {
|
func TestFsLsForDbfsOnFile(t *testing.T) {
|
||||||
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue