diff --git a/cmd/fs/ls.go b/cmd/fs/ls.go index eccdeacb..0adae4db 100644 --- a/cmd/fs/ls.go +++ b/cmd/fs/ls.go @@ -1,9 +1,7 @@ package fs import ( - "fmt" "path" - "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" @@ -11,17 +9,10 @@ import ( "github.com/spf13/cobra" ) -func parseFileInfo(info filer.FileInfo, parentDir string, isAbsolute bool) map[string]string { - fullName := info.Name - if isAbsolute { - fullName = path.Join(parentDir, info.Name) - } - return map[string]string{ - "Name": fullName, - "ModTime": info.ModTime.UTC().Format(time.UnixDate), - "Size": fmt.Sprint(info.Size), - "Type": info.Type, - } +type FileInfo filer.FileInfo + +func (i *FileInfo) ExpandPath(root string) { + i.Name = path.Join(root, i.Name) } // lsCmd represents the ls command @@ -41,21 +32,21 @@ var lsCmd = &cobra.Command{ `) if longMode { template = cmdio.Heredoc(` - {{range .}}{{.Type|printf "%-10s"}} {{.Size}} {{.ModTime}} {{.Name}} + {{range .}}{{.Type|printf "%-10s"}} {{.Size}} {{.ModTime|unix_date}} {{.Name}} {{end}} `) } // Path to list files from. Defaults to`/` - path := "/" + rootPath := "/" if len(args) > 0 { - path = args[0] + rootPath = args[0] } // Initialize workspace client ctx := cmd.Context() w := root.WorkspaceClient(ctx) - f, err := filer.NewWorkspaceFilesClient(w, path) + f, err := filer.NewWorkspaceFilesClient(w, rootPath) if err != nil { return err } @@ -66,10 +57,13 @@ var lsCmd = &cobra.Command{ return err } - // Parse it so it's ready to be rendered - output := make([]map[string]string, 0) - for _, info := range filesInfo { - output = append(output, parseFileInfo(info, path, absolute)) + // compute output with expanded paths if necessary + output := make([]FileInfo, len(filesInfo)) + for i, v := range filesInfo { + output[i] = FileInfo(v) + if absolute { + output[i].ExpandPath(rootPath) + } } return cmdio.RenderWithTemplate(ctx, output, template) }, diff --git a/libs/cmdio/render.go b/libs/cmdio/render.go index 8aff2b8d..70e26d71 100644 --- a/libs/cmdio/render.go +++ b/libs/cmdio/render.go @@ -6,6 +6,7 @@ import ( "strings" "text/tabwriter" "text/template" + "time" "github.com/fatih/color" "github.com/nwidger/jsoncolor" @@ -86,6 +87,9 @@ func renderTemplate(w io.Writer, tmpl string, v any) error { } return string(b), nil }, + "unix_date": func(t time.Time) string { + return t.UTC().Format(time.UnixDate) + }, }).Parse(tmpl) if err != nil { return err