aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-07-28 18:03:38 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-07-28 18:24:10 -0400
commitf829b01a1c92e788f5114cf66c24856be23ec88f (patch)
tree3342db120daad20da5d0124c2b02bc5cd47cc086 /pkg
parent83e2cd972d12979232eb8c1043ad3d649d03880d (diff)
Add yaml output format
Diffstat (limited to 'pkg')
-rw-r--r--pkg/index/index.go14
-rw-r--r--pkg/query/outputs.go37
-rw-r--r--pkg/shell/interpreter.go2
3 files changed, 52 insertions, 1 deletions
diff --git a/pkg/index/index.go b/pkg/index/index.go
index 8c21e72..f4e4593 100644
--- a/pkg/index/index.go
+++ b/pkg/index/index.go
@@ -70,6 +70,20 @@ func (idx Index) String() string {
}
var _ yaml.NodeUnmarshaler = (*Document)(nil)
+var _ yaml.BytesMarshaler = (*Document)(nil)
+
+func (doc *Document) MarshalYAML() ([]byte, error) {
+ return yaml.Marshal(yaml.MapSlice{
+ {Key: "path", Value: doc.Path},
+ {Key: "title", Value: doc.Title},
+ {Key: "date", Value: doc.Date},
+ {Key: "filetime", Value: doc.FileTime},
+ {Key: "authors", Value: doc.Authors},
+ {Key: "tags", Value: doc.Tags},
+ {Key: "links", Value: doc.Links},
+ {Key: "meta", Value: doc.OtherMeta},
+ })
+}
func (doc *Document) UnmarshalYAML(node ast.Node) error {
// parse top level fields
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)
diff --git a/pkg/shell/interpreter.go b/pkg/shell/interpreter.go
index c244c9e..02d5aaf 100644
--- a/pkg/shell/interpreter.go
+++ b/pkg/shell/interpreter.go
@@ -519,7 +519,7 @@ out:
return false, fmt.Errorf("Error occured while excuting query: %s", err)
}
- _, err = query.DefaultOutput{}.OutputTo(w, slices.Collect(maps.Values(results)))
+ _, err = query.YamlOutput{}.OutputTo(w, slices.Collect(maps.Values(results)))
if err != nil {
return false, fmt.Errorf("Can't output results: %s", err)
}