feat: go output formatter
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
type AppConfig struct {
|
||||
OutputVerbose bool
|
||||
OutputMode string
|
||||
OutputVerbose bool
|
||||
OutputFormatter string
|
||||
PrintConfig bool
|
||||
}
|
||||
|
||||
@ -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")
|
||||
}
|
||||
|
||||
21
cmd/verbs.go
21
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())
|
||||
|
||||
Reference in New Issue
Block a user