2022-05-14 17:54:35 +00:00
|
|
|
package fs
|
2022-05-13 14:21:47 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-05 15:41:30 +00:00
|
|
|
"io/fs"
|
2023-06-27 14:06:56 +00:00
|
|
|
"path"
|
2023-06-05 15:41:30 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
2022-05-13 14:21:47 +00:00
|
|
|
|
2023-06-05 15:41:30 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2022-05-13 14:21:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-06-05 15:41:30 +00:00
|
|
|
type jsonDirEntry struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
IsDir bool `json:"is_directory"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
ModTime time.Time `json:"last_modified"`
|
|
|
|
}
|
|
|
|
|
2023-06-27 14:06:56 +00:00
|
|
|
func toJsonDirEntry(f fs.DirEntry, baseDir string, isAbsolute bool) (*jsonDirEntry, error) {
|
2023-06-05 15:41:30 +00:00
|
|
|
info, err := f.Info()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-27 14:06:56 +00:00
|
|
|
name := f.Name()
|
|
|
|
if isAbsolute {
|
|
|
|
name = path.Join(baseDir, name)
|
|
|
|
}
|
|
|
|
|
2023-06-05 15:41:30 +00:00
|
|
|
return &jsonDirEntry{
|
2023-06-27 14:06:56 +00:00
|
|
|
Name: name,
|
2023-06-05 15:41:30 +00:00
|
|
|
IsDir: f.IsDir(),
|
|
|
|
Size: info.Size(),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func newLsCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls DIR_PATH",
|
|
|
|
Short: "Lists files",
|
|
|
|
Long: `Lists files`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
PreRunE: root.MustWorkspaceClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
var long bool
|
|
|
|
var absolute bool
|
|
|
|
cmd.Flags().BoolVarP(&long, "long", "l", false, "Displays full information including size, file type and modification time since Epoch in milliseconds.")
|
|
|
|
cmd.Flags().BoolVar(&absolute, "absolute", false, "Displays absolute paths.")
|
2022-09-16 09:06:58 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2023-06-05 15:41:30 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
|
2023-06-16 15:09:08 +00:00
|
|
|
f, path, err := filerForPath(ctx, args[0])
|
2023-06-05 15:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := f.ReadDir(ctx, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonDirEntries := make([]jsonDirEntry, len(entries))
|
|
|
|
for i, entry := range entries {
|
2023-07-27 10:03:08 +00:00
|
|
|
jsonDirEntry, err := toJsonDirEntry(entry, args[0], absolute)
|
2023-06-05 15:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
jsonDirEntries[i] = *jsonDirEntry
|
|
|
|
}
|
|
|
|
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
|
2023-07-27 10:03:08 +00:00
|
|
|
if long {
|
2023-06-05 15:41:30 +00:00
|
|
|
return cmdio.RenderWithTemplate(ctx, jsonDirEntries, cmdio.Heredoc(`
|
|
|
|
{{range .}}{{if .IsDir}}DIRECTORY {{else}}FILE {{end}}{{.Size}} {{.ModTime|pretty_date}} {{.Name}}
|
|
|
|
{{end}}
|
|
|
|
`))
|
|
|
|
}
|
|
|
|
return cmdio.RenderWithTemplate(ctx, jsonDirEntries, cmdio.Heredoc(`
|
|
|
|
{{range .}}{{.Name}}
|
|
|
|
{{end}}
|
|
|
|
`))
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
2023-06-05 15:41:30 +00:00
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
2022-05-13 14:21:47 +00:00
|
|
|
}
|