This commit is contained in:
Shreyas Goenka 2023-06-05 13:49:33 +02:00
parent 3dc47709d0
commit 19a20512e9
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 46 additions and 57 deletions

View File

@ -1,7 +1,9 @@
package fs
import (
"io/fs"
"sort"
"time"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
@ -9,6 +11,27 @@ import (
"github.com/spf13/cobra"
)
type jsonDirEntry struct {
Name string `json:"name"`
IsDir bool `json:"is_directory"`
Size int64 `json:"size"`
ModTime time.Time `json:"last_modified"`
}
func toJsonDirEntry(f fs.DirEntry) (*jsonDirEntry, error) {
info, err := f.Info()
if err != nil {
return nil, err
}
return &jsonDirEntry{
Name: f.Name(),
IsDir: f.IsDir(),
Size: info.Size(),
ModTime: info.ModTime(),
}, nil
}
// lsCmd represents the ls command
var lsCmd = &cobra.Command{
Use: "ls <dir-name>",
@ -17,10 +40,6 @@ var lsCmd = &cobra.Command{
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}}
@ -46,9 +65,9 @@ var lsCmd = &cobra.Command{
return err
}
lsOutputs := make([]lsOutput, 0)
lsOutputs := make([]jsonDirEntry, 0)
for _, entry := range entries {
parsedEntry, err := toLsOutput(entry)
parsedEntry, err := toJsonDirEntry(entry)
if err != nil {
return err
}
@ -60,7 +79,10 @@ var lsCmd = &cobra.Command{
// Use template for long mode if the flag is set
if longMode {
return cmdio.RenderWithTemplate(ctx, lsOutputs, "template_long")
return cmdio.RenderWithTemplate(ctx, lsOutputs, cmdio.Heredoc(`
{{range .}}{{if .IsDir}}DIRECTORY {{else}}FILE {{end}}{{.Size}} {{.ModTime|pretty_date}} {{.Name}}
{{end}}
`))
}
return cmdio.Render(ctx, lsOutputs)
},

View File

@ -1,27 +0,0 @@
package fs
import (
"io/fs"
"time"
)
type lsOutput struct {
Name string `json:"name"`
IsDir bool `json:"is_directory"`
Size int64 `json:"size"`
ModTime time.Time `json:"last_modified"`
}
func toLsOutput(f fs.DirEntry) (*lsOutput, error) {
info, err := f.Info()
if err != nil {
return nil, err
}
return &lsOutput{
Name: f.Name(),
IsDir: f.IsDir(),
Size: info.Size(),
ModTime: info.ModTime(),
}, nil
}

View File

@ -2,7 +2,6 @@ package root
import (
"os"
"strings"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags"
@ -28,14 +27,13 @@ func OutputType() flags.Output {
}
func initializeIO(cmd *cobra.Command) error {
templates := make(map[string]string, 0)
for k, v := range cmd.Annotations {
if strings.Contains(k, "template") {
templates[k] = v
}
var template string
if cmd.Annotations != nil {
// rely on zeroval being an empty string
template = cmd.Annotations["template"]
}
cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), templates)
cmdIO := cmdio.NewIO(outputType, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), template)
ctx := cmdio.InContext(cmd.Context(), cmdIO)
cmd.SetContext(ctx)

View File

@ -24,17 +24,17 @@ type cmdIO struct {
// e.g. if stdout is a terminal
interactive bool
outputFormat flags.Output
templates map[string]string
template string
in io.Reader
out io.Writer
err io.Writer
}
func NewIO(outputFormat flags.Output, in io.Reader, out io.Writer, err io.Writer, templates map[string]string) *cmdIO {
func NewIO(outputFormat flags.Output, in io.Reader, out io.Writer, err io.Writer, template string) *cmdIO {
return &cmdIO{
interactive: !color.NoColor,
outputFormat: outputFormat,
templates: templates,
template: template,
in: in,
out: out,
err: err,
@ -66,14 +66,20 @@ func (c *cmdIO) IsTTY() bool {
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}
func (c *cmdIO) Render(v any, templateName string) error {
func Render(ctx context.Context, v any) error {
c := fromContext(ctx)
return RenderWithTemplate(ctx, v, c.template)
}
func RenderWithTemplate(ctx context.Context, v any, template string) error {
// TODO: add terminal width & white/dark theme detection
c := fromContext(ctx)
switch c.outputFormat {
case flags.OutputJSON:
return renderJson(c.out, v)
case flags.OutputText:
if c.templates[templateName] != "" {
return renderTemplate(c.out, c.templates[templateName], v)
if template != "" {
return renderTemplate(c.out, template, v)
}
return renderJson(c.out, v)
default:
@ -81,16 +87,6 @@ func (c *cmdIO) Render(v any, templateName string) error {
}
}
func Render(ctx context.Context, v any) error {
c := fromContext(ctx)
return c.Render(v, "template")
}
func RenderWithTemplate(ctx context.Context, v any, templateName string) error {
c := fromContext(ctx)
return c.Render(v, templateName)
}
type tuple struct{ Name, Id string }
func (c *cmdIO) Select(names map[string]string, label string) (id string, err error) {