♲ 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:
parent
d34363b8c0
commit
9f05f933dd
21 changed files with 338 additions and 360 deletions
|
|
@ -3,16 +3,27 @@ package formatters
|
|||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"codeberg.org/danjones000/my-log/models"
|
||||
"codeberg.org/danjones000/my-log/tools"
|
||||
"github.com/nalgeon/be"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func setupPlainTestContext(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
v := viper.New()
|
||||
v.SetConfigType("toml")
|
||||
return config.AddToContext(t.Context(), v)
|
||||
}
|
||||
|
||||
func TestPlainLogs(t *testing.T) {
|
||||
ctx := setupPlainTestContext(t)
|
||||
m := []models.Meta{
|
||||
{Key: "foo", Value: "bar"},
|
||||
{Key: "baz", Value: 42},
|
||||
|
|
@ -29,7 +40,7 @@ func TestPlainLogs(t *testing.T) {
|
|||
l2 := models.Log{Name: "more-stuff", Entries: []models.Entry{e2}}
|
||||
logs := []models.Log{l, l2}
|
||||
|
||||
f, err := New("plain")
|
||||
f, err := New(ctx, "plain")
|
||||
be.Err(t, err, nil)
|
||||
|
||||
out, err := f.Logs(logs)
|
||||
|
|
@ -95,40 +106,46 @@ func TestPlainLogs(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPlainName(t *testing.T) {
|
||||
f, _ := New("plain")
|
||||
ctx := setupPlainTestContext(t)
|
||||
f, _ := New(ctx, "plain")
|
||||
be.Equal(t, f.Name(), "plain")
|
||||
}
|
||||
|
||||
func TestPlainLogNone(t *testing.T) {
|
||||
f, _ := New("plain")
|
||||
ctx := setupPlainTestContext(t)
|
||||
f, _ := New(ctx, "plain")
|
||||
out, err := f.Logs([]models.Log{})
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(out), 0)
|
||||
}
|
||||
|
||||
func TestPlainLogNoEntries(t *testing.T) {
|
||||
f, _ := New("plain")
|
||||
ctx := setupPlainTestContext(t)
|
||||
f, _ := New(ctx, "plain")
|
||||
out, err := f.Log(models.Log{Name: "foo"})
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(out), 0)
|
||||
}
|
||||
|
||||
func TestPlainMetaEmpty(t *testing.T) {
|
||||
f, _ := New("plain")
|
||||
ctx := setupPlainTestContext(t)
|
||||
f, _ := New(ctx, "plain")
|
||||
out, err := f.Meta(models.Meta{Key: "foo", Value: ""})
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(out), 0)
|
||||
}
|
||||
|
||||
func TestPlainMetaError(t *testing.T) {
|
||||
f, _ := New("plain")
|
||||
ctx := setupPlainTestContext(t)
|
||||
f, _ := New(ctx, "plain")
|
||||
out, err := f.Meta(models.Meta{Key: "foo", Value: make(chan bool)})
|
||||
be.Err(t, err)
|
||||
be.Equal(t, len(out), 0)
|
||||
}
|
||||
|
||||
func TestPlainEntry(t *testing.T) {
|
||||
f, _ := New("plain")
|
||||
ctx := setupPlainTestContext(t)
|
||||
f, _ := New(ctx, "plain")
|
||||
now := time.Now()
|
||||
out, err := f.Entry(models.Entry{
|
||||
Title: "foo",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue