41 lines
844 B
Go
41 lines
844 B
Go
package formatters
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type formatMaker func(config *viper.Viper) (Formatter, error)
|
|
|
|
var formatterMap = map[string]formatMaker{
|
|
"plain": newPlain,
|
|
"json": newJson,
|
|
"zero": newNull,
|
|
}
|
|
|
|
func Preferred(ctx context.Context) (f Formatter, err error) {
|
|
_, c := config.RetrieveFromContext(ctx)
|
|
return New(ctx, c.Formatters.Preferred)
|
|
}
|
|
|
|
func New(ctx context.Context, kind string) (f Formatter, err error) {
|
|
v, _ := config.RetrieveFromContext(ctx)
|
|
formatterConf := v.Sub("formatters." + kind)
|
|
|
|
if maker, ok := formatterMap[kind]; ok {
|
|
return maker(formatterConf)
|
|
}
|
|
|
|
return nil, fmt.Errorf("unimplemented format: %s", kind)
|
|
}
|
|
|
|
func Kinds() []string {
|
|
r := []string{}
|
|
for kind, _ := range formatterMap {
|
|
r = append(r, kind)
|
|
}
|
|
return r
|
|
}
|