From 17a1cf1adea545eff7d7ccb09f17bd0c152e1117 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Mar 2026 16:11:53 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20Add=20AddFormatter=20function?= =?UTF-8?q?=20to=20allow=20custom=20builds=20to=20register=20new=20formatt?= =?UTF-8?q?ers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formatters/json.go | 4 +++- formatters/new.go | 32 +++++++++++++++++++++++++++----- formatters/new_test.go | 14 ++++++++++++++ formatters/null.go | 29 ++++++++++------------------- formatters/plain.go | 12 ++++++------ 5 files changed, 60 insertions(+), 31 deletions(-) diff --git a/formatters/json.go b/formatters/json.go index e79acab..0edfda3 100644 --- a/formatters/json.go +++ b/formatters/json.go @@ -10,6 +10,8 @@ import ( "github.com/spf13/viper" ) +const FormatJSON string = "json" + func newJson(ff *viper.Viper) (Formatter, error) { js := new(Json) err := ff.Unmarshal(js) @@ -24,7 +26,7 @@ type Json struct { } func (js *Json) Name() string { - return "json" + return FormatJSON } func (js *Json) marshal(v any) (o []byte, err error) { diff --git a/formatters/new.go b/formatters/new.go index ff9610d..bd063fa 100644 --- a/formatters/new.go +++ b/formatters/new.go @@ -2,18 +2,22 @@ package formatters import ( "context" + "errors" "fmt" + "sync" "codeberg.org/danjones000/my-log/config" "github.com/spf13/viper" ) -type formatMaker func(config *viper.Viper) (Formatter, error) +var mut sync.RWMutex -var formatterMap = map[string]formatMaker{ - "plain": newPlain, - "json": newJson, - "zero": newNull, +type FormatInit func(config *viper.Viper) (Formatter, error) + +var formatterMap = map[string]FormatInit{ + FormatPlain: newPlain, + FormatJSON: newJson, + FormatNull: newNull, } func Preferred(ctx context.Context) (f Formatter, err error) { @@ -25,6 +29,8 @@ func New(ctx context.Context, kind string) (f Formatter, err error) { v, _ := config.RetrieveFromContext(ctx) formatterConf := v.Sub("formatters." + kind) + mut.RLock() + defer mut.RUnlock() if maker, ok := formatterMap[kind]; ok { return maker(formatterConf) } @@ -34,8 +40,24 @@ func New(ctx context.Context, kind string) (f Formatter, err error) { func Kinds() []string { r := []string{} + + mut.RLock() + defer mut.RUnlock() for kind, _ := range formatterMap { r = append(r, kind) } return r } + +var ErrAlreadyAdded = errors.New("formatter already present") + +func AddFormatter(key string, f FormatInit) error { + mut.Lock() + defer mut.Unlock() + if _, present := formatterMap[key]; present { + return fmt.Errorf("%w: %s", ErrAlreadyAdded, key) + } + + formatterMap[key] = f + return nil +} diff --git a/formatters/new_test.go b/formatters/new_test.go index d680709..df51a0a 100644 --- a/formatters/new_test.go +++ b/formatters/new_test.go @@ -39,3 +39,17 @@ func TestPreferred(t *testing.T) { be.Err(t, err, nil) be.True(t, form != nil) } + +type dummyFormatter struct{ Null } + +func (dummyFormatter) Name() string { return "dummy" } + +func TestAddFormatter(t *testing.T) { + var df dummyFormatter + dummyInit := func(*viper.Viper) (Formatter, error) { return df, nil } + err := AddFormatter(df.Name(), dummyInit) + be.Err(t, err, nil) + + err = AddFormatter(df.Name(), dummyInit) + be.Err(t, err, ErrAlreadyAdded) +} diff --git a/formatters/null.go b/formatters/null.go index f83cbde..8851e04 100644 --- a/formatters/null.go +++ b/formatters/null.go @@ -5,28 +5,19 @@ import ( "github.com/spf13/viper" ) -func newNull(ff *viper.Viper) (Formatter, error) { - return &Null{}, nil +const FormatNull = "zero" + +func newNull(*viper.Viper) (Formatter, error) { + return Null{}, nil } type Null struct{} -func (n *Null) Name() string { - return "zero" +func (Null) Name() string { + return FormatNull } -func (n *Null) Meta(m models.Meta) (o []byte, err error) { - return -} - -func (n *Null) Entry(e models.Entry) (o []byte, err error) { - return -} - -func (n *Null) Log(l models.Log) (o []byte, err error) { - return -} - -func (n *Null) Logs(logs []models.Log) (out []byte, err error) { - return -} +func (Null) Meta(m models.Meta) (o []byte, err error) { return } +func (Null) Entry(e models.Entry) (o []byte, err error) { return } +func (Null) Log(l models.Log) (o []byte, err error) { return } +func (Null) Logs(logs []models.Log) (out []byte, err error) { return } diff --git a/formatters/plain.go b/formatters/plain.go index 0a94f81..6bf5c4f 100644 --- a/formatters/plain.go +++ b/formatters/plain.go @@ -8,16 +8,16 @@ import ( "github.com/spf13/viper" ) -func newPlain(ff *viper.Viper) (Formatter, error) { +const FormatPlain string = "plain" + +func newPlain(*viper.Viper) (Formatter, error) { return &PlainText{}, nil } -type PlainText struct { - // config might go here some day -} +type PlainText struct{} -func (pt *PlainText) Name() string { - return "plain" +func (*PlainText) Name() string { + return FormatPlain } func (pt *PlainText) Logs(logs []models.Log) (out []byte, err error) { From b7eaf941fb68aed3b9ff0b10a407231fa84f6dff Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Mar 2026 16:45:18 -0500 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20Add=20option=20to=20print=20con?= =?UTF-8?q?fig=20from=20config=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/config.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cli/config.go b/cli/config.go index 0512bb4..99d4414 100644 --- a/cli/config.go +++ b/cli/config.go @@ -22,12 +22,13 @@ import ( fp "path/filepath" "codeberg.org/danjones000/my-log/config" + "github.com/pelletier/go-toml/v2" "github.com/spf13/cobra" ) var ConfigCmd = &cobra.Command{ Use: "config", - Short: "Save default config to file", + Short: "Save default config to file, or print the current config value", SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) (err error) { print, _ := cmd.Flags().GetBool("print") @@ -35,6 +36,24 @@ var ConfigCmd = &cobra.Command{ fmt.Fprintln(cmd.OutOrStdout(), config.DefaultPath()) return nil } + + if len(args) > 0 { + v, _ := config.RetrieveFromContext(cmd.Context()) + val := v.Get(args[0]) + var out []byte + if val == nil { + out = []byte("") + } else { + var err error + out, err = toml.Marshal(val) + if err != nil { + return err + } + } + fmt.Fprintln(cmd.OutOrStdout(), string(out)) + return nil + } + force, _ := cmd.Flags().GetBool("force") configPath := config.DefaultPath() if !force { From edf10de87946d0fc87fdc922ad472f5494255ed9 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Mar 2026 16:48:58 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20Allow=20MYLOG=5FCONFIG=5FPATH?= =?UTF-8?q?=20to=20override=20the=20config=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 2 +- cli/root.go | 17 +++++++---------- config/load.go | 10 +++++++--- config/load_test.go | 9 +++------ 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 74f23bc..8ea2d6e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,7 +22,7 @@ ## Config System - Viper instance is stored in context using a custom key type (`confKeyType`) -- Use `config.New(path, overrides)` to create a new viper instance +- Use `config.New(ctx, path)` to create a new viper instance - Use `config.RetrieveFromContext(ctx)` to get both viper and the unmarshaled Config struct - Formatters can use `v.Sub("formatters." + kind)` to get their own sub-config and unmarshal into their specific config struct - Test files must create viper instances and add them to context using `config.AddToContext` diff --git a/cli/root.go b/cli/root.go index ddaca3b..9b32959 100644 --- a/cli/root.go +++ b/cli/root.go @@ -28,19 +28,12 @@ var RootCmd = &cobra.Command{ Use: "my-log", Short: "A brief description of your application", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - ctx, v, err := config.New(cmd.Context()) + + ctx, v, err := config.New(cmd.Context(), configPath) if err != nil { return err } - if configPath != "" { - v.SetConfigFile(configPath) - err := v.ReadInConfig() - if err != nil { - return err - } - } - for k, val := range configValues { v.Set(k, val) } @@ -62,6 +55,10 @@ var configPath string var configValues map[string]string func init() { - RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file") + path := os.Getenv("MYLOG_CONFIG_PATH") + if path == "" { + path = config.DefaultPath() + } + RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", path, "config file") 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/load.go b/config/load.go index fd20cae..09726d2 100644 --- a/config/load.go +++ b/config/load.go @@ -32,7 +32,7 @@ func AddToContext(ctx context.Context, v *viper.Viper) context.Context { return context.WithValue(ctx, viperKey, v) } -func New(ctx context.Context) (context.Context, *viper.Viper, error) { +func New(ctx context.Context, path string) (context.Context, *viper.Viper, error) { v := viper.New() v.SetConfigType("toml") @@ -40,12 +40,16 @@ func New(ctx context.Context) (context.Context, *viper.Viper, error) { return ctx, nil, err } - v.SetConfigFile(DefaultPath()) + if path == "" { + path = DefaultPath() + } + + v.SetConfigFile(path) v.SetEnvPrefix("MYLOG") v.AutomaticEnv() v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) - if err := v.ReadInConfig(); err != nil { + if err := v.MergeInConfig(); err != nil { return ctx, nil, err } diff --git a/config/load_test.go b/config/load_test.go index 71dbb0c..af885b0 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -9,7 +9,7 @@ import ( ) func TestNew(t *testing.T) { - _, v, err := New(t.Context()) + _, v, err := New(t.Context(), "") be.Err(t, err, nil) be.True(t, v != nil) } @@ -18,7 +18,7 @@ func TestNewWithEnvOverrides(t *testing.T) { os.Setenv("MYLOG_INPUT_PATH", "/test/path") defer os.Unsetenv("MYLOG_INPUT_PATH") - _, v, err := New(t.Context()) + _, v, err := New(t.Context(), "") be.Err(t, err, nil) be.Equal(t, v.GetString("input.path"), "/test/path") } @@ -32,12 +32,9 @@ path = "/file/path" ext = "log"`) f.Close() - _, v, err := New(t.Context()) + _, v, err := New(t.Context(), f.Name()) be.Err(t, err, nil) - v.SetConfigFile(f.Name()) - v.SetConfigType("toml") - err = v.ReadInConfig() be.Err(t, err, nil) be.Equal(t, v.GetString("input.path"), "/file/path") be.Equal(t, v.GetString("input.ext"), "log") From 5cf8e50cf966c33c9f3d78d27d7f83059bd306b0 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Mar 2026 16:50:53 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9D=20Update=20CHANGELOG=20for=20v?= =?UTF-8?q?0.3.1=20release?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e48feb..4ed9019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.3.1] - 2026-03-09 + +- Add AddFormatter function to allow custom builds to register new formatters +- Add option to print config from config command +- Allow MYLOG_CONFIG_PATH to override the config path + ## [0.3.0] - 2026-03-09 - Refactor configuration to use viper with context propagation instead of global variables