2023-01-01 09:00:07 +03:00
|
|
|
package jsonw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
"github.com/neilotoole/sq/cli/output/jsonw/internal"
|
2024-01-15 04:45:34 +03:00
|
|
|
jcolorenc "github.com/neilotoole/sq/cli/output/jsonw/internal/jcolorenc" //nolint:revive
|
2023-01-01 09:00:07 +03:00
|
|
|
"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.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewPingWriter(out io.Writer, pr *output.Printing) output.PingWriter {
|
|
|
|
return &pingWriter{out: out, pr: pr}
|
2023-01-01 09:00:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type pingWriter struct {
|
|
|
|
out io.Writer
|
2023-04-22 06:36:32 +03:00
|
|
|
pr *output.Printing
|
2023-01-01 09:00:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open implements output.PingWriter.
|
2023-04-01 11:38:32 +03:00
|
|
|
func (p pingWriter) Open(_ []*source.Source) error {
|
2023-01-01 09:00:07 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result implements output.PingWriter.
|
|
|
|
func (p pingWriter) Result(src *source.Source, d time.Duration, err error) error {
|
2024-01-27 10:11:24 +03:00
|
|
|
r := struct { //nolint:govet // field alignment
|
2023-01-01 09:00:07 +03:00
|
|
|
*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)
|
2023-04-22 06:36:32 +03:00
|
|
|
enc.SetColors(internal.NewColors(p.pr))
|
2023-01-01 09:00:07 +03:00
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
|
|
|
|
return errz.Err(enc.Encode(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements output.PingWriter.
|
|
|
|
func (p pingWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|