2 Commits

Author SHA1 Message Date
5205ddf094 feat: output available output formatters in help page 2025-07-24 21:20:02 +02:00
79c6a1ea8a feat: add name formatter 2025-07-24 21:19:40 +02:00
5 changed files with 38 additions and 12 deletions

View File

@ -1,11 +1,8 @@
package cmd
const DefaultRequestTimeout uint = 30
type AppConfig struct {
OutputVerbose bool
OutputFormatter string
OutputOrderReverse bool
PrintConfig bool
RequestTimeoutSeconds uint
}

View File

@ -4,8 +4,12 @@ import (
"context"
"fmt"
"log/slog"
"maps"
"os"
"slices"
"strings"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/output"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/jlog"
"github.com/spf13/cobra"
)
@ -15,6 +19,7 @@ var rootCmd = &cobra.Command{
Short: "Unifood is a CLI for retrieving restaurant information",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
}
@ -30,12 +35,17 @@ func Execute() {
func initRootCmd() {
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().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format")
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format "+formattersList)
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().UintVar(&appConfig.RequestTimeoutSeconds, "timeout", DefaultRequestTimeout, "Set the request timeout in seconds")
// Create logger and add child commands
logger := jlog.New(slog.LevelDebug)
ctx := jlog.ContextWith(context.Background(), logger)

View File

@ -8,7 +8,6 @@ import (
"os"
"slices"
"strings"
"time"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces/params"
@ -93,8 +92,6 @@ func getVerbs(ctx context.Context, config *AppConfig) (commands []*cobra.Command
}
logger := jlog.New(logLevel)
ctx := jlog.ContextWith(context.Background(), logger)
ctx, cancel := context.WithTimeout(ctx, time.Duration(config.RequestTimeoutSeconds)*time.Second)
defer cancel()
// Print config
if config.PrintConfig {

View File

@ -12,4 +12,5 @@ var Formatters = map[string]interfaces.Formatter{
"csv": &TableFormatter{HideSummary: true, RenderFormat: tableFormatCSV},
"html": &TableFormatter{HideSummary: true, RenderFormat: tableFormatHTML},
"markdown": &TableFormatter{HideSummary: true, RenderFormat: tableFormatMarkdown},
"name": &NameFormatter{},
}

21
core/output/name.go Normal file
View File

@ -0,0 +1,21 @@
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
}