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-11-22 00:49:52 +03:00
|
|
|
"github.com/samber/lo"
|
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"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/metadata"
|
2023-04-19 08:28:09 +03:00
|
|
|
)
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
var _ output.MetadataWriter = (*mdWriter)(nil)
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// 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.
|
2023-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) TableMetadata(md *metadata.Table) 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-11-21 00:42:38 +03:00
|
|
|
func (w *mdWriter) SourceMetadata(md *metadata.Source, 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 {
|
2023-11-21 00:42:38 +03:00
|
|
|
metadata.Source `yaml:",omitempty,inline"`
|
|
|
|
Tables *[]*metadata.Table `yaml:"tables,omitempty"`
|
|
|
|
TableCount *int64 `yaml:"table_count,omitempty"`
|
|
|
|
ViewCount *int64 `yaml:"view_count,omitempty"`
|
2023-06-22 08:48:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
return writeYAML(w.out, w.yp, &mdNoSchema{Source: 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)
|
|
|
|
}
|
2023-11-21 00:42:38 +03:00
|
|
|
|
|
|
|
// Catalogs implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) Catalogs(currentCatalog string, catalogs []string) error {
|
|
|
|
if len(catalogs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type cat struct {
|
|
|
|
Name string `yaml:"catalog"`
|
|
|
|
Active *bool `yaml:"active,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
cats := make([]cat, len(catalogs))
|
|
|
|
for i, c := range catalogs {
|
|
|
|
cats[i] = cat{Name: c}
|
|
|
|
if c == currentCatalog {
|
|
|
|
cats[i].Active = lo.ToPtr(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return writeYAML(w.out, w.yp, cats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schemata implements output.MetadataWriter.
|
|
|
|
func (w *mdWriter) Schemata(currentSchema string, schemas []*metadata.Schema) error {
|
|
|
|
if len(schemas) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We wrap each schema in a struct that has an "active" field,
|
|
|
|
// because we need to show the current schema in the output.
|
|
|
|
type wrapper struct {
|
|
|
|
metadata.Schema `yaml:",omitempty,inline"`
|
|
|
|
Active *bool `yaml:"active,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
a := make([]*wrapper, len(schemas))
|
|
|
|
for i, s := range schemas {
|
|
|
|
a[i] = &wrapper{Schema: *s}
|
|
|
|
if s.Name == currentSchema {
|
|
|
|
a[i].Active = lo.ToPtr(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeYAML(w.out, w.yp, a)
|
|
|
|
}
|