2024-03-07 10:10:54 -06:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-07 21:19:45 -06:00
|
|
|
type formatMaker func(config.Formatters) (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-07 10:10:54 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 16:05:59 -06:00
|
|
|
func Preferred() (f Formatter, err error) {
|
|
|
|
|
conf, err := config.Load()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
std, _ := conf.Outputs.Stdout()
|
2024-03-10 21:08:34 -05:00
|
|
|
return New(std.Format)
|
2024-03-09 16:05:59 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-07 10:10:54 -06:00
|
|
|
func New(kind string) (f Formatter, err error) {
|
|
|
|
|
conf, err := config.Load()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if make, ok := formatterMap[kind]; ok {
|
2024-03-07 21:19:45 -06:00
|
|
|
return make(conf.Formatters)
|
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
|
|
|
|
|
}
|