⚙️ Move preferred formatter to its own config field

This commit is contained in:
Dan Jones 2026-03-09 13:38:13 -05:00
commit cfeab035d3
6 changed files with 12 additions and 7 deletions

View file

@ -44,7 +44,7 @@ var DropCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
v, _ := config.RetrieveFromContext(cmd.Context()) v, _ := config.RetrieveFromContext(cmd.Context())
if outJson { if outJson {
v.Set("output.stdout.config.format", "json") v.Set("formatters.preferred", "json")
} }
log := args[0] log := args[0]

View file

@ -63,5 +63,5 @@ var configValues map[string]string
func init() { func init() {
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file") RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file")
RootCmd.PersistentFlags().StringToStringVarP(&configValues, "config-value", "v", nil, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json") RootCmd.PersistentFlags().StringToStringVarP(&configValues, "config-value", "v", nil, "Override config values. Use dot syntax to specify key. E.g. -v formatters.preferred=json")
} }

View file

@ -30,6 +30,10 @@ format = "plain"
[formatters] [formatters]
# Default formatter used. Some outputs can override this value
preferred = "plain"
# Each formatter may have additional configuration
[formatters.json] [formatters.json]
# Set to true to pretty print JSON output # Set to true to pretty print JSON output
pretty_print = false pretty_print = false

View file

@ -20,4 +20,6 @@ type Output struct {
Config map[string]any Config map[string]any
} }
type Formatters map[string]map[string]any type Formatters struct {
Preferred string
}

View file

@ -17,9 +17,8 @@ var formatterMap = map[string]formatMaker{
} }
func Preferred(ctx context.Context) (f Formatter, err error) { func Preferred(ctx context.Context) (f Formatter, err error) {
v, _ := config.RetrieveFromContext(ctx) _, c := config.RetrieveFromContext(ctx)
format := v.GetString("output.stdout.config.format") return New(ctx, c.Formatters.Preferred)
return New(ctx, format)
} }
func New(ctx context.Context, kind string) (f Formatter, err error) { func New(ctx context.Context, kind string) (f Formatter, err error) {

View file

@ -21,7 +21,7 @@ func setupNewTest(t *testing.T) context.Context {
t.Helper() t.Helper()
v := viper.New() v := viper.New()
v.SetConfigType("toml") v.SetConfigType("toml")
v.Set("output.stdout.config.format", "plain") v.Set("formatters.preferred", "plain")
v.Set("formatters.json.pretty_print", false) v.Set("formatters.json.pretty_print", false)
return config.AddToContext(t.Context(), v) return config.AddToContext(t.Context(), v)
} }