Compare commits
1 Commits
feat/outpu
...
fix/table-
| Author | SHA1 | Date | |
|---|---|---|---|
| 7fc3bc7890 |
16
cmd/root.go
16
cmd/root.go
@ -4,12 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/output"
|
|
||||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/jlog"
|
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/jlog"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -19,7 +15,6 @@ var rootCmd = &cobra.Command{
|
|||||||
Short: "Unifood is a CLI for retrieving restaurant information",
|
Short: "Unifood is a CLI for retrieving restaurant information",
|
||||||
Long: ``,
|
Long: ``,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
_ = cmd.Help()
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,20 +30,11 @@ func Execute() {
|
|||||||
func initRootCmd() {
|
func initRootCmd() {
|
||||||
var appConfig AppConfig
|
var appConfig AppConfig
|
||||||
|
|
||||||
// Compile list of available formatters
|
|
||||||
formatters := slices.AppendSeq([]string{}, maps.Keys(output.Formatters))
|
|
||||||
formattersList := fmt.Sprintf("(available: %s)", strings.Join(formatters, ", "))
|
|
||||||
|
|
||||||
// Add persistent flags
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&appConfig.OutputVerbose, "verbose", "v", false, "Enable verbose output")
|
rootCmd.PersistentFlags().BoolVarP(&appConfig.OutputVerbose, "verbose", "v", false, "Enable verbose output")
|
||||||
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format "+formattersList)
|
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format")
|
||||||
rootCmd.PersistentFlags().BoolVar(&appConfig.OutputOrderReverse, "reverse", false, "Reverses output item order")
|
rootCmd.PersistentFlags().BoolVar(&appConfig.OutputOrderReverse, "reverse", false, "Reverses output item order")
|
||||||
rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config")
|
rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config")
|
||||||
|
|
||||||
// Add flag completions
|
|
||||||
rootCmd.RegisterFlagCompletionFunc("output", cobra.FixedCompletions(formatters, cobra.ShellCompDirectiveNoFileComp))
|
|
||||||
|
|
||||||
// Create logger and add child commands
|
|
||||||
logger := jlog.New(slog.LevelDebug)
|
logger := jlog.New(slog.LevelDebug)
|
||||||
ctx := jlog.ContextWith(context.Background(), logger)
|
ctx := jlog.ContextWith(context.Background(), logger)
|
||||||
|
|
||||||
|
|||||||
@ -12,5 +12,4 @@ var Formatters = map[string]interfaces.Formatter{
|
|||||||
"csv": &TableFormatter{HideSummary: true, RenderFormat: tableFormatCSV},
|
"csv": &TableFormatter{HideSummary: true, RenderFormat: tableFormatCSV},
|
||||||
"html": &TableFormatter{HideSummary: true, RenderFormat: tableFormatHTML},
|
"html": &TableFormatter{HideSummary: true, RenderFormat: tableFormatHTML},
|
||||||
"markdown": &TableFormatter{HideSummary: true, RenderFormat: tableFormatMarkdown},
|
"markdown": &TableFormatter{HideSummary: true, RenderFormat: tableFormatMarkdown},
|
||||||
"name": &NameFormatter{},
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +0,0 @@
|
|||||||
package output
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NameFormatter struct{}
|
|
||||||
|
|
||||||
func (f *NameFormatter) Format(list *interfaces.ResourceList) (io.Reader, error) {
|
|
||||||
var buffer = make([]byte, 0, 1024)
|
|
||||||
outputBuffer := bytes.NewBuffer(buffer)
|
|
||||||
|
|
||||||
for _, item := range list.Items {
|
|
||||||
outputBuffer.WriteString(item.ItemName() + "\r\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputBuffer, nil
|
|
||||||
}
|
|
||||||
@ -41,7 +41,6 @@ func (f *TableFormatter) Format(list *interfaces.ResourceList) (io.Reader, error
|
|||||||
|
|
||||||
// Setup table
|
// Setup table
|
||||||
t := table.NewWriter()
|
t := table.NewWriter()
|
||||||
t.SetOutputMirror(outputBuffer)
|
|
||||||
t.SetStyle(table.StyleLight)
|
t.SetStyle(table.StyleLight)
|
||||||
|
|
||||||
// Write header
|
// Write header
|
||||||
@ -66,15 +65,22 @@ func (f *TableFormatter) Format(list *interfaces.ResourceList) (io.Reader, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
switch f.RenderFormat {
|
output := func() string {
|
||||||
case tableFormatCSV:
|
switch f.RenderFormat {
|
||||||
t.RenderCSV()
|
case tableFormatCSV:
|
||||||
case tableFormatHTML:
|
return t.RenderCSV()
|
||||||
t.RenderHTML()
|
case tableFormatHTML:
|
||||||
case tableFormatMarkdown:
|
return t.RenderHTML()
|
||||||
t.RenderMarkdown()
|
case tableFormatMarkdown:
|
||||||
default:
|
return t.RenderMarkdown()
|
||||||
t.Render()
|
default:
|
||||||
|
return t.Render()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
_, err := outputBuffer.WriteString(output)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to write rendered table: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return outputBuffer, nil
|
return outputBuffer, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user