diff --git a/cmd/config.go b/cmd/config.go index 767e769..560a052 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,6 +1,7 @@ package cmd type AppConfig struct { - OutputVerbose bool - OutputMode string + OutputVerbose bool + OutputFormatter string + PrintConfig bool } diff --git a/cmd/root.go b/cmd/root.go index 9a4b24f..804832e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") } diff --git a/cmd/verbs.go b/cmd/verbs.go index 827c747..6775c9c 100644 --- a/cmd/verbs.go +++ b/cmd/verbs.go @@ -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()) diff --git a/core/interfaces/params/params.go b/core/interfaces/params/params.go index e763510..f2f7fbb 100644 --- a/core/interfaces/params/params.go +++ b/core/interfaces/params/params.go @@ -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 +} diff --git a/core/output/formatter.go b/core/output/formatter.go index 243caab..c6b3159 100644 --- a/core/output/formatter.go +++ b/core/output/formatter.go @@ -6,7 +6,7 @@ import ( var Formatters = map[string]interfaces.Formatter{ "json": &JsonFormatter{}, - "go": nil, + "yaml": nil, + "go": &GoFormatter{}, "table": nil, - "xml": nil, } diff --git a/core/output/go.go b/core/output/go.go new file mode 100644 index 0000000..4cf80bf --- /dev/null +++ b/core/output/go.go @@ -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 +}