✨ Allow formatters to use sub-config with their own config structs
This commit is contained in:
parent
9f05f933dd
commit
6c6a959af4
4 changed files with 21 additions and 21 deletions
|
|
@ -3,23 +3,24 @@ package formatters
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/models"
|
"codeberg.org/danjones000/my-log/models"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newJson(ff map[string]any) (Formatter, error) {
|
func newJson(ff *viper.Viper) (Formatter, error) {
|
||||||
prettyPrint := false
|
js := new(Json)
|
||||||
if jf, ok := ff["json"].(map[string]any); ok {
|
err := ff.Unmarshal(js)
|
||||||
if pp, ok := jf["pretty_print"].(bool); ok {
|
if err != nil {
|
||||||
prettyPrint = pp
|
return nil, fmt.Errorf("failed to get json config: %w", err)
|
||||||
}
|
}
|
||||||
}
|
return js, nil
|
||||||
return &Json{prettyPrint}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Json struct {
|
type Json struct {
|
||||||
prettPrint bool
|
prettPrint bool `mapstructure:"pretty_print"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (js *Json) Name() string {
|
func (js *Json) Name() string {
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,13 @@ package formatters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/config"
|
"codeberg.org/danjones000/my-log/config"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type formatMaker func(config map[string]any) (Formatter, error)
|
type formatMaker func(config *viper.Viper) (Formatter, error)
|
||||||
|
|
||||||
var formatterMap = map[string]formatMaker{
|
var formatterMap = map[string]formatMaker{
|
||||||
"plain": newPlain,
|
"plain": newPlain,
|
||||||
|
|
@ -22,18 +23,14 @@ func Preferred(ctx context.Context) (f Formatter, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, kind string) (f Formatter, err error) {
|
func New(ctx context.Context, kind string) (f Formatter, err error) {
|
||||||
_, c := config.RetrieveFromContext(ctx)
|
v, _ := config.RetrieveFromContext(ctx)
|
||||||
conf := c.Formatters
|
formatterConf := v.Sub("formatters." + kind)
|
||||||
|
|
||||||
if make, ok := formatterMap[kind]; ok {
|
if maker, ok := formatterMap[kind]; ok {
|
||||||
var formatterConf map[string]any
|
return maker(formatterConf)
|
||||||
if cf, ok := conf[kind]; ok {
|
|
||||||
formatterConf = cf
|
|
||||||
}
|
|
||||||
return make(formatterConf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New("unimplemented")
|
return nil, fmt.Errorf("unimplemented format: %s", kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Kinds() []string {
|
func Kinds() []string {
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,10 @@ package formatters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"codeberg.org/danjones000/my-log/models"
|
"codeberg.org/danjones000/my-log/models"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newNull(ff map[string]any) (Formatter, error) {
|
func newNull(ff *viper.Viper) (Formatter, error) {
|
||||||
return &Null{}, nil
|
return &Null{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,10 @@ import (
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/models"
|
"codeberg.org/danjones000/my-log/models"
|
||||||
"codeberg.org/danjones000/my-log/tools"
|
"codeberg.org/danjones000/my-log/tools"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newPlain(ff map[string]any) (Formatter, error) {
|
func newPlain(ff *viper.Viper) (Formatter, error) {
|
||||||
return &PlainText{}, nil
|
return &PlainText{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue