Merge branch 'fs-ls' into fs-rm

This commit is contained in:
Shreyas Goenka 2023-06-05 15:42:09 +02:00
commit 4b8f5746ef
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
8 changed files with 84 additions and 94 deletions

14
cmd/fs/helpers.go Normal file
View File

@ -0,0 +1,14 @@
package fs
import (
"fmt"
"strings"
)
func resolveDbfsPath(path string) (string, error) {
if !strings.HasPrefix(path, "dbfs:/") {
return "", fmt.Errorf("expected dbfs path (with the dbfs:/ prefix): %s", path)
}
return strings.TrimPrefix(path, "dbfs:"), nil
}

View File

@ -1,4 +1,4 @@
package filer package fs
import ( import (
"testing" "testing"
@ -7,32 +7,32 @@ import (
) )
func TestResolveDbfsPath(t *testing.T) { func TestResolveDbfsPath(t *testing.T) {
path, err := ResolveDbfsPath("dbfs:/") path, err := resolveDbfsPath("dbfs:/")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/", path) assert.Equal(t, "/", path)
path, err = ResolveDbfsPath("dbfs:/abc") path, err = resolveDbfsPath("dbfs:/abc")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/abc", path) assert.Equal(t, "/abc", path)
path, err = ResolveDbfsPath("dbfs:/a/b/c") path, err = resolveDbfsPath("dbfs:/a/b/c")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/a/b/c", path) assert.Equal(t, "/a/b/c", path)
path, err = ResolveDbfsPath("dbfs:/a/b/.") path, err = resolveDbfsPath("dbfs:/a/b/.")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/a/b/.", path) assert.Equal(t, "/a/b/.", path)
path, err = ResolveDbfsPath("dbfs:/a/../c") path, err = resolveDbfsPath("dbfs:/a/../c")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/a/../c", path) assert.Equal(t, "/a/../c", path)
_, err = ResolveDbfsPath("dbf:/a/b/c") _, err = resolveDbfsPath("dbf:/a/b/c")
assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): dbf:/a/b/c") assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): dbf:/a/b/c")
_, err = ResolveDbfsPath("/a/b/c") _, err = resolveDbfsPath("/a/b/c")
assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): /a/b/c") assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): /a/b/c")
_, err = ResolveDbfsPath("dbfs:a/b/c") _, err = resolveDbfsPath("dbfs:a/b/c")
assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): dbfs:a/b/c") assert.ErrorContains(t, err, "expected dbfs path (with the dbfs:/ prefix): dbfs:a/b/c")
} }

View File

@ -1,7 +1,9 @@
package fs package fs
import ( import (
"io/fs"
"sort" "sort"
"time"
"github.com/databricks/cli/cmd/root" "github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/cmdio"
@ -9,66 +11,83 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type jsonDirEntry struct {
Name string `json:"name"`
IsDir bool `json:"is_directory"`
Size int64 `json:"size"`
ModTime time.Time `json:"last_modified"`
}
func toJsonDirEntry(f fs.DirEntry) (*jsonDirEntry, error) {
info, err := f.Info()
if err != nil {
return nil, err
}
return &jsonDirEntry{
Name: f.Name(),
IsDir: f.IsDir(),
Size: info.Size(),
ModTime: info.ModTime(),
}, nil
}
// lsCmd represents the ls command // lsCmd represents the ls command
var lsCmd = &cobra.Command{ var lsCmd = &cobra.Command{
Use: "ls <dir-name>", Use: "ls DIR_PATH",
Short: "Lists files", Short: "Lists files",
Long: `Lists files`, Long: `Lists files`,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
PreRunE: root.MustWorkspaceClient, PreRunE: root.MustWorkspaceClient,
Annotations: map[string]string{
"template_long": cmdio.Heredoc(`
{{range .}}{{if .IsDir}}DIRECTORY {{else}}FILE {{end}}{{.Size}} {{.ModTime|pretty_date}} {{.Name}}
{{end}}
`),
"template": cmdio.Heredoc(`
{{range .}}{{.Name}}
{{end}}
`),
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context() ctx := cmd.Context()
w := root.WorkspaceClient(ctx) w := root.WorkspaceClient(ctx)
path, err := filer.ResolveDbfsPath(args[0]) path, err := resolveDbfsPath(args[0])
if err != nil { if err != nil {
return err return err
} }
f, err := filer.NewDbfsClient(w, path) f, err := filer.NewDbfsClient(w, "/")
if err != nil { if err != nil {
return err return err
} }
entries, err := f.ReadDir(ctx, "") entries, err := f.ReadDir(ctx, path)
if err != nil { if err != nil {
return err return err
} }
lsOutputs := make([]lsOutput, 0) jsonDirEntries := make([]jsonDirEntry, len(entries))
for _, entry := range entries { for i, entry := range entries {
parsedEntry, err := toLsOutput(entry) jsonDirEntry, err := toJsonDirEntry(entry)
if err != nil { if err != nil {
return err return err
} }
lsOutputs = append(lsOutputs, *parsedEntry) jsonDirEntries[i] = *jsonDirEntry
sort.Slice(lsOutputs, func(i, j int) bool {
return lsOutputs[i].Name < lsOutputs[j].Name
})
} }
sort.Slice(jsonDirEntries, func(i, j int) bool {
return jsonDirEntries[i].Name < jsonDirEntries[j].Name
})
// Use template for long mode if the flag is set // Use template for long mode if the flag is set
if longMode { if longMode {
return cmdio.RenderWithTemplate(ctx, lsOutputs, "template_long") return cmdio.RenderWithTemplate(ctx, jsonDirEntries, cmdio.Heredoc(`
{{range .}}{{if .IsDir}}DIRECTORY {{else}}FILE {{end}}{{.Size}} {{.ModTime|pretty_date}} {{.Name}}
{{end}}
`))
} }
return cmdio.Render(ctx, lsOutputs) return cmdio.RenderWithTemplate(ctx, jsonDirEntries, cmdio.Heredoc(`
{{range .}}{{.Name}}
{{end}}
`))
}, },
} }
var longMode bool var longMode 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.")
fsCmd.AddCommand(lsCmd) fsCmd.AddCommand(lsCmd)
} }

View File

@ -1,27 +0,0 @@
package fs
import (
"io/fs"
"time"
)
type lsOutput struct {
Name string `json:"name"`
IsDir bool `json:"is_directory"`
Size int64 `json:"size"`
ModTime time.Time `json:"last_modified"`
}
func toLsOutput(f fs.DirEntry) (*lsOutput, error) {
info, err := f.Info()
if err != nil {
return nil, err
}
return &lsOutput{
Name: f.Name(),
IsDir: f.IsDir(),
Size: info.Size(),
ModTime: info.ModTime(),
}, nil
}

View File

@ -2,7 +2,6 @@ package root
import ( import (
"os" "os"
"strings"
"github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/flags"
@ -28,14 +27,13 @@ func OutputType() flags.Output {
} }
func initializeIO(cmd *cobra.Command) error { func initializeIO(cmd *cobra.Command) error {
templates := make(map[string]string, 0) var template string
for k, v := range cmd.Annotations { if cmd.Annotations != nil {
if strings.Contains(k, "template") { // rely on zeroval being an empty string
templates[k] = v template = cmd.Annotations["template"]
}
} }
cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), templates) cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), template)
ctx := cmdio.InContext(cmd.Context(), cmdIO) ctx := cmdio.InContext(cmd.Context(), cmdIO)
cmd.SetContext(ctx) cmd.SetContext(ctx)

View File

@ -24,17 +24,17 @@ type cmdIO struct {
// e.g. if stdout is a terminal // e.g. if stdout is a terminal
interactive bool interactive bool
outputFormat flags.Output outputFormat flags.Output
templates map[string]string template string
in io.Reader in io.Reader
out io.Writer out io.Writer
err io.Writer err io.Writer
} }
func NewIO(outputFormat flags.Output, in io.Reader, out io.Writer, err io.Writer, templates map[string]string) *cmdIO { func NewIO(outputFormat flags.Output, in io.Reader, out io.Writer, err io.Writer, template string) *cmdIO {
return &cmdIO{ return &cmdIO{
interactive: !color.NoColor, interactive: !color.NoColor,
outputFormat: outputFormat, outputFormat: outputFormat,
templates: templates, template: template,
in: in, in: in,
out: out, out: out,
err: err, err: err,
@ -66,14 +66,20 @@ func (c *cmdIO) IsTTY() bool {
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd) return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
} }
func (c *cmdIO) Render(v any, templateName string) error { func Render(ctx context.Context, v any) error {
c := fromContext(ctx)
return RenderWithTemplate(ctx, v, c.template)
}
func RenderWithTemplate(ctx context.Context, v any, template string) error {
// TODO: add terminal width & white/dark theme detection // TODO: add terminal width & white/dark theme detection
c := fromContext(ctx)
switch c.outputFormat { switch c.outputFormat {
case flags.OutputJSON: case flags.OutputJSON:
return renderJson(c.out, v) return renderJson(c.out, v)
case flags.OutputText: case flags.OutputText:
if c.templates[templateName] != "" { if template != "" {
return renderTemplate(c.out, c.templates[templateName], v) return renderTemplate(c.out, template, v)
} }
return renderJson(c.out, v) return renderJson(c.out, v)
default: default:
@ -81,16 +87,6 @@ func (c *cmdIO) Render(v any, templateName string) error {
} }
} }
func Render(ctx context.Context, v any) error {
c := fromContext(ctx)
return c.Render(v, "template")
}
func RenderWithTemplate(ctx context.Context, v any, templateName string) error {
c := fromContext(ctx)
return c.Render(v, templateName)
}
type tuple struct{ Name, Id string } type tuple struct{ Name, Id string }
func (c *cmdIO) Select(names map[string]string, label string) (id string, err error) { func (c *cmdIO) Select(names map[string]string, label string) (id string, err error) {

View File

@ -88,7 +88,7 @@ func renderTemplate(w io.Writer, tmpl string, v any) error {
return string(b), nil return string(b), nil
}, },
"pretty_date": func(t time.Time) string { "pretty_date": func(t time.Time) string {
return t.UTC().Format("2006-01-02T15:04:05Z") return t.Format("2006-01-02T15:04:05Z")
}, },
}).Parse(tmpl) }).Parse(tmpl)
if err != nil { if err != nil {

View File

@ -3,13 +3,11 @@ package filer
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"io/fs" "io/fs"
"net/http" "net/http"
"path" "path"
"sort" "sort"
"strings"
"time" "time"
"github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go"
@ -272,11 +270,3 @@ func (w *DbfsClient) Stat(ctx context.Context, name string) (fs.FileInfo, error)
return dbfsFileInfo{*info}, nil return dbfsFileInfo{*info}, nil
} }
func ResolveDbfsPath(path string) (string, error) {
if !strings.HasPrefix(path, "dbfs:/") {
return "", fmt.Errorf("expected dbfs path (with the dbfs:/ prefix): %s", path)
}
return strings.TrimPrefix(path, "dbfs:"), nil
}