diff options
| -rw-r--r-- | cmd/query.go | 8 | ||||
| -rw-r--r-- | pkg/index/index.go | 14 | ||||
| -rw-r--r-- | pkg/query/outputs.go | 37 | ||||
| -rw-r--r-- | pkg/shell/interpreter.go | 2 |
4 files changed, 58 insertions, 3 deletions
diff --git a/cmd/query.go b/cmd/query.go index 4b28e09..190289a 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -23,7 +23,7 @@ type QueryFlags struct { func SetupQueryFlags(args []string, fs *flag.FlagSet, flags *QueryFlags, dateFormat string) { // NOTE: providing `-outFormat` before `-outCustomFormat` might ignore user specified format - fs.Func("outFormat", "output `format` for queries (default, json, pathonly, custom)", + fs.Func("outFormat", "output `format` for queries (default, json, yaml, pathonly, custom)", func(arg string) error { switch arg { case "default": @@ -32,6 +32,9 @@ func SetupQueryFlags(args []string, fs *flag.FlagSet, flags *QueryFlags, dateFor case "json": flags.Outputer = query.JsonOutput{} return nil + case "yaml": + flags.Outputer = query.YamlOutput{} + return nil case "pathonly": flags.Outputer, _ = query.NewCustomOutput("%p", dateFormat, "\n", "") return nil @@ -39,8 +42,9 @@ func SetupQueryFlags(args []string, fs *flag.FlagSet, flags *QueryFlags, dateFor var err error flags.Outputer, err = query.NewCustomOutput(flags.CustomFormat, dateFormat, flags.DocumentSeparator, flags.ListSeparator) return err + default: + return fmt.Errorf("Unrecognized output format: %s", arg) } - return fmt.Errorf("Unrecognized output format: %s", arg) }) fs.StringVar(&flags.SortBy, "sortBy", "", "category to sort by (path,title,date,filetime,meta)") 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) } |
