sq/cli/output/csvw/pingwriter.go

54 lines
1.0 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package csvw
import (
"context"
"encoding/csv"
"io"
"time"
"github.com/neilotoole/sq/libsq/errz"
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/libsq/source"
)
// NewPingWriter returns a new instance.
func NewPingWriter(out io.Writer, sep rune) output.PingWriter {
csvw := csv.NewWriter(out)
csvw.Comma = sep
return &pingWriter{csvw: csvw}
}
// pingWriter implements out.pingWriter.
type pingWriter struct {
csvw *csv.Writer
}
// Open implements output.PingWriter.
func (p *pingWriter) Open(srcs []*source.Source) {
}
// Result implements output.PingWriter.
func (p *pingWriter) Result(src *source.Source, d time.Duration, err error) {
rec := make([]string, 3)
rec[0] = src.Handle
rec[1] = d.Truncate(time.Millisecond).String()
if err != nil {
if err == context.DeadlineExceeded {
rec[2] = "timeout exceeded"
} else {
rec[2] = err.Error()
}
}
_ = p.csvw.Write(rec)
p.csvw.Flush()
}
// Close implements output.PingWriter.
func (p *pingWriter) Close() error {
p.csvw.Flush()
return errz.Err(p.csvw.Error())
}