From 19a20512e9870859113f1c28755d39c7695e7fd5 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 5 Jun 2023 13:49:33 +0200 Subject: [PATCH] comments --- cmd/fs/ls.go | 36 +++++++++++++++++++++++++++++------- cmd/fs/ls_output.go | 27 --------------------------- cmd/root/io.go | 12 +++++------- libs/cmdio/io.go | 28 ++++++++++++---------------- 4 files changed, 46 insertions(+), 57 deletions(-) delete mode 100644 cmd/fs/ls_output.go diff --git a/cmd/fs/ls.go b/cmd/fs/ls.go index 76b51ce7..1df8dfcf 100644 --- a/cmd/fs/ls.go +++ b/cmd/fs/ls.go @@ -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 ", @@ -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) }, diff --git a/cmd/fs/ls_output.go b/cmd/fs/ls_output.go deleted file mode 100644 index e8728b04..00000000 --- a/cmd/fs/ls_output.go +++ /dev/null @@ -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 -} diff --git a/cmd/root/io.go b/cmd/root/io.go index 07db9e0a..93830c80 100644 --- a/cmd/root/io.go +++ b/cmd/root/io.go @@ -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) diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 4f134c51..1df6f5c1 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -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) {