From cfeab035d33691277d29e83e372a039adb4062c3 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Mar 2026 13:38:13 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Move=20preferred=20formatt?= =?UTF-8?q?er=20to=20its=20own=20config=20field?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/drop.go | 2 +- cli/root.go | 2 +- config/default.go | 4 ++++ config/types.go | 4 +++- formatters/new.go | 5 ++--- formatters/new_test.go | 2 +- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cli/drop.go b/cli/drop.go index e50a1ca..3a1c794 100644 --- a/cli/drop.go +++ b/cli/drop.go @@ -44,7 +44,7 @@ var DropCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { v, _ := config.RetrieveFromContext(cmd.Context()) if outJson { - v.Set("output.stdout.config.format", "json") + v.Set("formatters.preferred", "json") } log := args[0] diff --git a/cli/root.go b/cli/root.go index def2fe8..ddaca3b 100644 --- a/cli/root.go +++ b/cli/root.go @@ -63,5 +63,5 @@ var configValues map[string]string func init() { 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") } diff --git a/config/default.go b/config/default.go index 805c954..2f3fc2e 100644 --- a/config/default.go +++ b/config/default.go @@ -30,6 +30,10 @@ format = "plain" [formatters] +# Default formatter used. Some outputs can override this value +preferred = "plain" + +# Each formatter may have additional configuration [formatters.json] # Set to true to pretty print JSON output pretty_print = false diff --git a/config/types.go b/config/types.go index 4f9a32b..fff0d9b 100644 --- a/config/types.go +++ b/config/types.go @@ -20,4 +20,6 @@ type Output struct { Config map[string]any } -type Formatters map[string]map[string]any +type Formatters struct { + Preferred string +} diff --git a/formatters/new.go b/formatters/new.go index 96692ef..ff9610d 100644 --- a/formatters/new.go +++ b/formatters/new.go @@ -17,9 +17,8 @@ var formatterMap = map[string]formatMaker{ } func Preferred(ctx context.Context) (f Formatter, err error) { - v, _ := config.RetrieveFromContext(ctx) - format := v.GetString("output.stdout.config.format") - return New(ctx, format) + _, c := config.RetrieveFromContext(ctx) + return New(ctx, c.Formatters.Preferred) } func New(ctx context.Context, kind string) (f Formatter, err error) { diff --git a/formatters/new_test.go b/formatters/new_test.go index 18968e8..d680709 100644 --- a/formatters/new_test.go +++ b/formatters/new_test.go @@ -21,7 +21,7 @@ func setupNewTest(t *testing.T) context.Context { t.Helper() v := viper.New() v.SetConfigType("toml") - v.Set("output.stdout.config.format", "plain") + v.Set("formatters.preferred", "plain") v.Set("formatters.json.pretty_print", false) return config.AddToContext(t.Context(), v) }