2023-01-01 06:17:44 +03:00
|
|
|
package jsonw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ output.SourceWriter = (*sourceWriter)(nil)
|
|
|
|
|
|
|
|
type sourceWriter struct {
|
|
|
|
out io.Writer
|
2023-04-22 06:36:32 +03:00
|
|
|
pr *output.Printing
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSourceWriter returns a source writer that outputs source
|
|
|
|
// details in text table format.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewSourceWriter(out io.Writer, pr *output.Printing) output.SourceWriter {
|
|
|
|
return &sourceWriter{out: out, pr: pr}
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Collection implements output.SourceWriter.
|
|
|
|
func (w *sourceWriter) Collection(coll *source.Collection) error {
|
|
|
|
if coll == nil {
|
2023-01-01 06:17:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// This is a bit hacky. Basically we want to JSON-print coll.Data().
|
2023-04-16 01:28:51 +03:00
|
|
|
// But, we want to do it just for the active group.
|
2023-04-19 08:28:09 +03:00
|
|
|
// So, our hack is that we clone the coll, and remove any
|
2023-04-16 01:28:51 +03:00
|
|
|
// sources that are not in the active group.
|
|
|
|
//
|
|
|
|
// This whole function, including what it outputs, should be revisited.
|
2023-04-19 08:28:09 +03:00
|
|
|
coll = coll.Clone()
|
|
|
|
group := coll.ActiveGroup()
|
2023-04-16 01:28:51 +03:00
|
|
|
|
|
|
|
// We store the active src handle
|
2023-04-19 08:28:09 +03:00
|
|
|
activeHandle := coll.ActiveHandle()
|
2023-04-16 01:28:51 +03:00
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
handles, err := coll.HandlesInGroup(group)
|
2023-04-16 01:28:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
srcs := coll.Sources()
|
2023-04-16 01:28:51 +03:00
|
|
|
for _, src := range srcs {
|
|
|
|
if !slices.Contains(handles, src.Handle) {
|
2023-04-19 08:28:09 +03:00
|
|
|
if err = coll.Remove(src.Handle); err != nil {
|
2023-04-16 01:28:51 +03:00
|
|
|
// Should never happen
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
srcs = coll.Sources()
|
2023-04-16 01:28:51 +03:00
|
|
|
for i := range srcs {
|
|
|
|
srcs[i].Location = srcs[i].RedactedLocation()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: we set the activeHandle back, even though that
|
|
|
|
// active source may have been removed (because it is not in
|
|
|
|
// the active group). This whole thing is a mess.
|
2023-04-19 08:28:09 +03:00
|
|
|
_, _ = coll.SetActive(activeHandle, true)
|
2023-04-16 01:28:51 +03:00
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
return writeJSON(w.out, w.pr, coll.Data())
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Source implements output.SourceWriter.
|
2023-04-19 08:28:09 +03:00
|
|
|
func (w *sourceWriter) Source(_ *source.Collection, src *source.Source) error {
|
2023-01-01 06:17:44 +03:00
|
|
|
if src == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
src = src.Clone()
|
|
|
|
src.Location = src.RedactedLocation()
|
2023-04-22 06:36:32 +03:00
|
|
|
return writeJSON(w.out, w.pr, src)
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removed implements output.SourceWriter.
|
|
|
|
func (w *sourceWriter) Removed(srcs ...*source.Source) error {
|
2023-04-22 06:36:32 +03:00
|
|
|
if !w.pr.Verbose || len(srcs) == 0 {
|
2023-01-01 06:17:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
srcs2 := make([]*source.Source, len(srcs))
|
|
|
|
for i := range srcs {
|
|
|
|
srcs2[i] = srcs[i].Clone()
|
|
|
|
srcs2[i].Location = srcs2[i].RedactedLocation()
|
|
|
|
}
|
2023-04-22 06:36:32 +03:00
|
|
|
return writeJSON(w.out, w.pr, srcs2)
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
2023-04-16 01:28:51 +03:00
|
|
|
|
|
|
|
// Group implements output.SourceWriter.
|
|
|
|
func (w *sourceWriter) Group(group *source.Group) error {
|
|
|
|
if group == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
group.RedactLocations()
|
2023-04-22 06:36:32 +03:00
|
|
|
return writeJSON(w.out, w.pr, group)
|
2023-04-16 01:28:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetActiveGroup implements output.SourceWriter.
|
|
|
|
func (w *sourceWriter) SetActiveGroup(group *source.Group) error {
|
|
|
|
if group == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
group.RedactLocations()
|
2023-04-22 06:36:32 +03:00
|
|
|
return writeJSON(w.out, w.pr, group)
|
2023-04-16 01:28:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Groups implements output.SourceWriter.
|
|
|
|
func (w *sourceWriter) Groups(tree *source.Group) error {
|
|
|
|
tree.RedactLocations()
|
2023-04-22 06:36:32 +03:00
|
|
|
return writeJSON(w.out, w.pr, tree)
|
2023-04-16 01:28:51 +03:00
|
|
|
}
|