databricks-cli/cmd/fs/ls.go

78 lines
1.7 KiB
Go

package fs
import (
"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"
)
// lsCmd represents the ls command
var lsCmd = &cobra.Command{
Use: "ls <dir-name>",
Short: "Lists files",
Long: `Lists files`,
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 {
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() {
lsCmd.Flags().BoolVarP(&longMode, "long", "l", false, "Displays full information including size, file type and modification time since Epoch in milliseconds.")
fsCmd.AddCommand(lsCmd)
}