From da3b5249259b4b1b879eb9aebb120de83fb8418a Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Thu, 7 Mar 2024 21:19:45 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Separate=20formatters=20in=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/default.go | 10 ++++++++-- config/load.go | 19 +++++++++++++++++++ config/load_test.go | 19 ++++++++++++++++--- config/types.go | 13 ++++++++++--- formatters/new.go | 4 ++-- formatters/plain.go | 2 +- 6 files changed, 56 insertions(+), 11 deletions(-) diff --git a/config/default.go b/config/default.go index 461f5c3..76ef8d2 100644 --- a/config/default.go +++ b/config/default.go @@ -27,8 +27,14 @@ dotFolder = true [output.stdout] enabled = true [output.stdout.config] -# Whether to output as JSON. Maybe useful to pipe elsewhere. -json = false +# Formatter to use when outputting to stdout +formatter = "plain" + +[formatters] + +[formatters.json] +# Set to true to pretty print JSON output +pretty_print = false ` diff --git a/config/load.go b/config/load.go index 04fd95d..3de0a19 100644 --- a/config/load.go +++ b/config/load.go @@ -32,6 +32,7 @@ func Load() (Config, error) { } env.Parse(&c) c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"]) + c.Formatters["json"] = loadJsonFormat(c.Formatters["json"]) l := "" for k, v := range Overrides { @@ -77,3 +78,21 @@ func (oo Outputs) Stdout() (s Stdout, enabled bool) { return } + +func loadJsonFormat(c map[string]any) map[string]any { + jf := JsonFormat{} + mapst.Decode(c, &jf) + env.Parse(&jf) + mapst.Decode(jf, &c) + return c +} + +func (ff Formatters) Json() (jf JsonFormat) { + o, ok := ff["json"] + if !ok { + return + } + + mapst.Decode(o, &jf) + return +} diff --git a/config/load_test.go b/config/load_test.go index 7399316..ca6a0b7 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -65,12 +65,25 @@ func TestStdoutMissing(t *testing.T) { } func TestStdoutLoad(t *testing.T) { - os.Setenv("LOG_STDOUT_JSON", "true") - defer os.Unsetenv("LOG_STDOUT_JSON") + os.Setenv("LOG_STDOUT_FORMATTER", "json") + defer os.Unsetenv("LOG_STDOUT_FORMATTER") os.Setenv("LOG_STDOUT_ENABLED", "true") defer os.Unsetenv("LOG_STDOUT_ENABLED") c, _ := Load() std, en := c.Outputs.Stdout() assert.True(t, en) - assert.True(t, std.Json) + assert.Equal(t, "json", std.Formatter) +} + +func TestFormatJson(t *testing.T) { + ff := Formatters{ + "json": map[string]any{"pretty_print": true}, + } + + js := ff.Json() + assert.True(t, js.PrettyPrint) + + ff = Formatters{} + js = ff.Json() + assert.False(t, js.PrettyPrint) } diff --git a/config/types.go b/config/types.go index a3a3681..a950a80 100644 --- a/config/types.go +++ b/config/types.go @@ -1,8 +1,9 @@ package config type Config struct { - Input Input - Outputs Outputs `toml:"output"` + Input Input + Outputs Outputs `toml:"output"` + Formatters Formatters } type Input struct { @@ -20,9 +21,15 @@ type Output struct { } type Stdout struct { - Json bool `env:"LOG_STDOUT_JSON" mapstructure:"json"` + Formatter string `env:"LOG_STDOUT_FORMATTER" mapstructure:"formatter"` } type stdoutEnabled struct { Enabled bool `env:"LOG_STDOUT_ENABLED"` } + +type Formatters map[string]map[string]any + +type JsonFormat struct { + PrettyPrint bool `env:"LOG_JSON_PRETTY_PRINT" mapstructure:"pretty_print"` +} diff --git a/formatters/new.go b/formatters/new.go index 8cd567f..33084e3 100644 --- a/formatters/new.go +++ b/formatters/new.go @@ -6,7 +6,7 @@ import ( "codeberg.org/danjones000/my-log/config" ) -type formatMaker func(oo config.Outputs) (Formatter, error) +type formatMaker func(config.Formatters) (Formatter, error) var formatterMap = map[string]formatMaker{ "plain": newPlain, @@ -19,7 +19,7 @@ func New(kind string) (f Formatter, err error) { } if make, ok := formatterMap[kind]; ok { - return make(conf.Outputs) + return make(conf.Formatters) } return nil, errors.New("unimplemented") diff --git a/formatters/plain.go b/formatters/plain.go index 420b008..ed8ee82 100644 --- a/formatters/plain.go +++ b/formatters/plain.go @@ -8,7 +8,7 @@ import ( "codeberg.org/danjones000/my-log/tools" ) -func newPlain(oo config.Outputs) (Formatter, error) { +func newPlain(ff config.Formatters) (Formatter, error) { return &PlainText{}, nil }