alias for fileinfo

This commit is contained in:
Shreyas Goenka 2023-05-26 17:56:30 +02:00
parent d876eaf619
commit c3bc6fd540
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 19 additions and 21 deletions

View File

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

View File

@ -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
}, },
"unix_date": func(t time.Time) string {
return t.UTC().Format(time.UnixDate)
},
}).Parse(tmpl) }).Parse(tmpl)
if err != nil { if err != nil {
return err return err