mirror of https://github.com/databricks/cli.git
Add fs ls command for dbfs
This commit is contained in:
parent
1c0d67f66c
commit
b5f5f1103f
|
@ -10,7 +10,6 @@ var fsCmd = &cobra.Command{
|
||||||
Use: "fs",
|
Use: "fs",
|
||||||
Short: "Filesystem related commands",
|
Short: "Filesystem related commands",
|
||||||
Long: `Commands to do DBFS operations.`,
|
Long: `Commands to do DBFS operations.`,
|
||||||
Hidden: true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
64
cmd/fs/ls.go
64
cmd/fs/ls.go
|
@ -2,22 +2,76 @@ package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/cmd/root"
|
||||||
|
"github.com/databricks/cli/libs/cmdio"
|
||||||
|
"github.com/databricks/cli/libs/filer"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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-name>",
|
||||||
Short: "Lists files",
|
Short: "Lists files",
|
||||||
Long: `Lists files`,
|
Long: `Lists files`,
|
||||||
Hidden: true,
|
Args: cobra.ExactArgs(1),
|
||||||
|
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 {
|
||||||
return fmt.Errorf("TODO")
|
ctx := cmd.Context()
|
||||||
|
w := root.WorkspaceClient(ctx)
|
||||||
|
|
||||||
|
fileUri, err := url.Parse(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)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := f.ReadDir(ctx, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
lsOutputs := make([]lsOutput, 0)
|
||||||
|
for _, entry := range entries {
|
||||||
|
parsedEntry, err := toLsOutput(entry)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
lsOutputs = append(lsOutputs, *parsedEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use template for long mode if the flag is set
|
||||||
|
if longMode {
|
||||||
|
return cmdio.RenderWithTemplate(ctx, lsOutputs, "template_long")
|
||||||
|
}
|
||||||
|
return cmdio.Render(ctx, lsOutputs)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.")
|
||||||
fsCmd.AddCommand(lsCmd)
|
fsCmd.AddCommand(lsCmd)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
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
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ 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"
|
||||||
|
@ -27,13 +28,14 @@ func OutputType() flags.Output {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeIO(cmd *cobra.Command) error {
|
func initializeIO(cmd *cobra.Command) error {
|
||||||
var template string
|
templates := make(map[string]string, 0)
|
||||||
if cmd.Annotations != nil {
|
for k, v := range cmd.Annotations {
|
||||||
// rely on zeroval being an empty string
|
if strings.Contains(k, "template") {
|
||||||
template = cmd.Annotations["template"]
|
templates[k] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), template)
|
cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), templates)
|
||||||
ctx := cmdio.InContext(cmd.Context(), cmdIO)
|
ctx := cmdio.InContext(cmd.Context(), cmdIO)
|
||||||
cmd.SetContext(ctx)
|
cmd.SetContext(ctx)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
package internal
|
||||||
|
|
|
@ -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
|
||||||
template string
|
templates map[string]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, template string) *cmdIO {
|
func NewIO(outputFormat flags.Output, in io.Reader, out io.Writer, err io.Writer, templates map[string]string) *cmdIO {
|
||||||
return &cmdIO{
|
return &cmdIO{
|
||||||
interactive: !color.NoColor,
|
interactive: !color.NoColor,
|
||||||
outputFormat: outputFormat,
|
outputFormat: outputFormat,
|
||||||
template: template,
|
templates: templates,
|
||||||
in: in,
|
in: in,
|
||||||
out: out,
|
out: out,
|
||||||
err: err,
|
err: err,
|
||||||
|
@ -66,14 +66,14 @@ 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) error {
|
func (c *cmdIO) Render(v any, templateName string) error {
|
||||||
// TODO: add terminal width & white/dark theme detection
|
// TODO: add terminal width & white/dark theme detection
|
||||||
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.template != "" {
|
if c.templates[templateName] != "" {
|
||||||
return renderTemplate(c.out, c.template, v)
|
return renderTemplate(c.out, c.templates[templateName], v)
|
||||||
}
|
}
|
||||||
return renderJson(c.out, v)
|
return renderJson(c.out, v)
|
||||||
default:
|
default:
|
||||||
|
@ -83,7 +83,12 @@ func (c *cmdIO) Render(v any) error {
|
||||||
|
|
||||||
func Render(ctx context.Context, v any) error {
|
func Render(ctx context.Context, v any) error {
|
||||||
c := fromContext(ctx)
|
c := fromContext(ctx)
|
||||||
return c.Render(v)
|
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 }
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/nwidger/jsoncolor"
|
"github.com/nwidger/jsoncolor"
|
||||||
|
@ -86,6 +87,9 @@ 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 {
|
||||||
|
return t.UTC().Format("2006-01-02T15:04:05Z")
|
||||||
|
},
|
||||||
}).Parse(tmpl)
|
}).Parse(tmpl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -16,6 +16,8 @@ import (
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const DbfsScheme = "dbfs"
|
||||||
|
|
||||||
// Type that implements fs.DirEntry for DBFS.
|
// Type that implements fs.DirEntry for DBFS.
|
||||||
type dbfsDirEntry struct {
|
type dbfsDirEntry struct {
|
||||||
dbfsFileInfo
|
dbfsFileInfo
|
||||||
|
|
Loading…
Reference in New Issue