api/internal/format/console_writer.go
Peter Kurfer 49e58ac2e4 Add advanced matching options to HTTP handler
- move to Gitlab
- make code better testable
- create app abstraction for server
- cleanup
2020-12-26 13:11:49 +00:00

47 lines
976 B
Go

package format
import (
"encoding/json"
"io"
"strings"
"github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v3"
)
type consoleWriterFactory func(io.Writer) ConsoleWriter
var (
writers = map[string]consoleWriterFactory{
"table": func(writer io.Writer) ConsoleWriter {
tw := tablewriter.NewWriter(writer)
tw.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
tw.SetCenterSeparator("|")
return &tblWriter{
tableWriter: tw,
}
},
"json": func(writer io.Writer) ConsoleWriter {
return &jsonWriter{
encoder: json.NewEncoder(writer),
}
},
"yaml": func(writer io.Writer) ConsoleWriter {
return &yamlWriter{
encoder: yaml.NewEncoder(writer),
}
},
}
)
func Writer(format string, writer io.Writer) ConsoleWriter {
if cw, ok := writers[strings.ToLower(format)]; ok {
return cw(writer)
}
return writers["table"](writer)
}
type ConsoleWriter interface {
Write(in interface{}) (err error)
}