-
Notifications
You must be signed in to change notification settings - Fork 13
/
pingwriter.go
59 lines (48 loc) · 1.33 KB
/
pingwriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package jsonw
import (
"io"
"time"
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/cli/output/jsonw/internal"
"github.com/neilotoole/sq/cli/output/jsonw/internal/jcolorenc"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/source"
)
var _ output.PingWriter = (*pingWriter)(nil)
// NewPingWriter returns JSON impl of output.PingWriter.
func NewPingWriter(out io.Writer, pr *output.Printing) output.PingWriter {
return &pingWriter{out: out, pr: pr}
}
type pingWriter struct {
out io.Writer
pr *output.Printing
}
// Open implements output.PingWriter.
func (p pingWriter) Open(_ []*source.Source) error {
return nil
}
// Result implements output.PingWriter.
func (p pingWriter) Result(src *source.Source, d time.Duration, err error) error {
r := struct { //nolint:govet // field alignment
*source.Source
Pong bool `json:"pong"`
Duration time.Duration `json:"duration"`
Error string `json:"error,omitempty"`
}{
Source: src,
Pong: err == nil,
Duration: d,
}
if err != nil {
r.Error = err.Error()
}
enc := jcolorenc.NewEncoder(p.out)
enc.SetColors(internal.NewColors(p.pr))
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
return errz.Err(enc.Encode(r))
}
// Close implements output.PingWriter.
func (p pingWriter) Close() error {
return nil
}