diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2025-07-28 18:03:38 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2025-07-28 18:24:10 -0400 |
| commit | f829b01a1c92e788f5114cf66c24856be23ec88f (patch) | |
| tree | 3342db120daad20da5d0124c2b02bc5cd47cc086 /pkg/query/outputs.go | |
| parent | 83e2cd972d12979232eb8c1043ad3d649d03880d (diff) | |
Add yaml output format
Diffstat (limited to 'pkg/query/outputs.go')
| -rw-r--r-- | pkg/query/outputs.go | 37 |
1 files changed, 37 insertions, 0 deletions
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 "<path> <title> <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) |
