2024-03-07 10:10:54 -06:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-08 22:59:33 -05:00
|
|
|
"context"
|
2024-03-07 10:10:54 -06:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-08 22:59:33 -05:00
|
|
|
type formatMaker func(config map[string]any) (Formatter, error)
|
2024-03-07 10:10:54 -06:00
|
|
|
|
|
|
|
|
var formatterMap = map[string]formatMaker{
|
|
|
|
|
"plain": newPlain,
|
2024-03-10 11:33:51 -05:00
|
|
|
"json": newJson,
|
2024-03-11 21:18:52 -05:00
|
|
|
"zero": newNull,
|
2024-03-07 10:10:54 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-08 22:59:33 -05:00
|
|
|
func Preferred(ctx context.Context) (f Formatter, err error) {
|
|
|
|
|
v, _ := config.RetrieveFromContext(ctx)
|
|
|
|
|
format := v.GetString("output.stdout.config.format")
|
|
|
|
|
return New(ctx, format)
|
2024-03-09 16:05:59 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-08 22:59:33 -05:00
|
|
|
func New(ctx context.Context, kind string) (f Formatter, err error) {
|
|
|
|
|
_, c := config.RetrieveFromContext(ctx)
|
|
|
|
|
conf := c.Formatters
|
2024-03-07 10:10:54 -06:00
|
|
|
|
|
|
|
|
if make, ok := formatterMap[kind]; ok {
|
2026-03-08 22:59:33 -05:00
|
|
|
var formatterConf map[string]any
|
|
|
|
|
if cf, ok := conf[kind]; ok {
|
|
|
|
|
formatterConf = cf
|
|
|
|
|
}
|
|
|
|
|
return make(formatterConf)
|
2024-03-07 10:10:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("unimplemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Kinds() []string {
|
|
|
|
|
r := []string{}
|
|
|
|
|
for kind, _ := range formatterMap {
|
|
|
|
|
r = append(r, kind)
|
|
|
|
|
}
|
|
|
|
|
return r
|
|
|
|
|
}
|