♲ 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,93 +1,51 @@
package config
import (
"fmt"
"os"
"testing"
"github.com/nalgeon/be"
"github.com/spf13/viper"
)
func TestLoad(t *testing.T) {
f, _ := os.CreateTemp("", "test")
ConfigPath = f.Name()
defer f.Close()
fmt.Fprint(f, `[input]
func TestNew(t *testing.T) {
_, v, err := New(t.Context())
be.Err(t, err, nil)
be.True(t, v != nil)
}
func TestNewWithEnvOverrides(t *testing.T) {
os.Setenv("MYLOG_INPUT_PATH", "/test/path")
defer os.Unsetenv("MYLOG_INPUT_PATH")
_, v, err := New(t.Context())
be.Err(t, err, nil)
be.Equal(t, v.GetString("input.path"), "/test/path")
}
func TestNewWithConfigFile(t *testing.T) {
dir := t.ArtifactDir()
f, _ := os.CreateTemp(dir, "test*.toml")
defer os.Remove(f.Name())
f.WriteString(`[input]
path = "/file/path"
ext = "log"`)
c, err := Load()
f.Close()
_, v, err := New(t.Context())
be.Err(t, err, nil)
be.Equal(t, c.Input.Ext, "log")
}
func TestLoadBadFile(t *testing.T) {
f, _ := os.CreateTemp("", "test")
ConfigPath = f.Name()
defer f.Close()
fmt.Fprint(f, `{"not":"toml"}`)
_, err := Load()
be.Err(t, err)
}
func TestLoadIgnoreMissingFile(t *testing.T) {
def, _ := DefaultConfig()
ConfigPath = "/not/a/real/file"
c, err := Load()
v.SetConfigFile(f.Name())
v.SetConfigType("toml")
err = v.ReadInConfig()
be.Err(t, err, nil)
be.Equal(t, c, def)
be.Equal(t, v.GetString("input.path"), "/file/path")
be.Equal(t, v.GetString("input.ext"), "log")
}
func TestOverride(t *testing.T) {
Overrides = map[string]string{
"input.path": "/path/to/it",
"input.ext": "~",
}
c, err := Load()
be.Err(t, err, nil)
be.Equal(t, c.Input.Path, Overrides["input.path"])
be.Equal(t, c.Input.Ext, "txt")
}
func TestOverrideJson(t *testing.T) {
Overrides = map[string]string{"input.ext": `{"a":"b"}`}
c, err := Load()
be.Err(t, err, nil)
be.Equal(t, c.Input.Ext, "txt")
}
func TestTimeParse(t *testing.T) {
Overrides = map[string]string{"input.ext": "now"}
c, err := Load()
be.Err(t, err, "incompatible types: TOML value has type time.Time; destination has type string")
be.Equal(t, c.Input.Ext, "txt")
}
func TestStdoutMissing(t *testing.T) {
var oo Outputs = map[string]Output{}
std, en := oo.Stdout()
be.True(t, !en)
be.Equal(t, std, Stdout{})
}
func TestStdoutLoad(t *testing.T) {
os.Setenv("LOG_STDOUT_FORMAT", "json")
defer os.Unsetenv("LOG_STDOUT_FORMAT")
os.Setenv("LOG_STDOUT_ENABLED", "true")
defer os.Unsetenv("LOG_STDOUT_ENABLED")
c, _ := Load()
std, en := c.Outputs.Stdout()
be.True(t, en)
be.Equal(t, std.Format, "json")
}
func TestFormatJson(t *testing.T) {
ff := Formatters{
"json": map[string]any{"pretty_print": true},
}
js := ff.Json()
be.True(t, js.PrettyPrint)
ff = Formatters{}
js = ff.Json()
be.True(t, !js.PrettyPrint)
func TestRetrieveFromContext(t *testing.T) {
v := viper.New()
ctx := AddToContext(t.Context(), v)
result, _ := RetrieveFromContext(ctx)
be.True(t, v == result)
}