♲ Refactor configuration to use viper with context propagation

- Replace global ConfigPath and Overrides with viper-based configuration
- Add viper.New() to create configurable viper instances
- Store viper and unmarshaled Config struct in context for testability
- Add RetrieveFromContext and AddToContext helper functions
- Update files.Append to accept context and retrieve config from it
- Update formatters.Preferred and formatters.New to accept context
- Add PersistentPreRunE in CLI to create and configure viper instance
- Support -c flag for custom config file path
- Support -v flag for config value overrides
- Update all test files to create viper and add to context
- Remove unused config types and load functions
- Add viper as dependency with automatic env var support (MYLOG_*)
This commit is contained in:
Dan Jones 2026-03-08 22:59:33 -05:00
commit 9f05f933dd
21 changed files with 338 additions and 360 deletions

View file

@ -1,98 +1,53 @@
package config
import (
"encoding/json"
"bytes"
"context"
"fmt"
"os"
fp "path/filepath"
"time"
"strings"
"codeberg.org/danjones000/my-log/tools"
"github.com/BurntSushi/toml"
"github.com/caarlos0/env/v10"
mapst "github.com/go-viper/mapstructure/v2"
"github.com/spf13/viper"
)
var ConfigPath string
var Overrides = map[string]string{}
type confKeyType uint8
func init() {
conf, _ := os.UserConfigDir()
ConfigPath = fp.Join(conf, "my-log", "config.toml")
}
const (
_ confKeyType = iota
viperKey
)
func Load() (Config, error) {
c, _ := DefaultConfig()
_, err := os.Stat(ConfigPath)
if !os.IsNotExist(err) {
_, err = toml.DecodeFile(ConfigPath, &c)
if err != nil {
return c, err
}
}
env.Parse(&c)
c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"])
c.Formatters["json"] = loadJsonFormat(c.Formatters["json"])
l := ""
for k, v := range Overrides {
val := tools.ParseString(v)
if val == nil {
continue
}
if _, isJson := val.(json.RawMessage); isJson {
continue
}
valout := fmt.Sprintf("%v", val)
if vals, isString := val.(string); isString {
valout = fmt.Sprintf(`"%s"`, vals)
}
if valt, isTime := val.(time.Time); isTime {
valout = valt.Format(time.RFC3339)
}
l = l + "\n" + fmt.Sprintf("%s = %s", k, valout)
}
_, err = toml.Decode(l, &c)
return c, err
}
func loadStdout(stdout Output) Output {
st := stdoutEnabled{stdout.Enabled}
env.Parse(&st)
stdout.Enabled = st.Enabled
var std Stdout
mapst.Decode(stdout.Config, &std)
env.Parse(&std)
mapst.Decode(std, &stdout.Config)
return stdout
}
func (oo Outputs) Stdout() (s Stdout, enabled bool) {
o, ok := oo["stdout"]
func RetrieveFromContext(ctx context.Context) (*viper.Viper, Config) {
v, ok := ctx.Value(viperKey).(*viper.Viper)
if !ok {
return s, false
panic("config not found in context")
}
var c Config
if err := v.Unmarshal(&c); err != nil {
panic(fmt.Errorf("failed to unmarshal config: %w", err))
}
return v, c
}
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) {
v := viper.New()
v.SetConfigType("toml")
if err := v.ReadConfig(bytes.NewBufferString(DefaultStr())); err != nil {
return ctx, nil, err
}
enabled = o.Enabled
mapst.Decode(o.Config, &s)
v.SetConfigFile(DefaultPath())
v.SetEnvPrefix("MYLOG")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
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
if err := v.ReadInConfig(); err != nil {
return ctx, nil, err
}
mapst.Decode(o, &jf)
return
return AddToContext(ctx, v), v, nil
}