feat: go output formatter

This commit is contained in:
2025-07-20 15:19:47 +02:00
parent 0b0f0c9f0a
commit ef07dd1b86
6 changed files with 50 additions and 11 deletions

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())