feat: unifood base with GET dishes #1

Merged
bissendorf merged 6 commits from feat/base into dev 2025-07-20 17:29:04 +00:00
24 changed files with 712 additions and 0 deletions
Showing only changes of commit ef07dd1b86 - Show all commits

View File

@ -2,5 +2,6 @@ package cmd
type AppConfig struct {
OutputVerbose bool
OutputMode string
OutputFormatter string
PrintConfig bool
}

View File

@ -31,12 +31,13 @@ func initRootCmd() {
var appConfig AppConfig
rootCmd.PersistentFlags().BoolVarP(&appConfig.OutputVerbose, "verbose", "v", false, "Enable verbose output")
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputMode, "output", "o", "json", "Set output format")
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "json", "Set output format")
rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config")
logger := jlog.New(slog.LevelDebug)
ctx := jlog.ContextWith(context.Background(), logger)
logger.Debug("Register verb commands")
rootCmd.AddCommand(getVerbs(ctx, appConfig)...)
rootCmd.AddCommand(getVerbs(ctx, &appConfig)...)
logger.Debug("Verb commands registered successfully")
}

View File

@ -20,14 +20,14 @@ type VerbItem struct {
Name interfaces.Verb
Aliases []string
Description string
RunFn func(ctx context.Context, config AppConfig, handler interfaces.ResourceHandler, params params.Container) error
RunFn func(ctx context.Context, config *AppConfig, handler interfaces.ResourceHandler, params params.Container) error
}
var verbs = []VerbItem{
{
Name: interfaces.VerbGet,
Description: "Retrieve resource information",
RunFn: func(ctx context.Context, config AppConfig, handler interfaces.ResourceHandler, params params.Container) error {
RunFn: func(ctx context.Context, config *AppConfig, handler interfaces.ResourceHandler, params params.Container) error {
h, ok := handler.(interfaces.GetHandler)
if !ok {
return fmt.Errorf("resource does not support GET")
@ -39,7 +39,7 @@ var verbs = []VerbItem{
return fmt.Errorf("retrieving item failed: %w", err)
}
formatterName := strings.ToLower(config.OutputMode)
formatterName := strings.ToLower(config.OutputFormatter)
formatter, exists := output.Formatters[formatterName]
if !exists {
return fmt.Errorf("could not find output formatter '%s'", formatterName)
@ -60,7 +60,7 @@ var verbs = []VerbItem{
},
}
func getVerbs(ctx context.Context, config AppConfig) (commands []*cobra.Command) {
func getVerbs(ctx context.Context, config *AppConfig) (commands []*cobra.Command) {
logger := jlog.FromContext(ctx)
for _, v := range verbs {
@ -83,8 +83,19 @@ func getVerbs(ctx context.Context, config AppConfig) (commands []*cobra.Command)
Aliases: r.Aliases,
Short: r.Description,
Run: func(cmd *cobra.Command, args []string) {
logger := jlog.New(slog.LevelInfo)
// Configure log
var logLevel = slog.LevelWarn
if config.OutputVerbose {
logLevel = slog.LevelDebug
}
logger := jlog.New(logLevel)
ctx := jlog.ContextWith(context.Background(), logger)
// Print config
if config.PrintConfig {
logger.WarnContext(ctx, "Printing app config", slog.Any("config", config), slog.Any("parameters", params.ToMap()))
}
err := v.RunFn(ctx, config, r.Handler, params)
if err != nil {
logger.ErrorContext(ctx, fmt.Sprintf("%s %s failed", strings.ToUpper(string(v.Name)), r.Name), "error", err.Error())

View File

@ -54,3 +54,16 @@ func (c *Container) GetValue(key string) (any, error) {
return item.parseFn(*item.value)
}
// Returns a map of all parameters. Parameters that produce errors during parsing are ignored.
func (c *Container) ToMap() (out map[string]any) {
out = make(map[string]any)
for key := range c.params {
v, err := c.GetValue(key)
if err == nil {
out[key] = v
}
}
return
}

View File

@ -6,7 +6,7 @@ import (
var Formatters = map[string]interfaces.Formatter{
"json": &JsonFormatter{},
"go": nil,
"yaml": nil,
"go": &GoFormatter{},
"table": nil,
"xml": nil,
}

13
core/output/go.go Normal file
View File

@ -0,0 +1,13 @@
package output
import (
"fmt"
"io"
"strings"
)
type GoFormatter struct{}
func (f *GoFormatter) Format(object any) (io.Reader, error) {
return strings.NewReader(fmt.Sprintf("%#v", object)), nil
}