sq/cli/output/jsonw/metadataw.go
Neil O'Toole 58ccfc9ded
Json driver; refactoring of core packages (#66)
* Type Detector refactor

* json driver impl; refactoring of source.Files reader et al

* working on kind detector

* significant switcheroo of packages

* partway throught refactoring Kind

* major package switcheroo for Kind
2020-08-23 04:42:15 -06:00

65 lines
1.5 KiB
Go

package jsonw
import (
"bytes"
"fmt"
"io"
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/cli/output/jsonw/internal"
jcolorenc "github.com/neilotoole/sq/cli/output/jsonw/internal/jcolorenc"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source"
)
// mdWriter implements output.MetadataWriter for JSON.
type mdWriter struct {
out io.Writer
fm *output.Formatting
}
// NewMetadataWriter returns a new output.MetadataWriter instance
// that outputs metadata in JSON.
func NewMetadataWriter(out io.Writer, fm *output.Formatting) output.MetadataWriter {
return &mdWriter{out: out, fm: fm}
}
func (w *mdWriter) write(v interface{}) error {
buf := &bytes.Buffer{}
enc := jcolorenc.NewEncoder(buf)
enc.SetColors(internal.NewColors(w.fm))
enc.SetEscapeHTML(false)
if w.fm.Pretty {
enc.SetIndent("", w.fm.Indent)
}
err := enc.Encode(v)
if err != nil {
return errz.Err(err)
}
_, err = fmt.Fprint(w.out, buf.String())
if err != nil {
return errz.Err(err)
}
return nil
}
// DriverMetadata implements output.MetadataWriter.
func (w *mdWriter) DriverMetadata(md []driver.Metadata) error {
return w.write(md)
}
// TableMetadata implements output.MetadataWriter.
func (w *mdWriter) TableMetadata(md *source.TableMetadata) error {
return w.write(md)
}
// SourceMetadata implements output.MetadataWriter.
func (w *mdWriter) SourceMetadata(md *source.Metadata) error {
return w.write(md)
}