Compare commits
No commits in common. "68ccf2d9facb2ca78e6445c8639a69ce8f26c41c" and "0fbd64247a385365cfadbe210ec94d6fc566b367" have entirely different histories.
68ccf2d9fa
...
0fbd64247a
11 changed files with 52 additions and 104 deletions
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
## Config System
|
## Config System
|
||||||
- Viper instance is stored in context using a custom key type (`confKeyType`)
|
- Viper instance is stored in context using a custom key type (`confKeyType`)
|
||||||
- Use `config.New(ctx, path)` to create a new viper instance
|
- Use `config.New(path, overrides)` to create a new viper instance
|
||||||
- Use `config.RetrieveFromContext(ctx)` to get both viper and the unmarshaled Config struct
|
- 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
|
- 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`
|
- Test files must create viper instances and add them to context using `config.AddToContext`
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,5 @@
|
||||||
# Changelog
|
# 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
|
## [0.3.0] - 2026-03-09
|
||||||
|
|
||||||
- Refactor configuration to use viper with context propagation instead of global variables
|
- Refactor configuration to use viper with context propagation instead of global variables
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,12 @@ import (
|
||||||
fp "path/filepath"
|
fp "path/filepath"
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/config"
|
"codeberg.org/danjones000/my-log/config"
|
||||||
"github.com/pelletier/go-toml/v2"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ConfigCmd = &cobra.Command{
|
var ConfigCmd = &cobra.Command{
|
||||||
Use: "config",
|
Use: "config",
|
||||||
Short: "Save default config to file, or print the current config value",
|
Short: "Save default config to file",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||||
print, _ := cmd.Flags().GetBool("print")
|
print, _ := cmd.Flags().GetBool("print")
|
||||||
|
|
@ -36,24 +35,6 @@ var ConfigCmd = &cobra.Command{
|
||||||
fmt.Fprintln(cmd.OutOrStdout(), config.DefaultPath())
|
fmt.Fprintln(cmd.OutOrStdout(), config.DefaultPath())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) > 0 {
|
|
||||||
v, _ := config.RetrieveFromContext(cmd.Context())
|
|
||||||
val := v.Get(args[0])
|
|
||||||
var out []byte
|
|
||||||
if val == nil {
|
|
||||||
out = []byte("<nil>")
|
|
||||||
} 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")
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
configPath := config.DefaultPath()
|
configPath := config.DefaultPath()
|
||||||
if !force {
|
if !force {
|
||||||
|
|
|
||||||
17
cli/root.go
17
cli/root.go
|
|
@ -28,12 +28,19 @@ var RootCmd = &cobra.Command{
|
||||||
Use: "my-log",
|
Use: "my-log",
|
||||||
Short: "A brief description of your application",
|
Short: "A brief description of your application",
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if configPath != "" {
|
||||||
|
v.SetConfigFile(configPath)
|
||||||
|
err := v.ReadInConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for k, val := range configValues {
|
for k, val := range configValues {
|
||||||
v.Set(k, val)
|
v.Set(k, val)
|
||||||
}
|
}
|
||||||
|
|
@ -55,10 +62,6 @@ var configPath string
|
||||||
var configValues map[string]string
|
var configValues map[string]string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
path := os.Getenv("MYLOG_CONFIG_PATH")
|
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file")
|
||||||
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")
|
RootCmd.PersistentFlags().StringToStringVarP(&configValues, "config-value", "v", nil, "Override config values. Use dot syntax to specify key. E.g. -v formatters.preferred=json")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ func AddToContext(ctx context.Context, v *viper.Viper) context.Context {
|
||||||
return context.WithValue(ctx, viperKey, v)
|
return context.WithValue(ctx, viperKey, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, path string) (context.Context, *viper.Viper, error) {
|
func New(ctx context.Context) (context.Context, *viper.Viper, error) {
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetConfigType("toml")
|
v.SetConfigType("toml")
|
||||||
|
|
||||||
|
|
@ -40,16 +40,12 @@ func New(ctx context.Context, path string) (context.Context, *viper.Viper, error
|
||||||
return ctx, nil, err
|
return ctx, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
v.SetConfigFile(DefaultPath())
|
||||||
path = DefaultPath()
|
|
||||||
}
|
|
||||||
|
|
||||||
v.SetConfigFile(path)
|
|
||||||
v.SetEnvPrefix("MYLOG")
|
v.SetEnvPrefix("MYLOG")
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
||||||
|
|
||||||
if err := v.MergeInConfig(); err != nil {
|
if err := v.ReadInConfig(); err != nil {
|
||||||
return ctx, nil, err
|
return ctx, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
_, v, err := New(t.Context(), "")
|
_, v, err := New(t.Context())
|
||||||
be.Err(t, err, nil)
|
be.Err(t, err, nil)
|
||||||
be.True(t, v != nil)
|
be.True(t, v != nil)
|
||||||
}
|
}
|
||||||
|
|
@ -18,7 +18,7 @@ func TestNewWithEnvOverrides(t *testing.T) {
|
||||||
os.Setenv("MYLOG_INPUT_PATH", "/test/path")
|
os.Setenv("MYLOG_INPUT_PATH", "/test/path")
|
||||||
defer os.Unsetenv("MYLOG_INPUT_PATH")
|
defer os.Unsetenv("MYLOG_INPUT_PATH")
|
||||||
|
|
||||||
_, v, err := New(t.Context(), "")
|
_, v, err := New(t.Context())
|
||||||
be.Err(t, err, nil)
|
be.Err(t, err, nil)
|
||||||
be.Equal(t, v.GetString("input.path"), "/test/path")
|
be.Equal(t, v.GetString("input.path"), "/test/path")
|
||||||
}
|
}
|
||||||
|
|
@ -32,9 +32,12 @@ path = "/file/path"
|
||||||
ext = "log"`)
|
ext = "log"`)
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
_, v, err := New(t.Context(), f.Name())
|
_, v, err := New(t.Context())
|
||||||
be.Err(t, err, nil)
|
be.Err(t, err, nil)
|
||||||
|
|
||||||
|
v.SetConfigFile(f.Name())
|
||||||
|
v.SetConfigType("toml")
|
||||||
|
err = v.ReadInConfig()
|
||||||
be.Err(t, err, nil)
|
be.Err(t, err, nil)
|
||||||
be.Equal(t, v.GetString("input.path"), "/file/path")
|
be.Equal(t, v.GetString("input.path"), "/file/path")
|
||||||
be.Equal(t, v.GetString("input.ext"), "log")
|
be.Equal(t, v.GetString("input.ext"), "log")
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,6 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FormatJSON string = "json"
|
|
||||||
|
|
||||||
func newJson(ff *viper.Viper) (Formatter, error) {
|
func newJson(ff *viper.Viper) (Formatter, error) {
|
||||||
js := new(Json)
|
js := new(Json)
|
||||||
err := ff.Unmarshal(js)
|
err := ff.Unmarshal(js)
|
||||||
|
|
@ -26,7 +24,7 @@ type Json struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (js *Json) Name() string {
|
func (js *Json) Name() string {
|
||||||
return FormatJSON
|
return "json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (js *Json) marshal(v any) (o []byte, err error) {
|
func (js *Json) marshal(v any) (o []byte, err error) {
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,18 @@ package formatters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/config"
|
"codeberg.org/danjones000/my-log/config"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mut sync.RWMutex
|
type formatMaker func(config *viper.Viper) (Formatter, error)
|
||||||
|
|
||||||
type FormatInit func(config *viper.Viper) (Formatter, error)
|
var formatterMap = map[string]formatMaker{
|
||||||
|
"plain": newPlain,
|
||||||
var formatterMap = map[string]FormatInit{
|
"json": newJson,
|
||||||
FormatPlain: newPlain,
|
"zero": newNull,
|
||||||
FormatJSON: newJson,
|
|
||||||
FormatNull: newNull,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Preferred(ctx context.Context) (f Formatter, err error) {
|
func Preferred(ctx context.Context) (f Formatter, err error) {
|
||||||
|
|
@ -29,8 +25,6 @@ func New(ctx context.Context, kind string) (f Formatter, err error) {
|
||||||
v, _ := config.RetrieveFromContext(ctx)
|
v, _ := config.RetrieveFromContext(ctx)
|
||||||
formatterConf := v.Sub("formatters." + kind)
|
formatterConf := v.Sub("formatters." + kind)
|
||||||
|
|
||||||
mut.RLock()
|
|
||||||
defer mut.RUnlock()
|
|
||||||
if maker, ok := formatterMap[kind]; ok {
|
if maker, ok := formatterMap[kind]; ok {
|
||||||
return maker(formatterConf)
|
return maker(formatterConf)
|
||||||
}
|
}
|
||||||
|
|
@ -40,24 +34,8 @@ func New(ctx context.Context, kind string) (f Formatter, err error) {
|
||||||
|
|
||||||
func Kinds() []string {
|
func Kinds() []string {
|
||||||
r := []string{}
|
r := []string{}
|
||||||
|
|
||||||
mut.RLock()
|
|
||||||
defer mut.RUnlock()
|
|
||||||
for kind, _ := range formatterMap {
|
for kind, _ := range formatterMap {
|
||||||
r = append(r, kind)
|
r = append(r, kind)
|
||||||
}
|
}
|
||||||
return r
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -39,17 +39,3 @@ func TestPreferred(t *testing.T) {
|
||||||
be.Err(t, err, nil)
|
be.Err(t, err, nil)
|
||||||
be.True(t, form != 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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,28 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FormatNull = "zero"
|
func newNull(ff *viper.Viper) (Formatter, error) {
|
||||||
|
return &Null{}, nil
|
||||||
func newNull(*viper.Viper) (Formatter, error) {
|
|
||||||
return Null{}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Null struct{}
|
type Null struct{}
|
||||||
|
|
||||||
func (Null) Name() string {
|
func (n *Null) Name() string {
|
||||||
return FormatNull
|
return "zero"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Null) Meta(m models.Meta) (o []byte, err error) { return }
|
func (n *Null) Meta(m models.Meta) (o []byte, err error) {
|
||||||
func (Null) Entry(e models.Entry) (o []byte, err error) { return }
|
return
|
||||||
func (Null) Log(l models.Log) (o []byte, err error) { return }
|
}
|
||||||
func (Null) Logs(logs []models.Log) (out []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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,16 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FormatPlain string = "plain"
|
func newPlain(ff *viper.Viper) (Formatter, error) {
|
||||||
|
|
||||||
func newPlain(*viper.Viper) (Formatter, error) {
|
|
||||||
return &PlainText{}, nil
|
return &PlainText{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlainText struct{}
|
type PlainText struct {
|
||||||
|
// config might go here some day
|
||||||
|
}
|
||||||
|
|
||||||
func (*PlainText) Name() string {
|
func (pt *PlainText) Name() string {
|
||||||
return FormatPlain
|
return "plain"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pt *PlainText) Logs(logs []models.Log) (out []byte, err error) {
|
func (pt *PlainText) Logs(logs []models.Log) (out []byte, err error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue