2020-08-06 20:58:47 +03:00
|
|
|
// Package output provides interfaces and implementations for
|
|
|
|
// outputting data and messages. All sq command output should be
|
|
|
|
// via one of the writer interfaces defined in this package.
|
|
|
|
// The RecordWriterAdapter type provides a bridge between the
|
|
|
|
// asynchronous libsq.RecordWriter interface and the simpler
|
|
|
|
// synchronous RecordWriter interface defined here.
|
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2023-05-22 18:08:14 +03:00
|
|
|
"io"
|
2020-08-06 20:58:47 +03:00
|
|
|
"time"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
2023-06-21 15:28:15 +03:00
|
|
|
"github.com/neilotoole/sq/cli/hostinfo"
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/record"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RecordWriter is an interface for writing records to a destination.
|
2023-01-01 06:17:44 +03:00
|
|
|
// In effect, it is a synchronous counterpart to the asynchronous
|
2020-08-06 20:58:47 +03:00
|
|
|
// libsq.RecordWriter interface. Being a synchronous interface, it is
|
|
|
|
// less tricky to implement than libsq.RecordWriter. The RecordWriterAdapter
|
|
|
|
// type defined in this package bridges the two interfaces.
|
|
|
|
//
|
|
|
|
// The Open method must be invoked before WriteRecords. Close must be
|
|
|
|
// invoked when all records are written. The Flush method advises the
|
|
|
|
// writer to flush any internal buffers.
|
|
|
|
type RecordWriter interface {
|
|
|
|
// Open instructs the writer to prepare to write records
|
|
|
|
// described by recMeta.
|
2023-05-22 18:08:14 +03:00
|
|
|
Open(recMeta record.Meta) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// WriteRecords writes rec to the destination.
|
2023-05-22 18:08:14 +03:00
|
|
|
WriteRecords(recs []record.Record) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Flush advises the writer to flush any internal
|
|
|
|
// buffer. Note that the writer may implement an independent
|
|
|
|
// flushing strategy, or may not buffer at all.
|
|
|
|
Flush() error
|
|
|
|
|
|
|
|
// Close closes the writer after flushing any internal buffer.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetadataWriter can output metadata.
|
|
|
|
type MetadataWriter interface {
|
|
|
|
// TableMetadata writes the table metadata.
|
|
|
|
TableMetadata(tblMeta *source.TableMetadata) error
|
|
|
|
|
|
|
|
// SourceMetadata writes the source metadata.
|
2023-06-22 08:48:58 +03:00
|
|
|
SourceMetadata(srcMeta *source.Metadata, showSchema bool) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// DBProperties writes the DB properties.
|
|
|
|
DBProperties(props map[string]any) error
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// DriverMetadata writes the metadata for the drivers.
|
|
|
|
DriverMetadata(drvrs []driver.Metadata) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SourceWriter can output data source details.
|
|
|
|
type SourceWriter interface {
|
2023-04-19 08:28:09 +03:00
|
|
|
// Collection outputs details of the collection. Specifically it prints
|
|
|
|
// the sources from coll's active group.
|
|
|
|
Collection(coll *source.Collection) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Source outputs details of the source.
|
2023-04-19 08:28:09 +03:00
|
|
|
Source(coll *source.Collection, src *source.Source) error
|
2023-01-01 06:17:44 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// Added is called when src is added to the collection.
|
|
|
|
Added(coll *source.Collection, src *source.Source) error
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Removed is called when sources are removed from the collection.
|
2023-01-01 06:17:44 +03:00
|
|
|
Removed(srcs ...*source.Source) error
|
2023-04-16 01:28:51 +03:00
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// Moved is called when a source is moved from old to nu.
|
|
|
|
Moved(coll *source.Collection, old, nu *source.Source) error
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
// Group prints the group.
|
|
|
|
Group(group *source.Group) error
|
|
|
|
|
|
|
|
// SetActiveGroup is called when the group is set.
|
|
|
|
SetActiveGroup(group *source.Group) error
|
|
|
|
|
|
|
|
// Groups prints a list of groups.
|
|
|
|
Groups(tree *source.Group) error
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorWriter outputs errors.
|
|
|
|
type ErrorWriter interface {
|
|
|
|
// Error outputs err.
|
|
|
|
Error(err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PingWriter writes ping results.
|
|
|
|
type PingWriter interface {
|
|
|
|
// Open opens the writer to write the supplied sources.
|
2023-01-01 09:00:07 +03:00
|
|
|
Open(srcs []*source.Source) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Result prints a ping result. The ping succeeded if
|
|
|
|
// err is nil. If err is context.DeadlineExceeded, the d
|
|
|
|
// arg will be the timeout value.
|
2023-01-01 09:00:07 +03:00
|
|
|
Result(src *source.Source, d time.Duration, err error) error
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Close is called after all results have been received.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:10:56 +03:00
|
|
|
// VersionWriter prints the CLI version.
|
|
|
|
type VersionWriter interface {
|
|
|
|
// Version prints version info. Arg latestVersion is the latest
|
|
|
|
// version available from the homebrew repository. The value
|
|
|
|
// may be empty.
|
2023-06-21 15:28:15 +03:00
|
|
|
Version(bi buildinfo.BuildInfo, latestVersion string, si hostinfo.Info) error
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// ConfigWriter prints config.
|
|
|
|
type ConfigWriter interface {
|
|
|
|
// Location prints the config location. The origin may be empty, or one
|
|
|
|
// of "flag", "env", "default".
|
|
|
|
Location(loc, origin string) error
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
// Opt prints a single options.Opt.
|
|
|
|
Opt(o options.Options, opt options.Opt) error
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Options prints config options.
|
2023-05-01 06:59:34 +03:00
|
|
|
Options(reg *options.Registry, o options.Options) error
|
|
|
|
|
|
|
|
// SetOption is called when an option is set.
|
2023-05-05 17:32:50 +03:00
|
|
|
SetOption(o options.Options, opt options.Opt) error
|
|
|
|
|
|
|
|
// UnsetOption is called when an option is unset.
|
|
|
|
UnsetOption(opt options.Opt) error
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
2023-05-19 17:24:18 +03:00
|
|
|
|
|
|
|
// Writers is a container for the various output Writers.
|
|
|
|
type Writers struct {
|
|
|
|
Printing *Printing
|
|
|
|
|
|
|
|
Record RecordWriter
|
|
|
|
Metadata MetadataWriter
|
|
|
|
Source SourceWriter
|
|
|
|
Error ErrorWriter
|
|
|
|
Ping PingWriter
|
|
|
|
Version VersionWriter
|
|
|
|
Config ConfigWriter
|
|
|
|
}
|
2023-05-22 18:08:14 +03:00
|
|
|
|
|
|
|
// NewRecordWriterFunc is a func type that returns an output.RecordWriter.
|
|
|
|
type NewRecordWriterFunc func(out io.Writer, pr *Printing) RecordWriter
|