2023-04-19 08:28:09 +03:00
|
|
|
package yamlw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
yamlp "github.com/goccy/go-yaml/printer"
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mdWriter implements output.MetadataWriter for YAML.
|
|
|
|
type mdWriter struct {
|
|
|
|
out io.Writer
|
2023-04-22 06:36:32 +03:00
|
|
|
pr *output.Printing
|
|
|
|
yp yamlp.Printer
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetadataWriter returns a new output.MetadataWriter instance
|
|
|
|
// that outputs metadata in JSON.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewMetadataWriter(out io.Writer, pr *output.Printing) output.MetadataWriter {
|
|
|
|
return &mdWriter{out: out, pr: pr, yp: newPrinter(pr)}
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DriverMetadata implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) DriverMetadata(md []driver.Metadata) error {
|
2023-05-05 17:32:50 +03:00
|
|
|
return writeYAML(w.out, w.yp, md)
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableMetadata implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) TableMetadata(md *source.TableMetadata) error {
|
2023-05-05 17:32:50 +03:00
|
|
|
return writeYAML(w.out, w.yp, md)
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SourceMetadata implements output.MetadataWriter.
|
2023-06-22 08:48:58 +03:00
|
|
|
func (w *mdWriter) SourceMetadata(md *source.Metadata, showSchema bool) error {
|
2023-04-19 08:28:09 +03:00
|
|
|
md2 := *md // Shallow copy is fine
|
|
|
|
md2.Location = source.RedactLocation(md2.Location)
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
if showSchema {
|
|
|
|
return writeYAML(w.out, w.yp, &md2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't render "tables", "table_count", and "view_count"
|
|
|
|
type mdNoSchema struct {
|
|
|
|
source.Metadata `yaml:",omitempty,inline"`
|
|
|
|
Tables *[]*source.TableMetadata `yaml:"tables,omitempty"`
|
|
|
|
TableCount *int64 `yaml:"table_count,omitempty"`
|
|
|
|
ViewCount *int64 `yaml:"view_count,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeYAML(w.out, w.yp, &mdNoSchema{Metadata: md2})
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
2023-05-19 17:24:18 +03:00
|
|
|
|
|
|
|
// DBProperties implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) DBProperties(props map[string]any) error {
|
|
|
|
if len(props) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeYAML(w.out, w.yp, props)
|
|
|
|
}
|