feat: add name formatter #10

Open
bissendorf wants to merge 3 commits from feat/output-name into dev
3 changed files with 34 additions and 1 deletions
Showing only changes of commit 5205ddf094 - Show all commits

View File

@ -4,8 +4,12 @@ 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"
) )
@ -15,6 +19,7 @@ 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()
}, },
} }
@ -30,11 +35,17 @@ 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") 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.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")
// 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)