databricks-cli/cmd/fs/ls.go

78 lines
1.7 KiB
Go
Raw Normal View History

2022-05-14 17:54:35 +00:00
package fs
2022-05-13 14:21:47 +00:00
import (
"fmt"
2023-06-02 15:24:04 +00:00
"net/url"
2022-05-13 14:21:47 +00:00
2023-06-02 15:24:04 +00:00
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/filer"
2022-05-13 14:21:47 +00:00
"github.com/spf13/cobra"
)
// lsCmd represents the ls command
var lsCmd = &cobra.Command{
2023-06-02 15:24:04 +00:00
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 {
2023-06-02 15:24:04 +00:00
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)
2022-05-13 14:21:47 +00:00
},
}
2023-06-02 15:24:04 +00:00
var longMode bool
2022-05-13 14:21:47 +00:00
func init() {
2023-06-02 15:24:04 +00:00
lsCmd.Flags().BoolVarP(&longMode, "long", "l", false, "Displays full information including size, file type and modification time since Epoch in milliseconds.")
2022-05-13 14:21:47 +00:00
fsCmd.AddCommand(lsCmd)
}