From f829b01a1c92e788f5114cf66c24856be23ec88f Mon Sep 17 00:00:00 2001 From: JP Appel Date: Mon, 28 Jul 2025 18:03:38 -0400 Subject: Add yaml output format --- pkg/query/outputs.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'pkg/query/outputs.go') diff --git a/pkg/query/outputs.go b/pkg/query/outputs.go index 1fb7214..d1e70e8 100644 --- a/pkg/query/outputs.go +++ b/pkg/query/outputs.go @@ -7,6 +7,7 @@ import ( "io" "strings" + "github.com/goccy/go-yaml" "github.com/jpappel/atlas/pkg/index" ) @@ -37,6 +38,7 @@ type Outputer interface { type DefaultOutput struct{} type JsonOutput struct{} +type YamlOutput struct{} type CustomOutput struct { stringTokens []string tokens []OutputToken @@ -49,6 +51,7 @@ type CustomOutput struct { var _ Outputer = &DefaultOutput{} var _ Outputer = &JsonOutput{} var _ Outputer = &CustomOutput{} +var _ Outputer = &YamlOutput{} // Returns " <date> authors:<authors...> tags:<tags>" // and a nil error @@ -150,6 +153,40 @@ func (o JsonOutput) OutputTo(w io.Writer, docs []*index.Document) (int, error) { return w.Write(b) } +func (o YamlOutput) OutputOne(doc *index.Document) (string, error) { + b, err := doc.MarshalYAML() + if err != nil { + return "", err + } + + return string(b), nil +} + +func (o YamlOutput) OutputOneTo(w io.Writer, doc *index.Document) (int, error) { + b, err := doc.MarshalYAML() + if err != nil { + return 0, err + } + return w.Write(b) +} + +func (o YamlOutput) Output(docs []*index.Document) (string, error) { + b, err := yaml.Marshal(docs) + if err != nil { + return "", err + } + return string(b), nil +} + +func (o YamlOutput) OutputTo(w io.Writer, docs []*index.Document) (int, error) { + b, err := yaml.Marshal(docs) + if err != nil { + return 0, err + } + + return w.Write(b) +} + func ParseOutputFormat(formatStr string) ([]OutputToken, []string, error) { toks := make([]OutputToken, 0, 16) curTok := make([]rune, 0, 16) -- cgit v1.2.3