zk/cmd/list.go

220 lines
5.5 KiB
Go
Raw Normal View History

2021-01-03 20:19:21 +00:00
package cmd
import (
2021-01-09 16:14:54 +00:00
"fmt"
"io"
2021-01-23 12:29:14 +00:00
"os"
2021-01-12 18:58:14 +00:00
"time"
2021-01-09 16:14:54 +00:00
2021-01-03 20:19:21 +00:00
"github.com/mickael-menu/zk/adapter/sqlite"
"github.com/mickael-menu/zk/core/note"
"github.com/mickael-menu/zk/core/zk"
2021-01-12 19:54:08 +00:00
"github.com/mickael-menu/zk/util/errors"
2021-01-09 16:14:54 +00:00
"github.com/mickael-menu/zk/util/opt"
"github.com/mickael-menu/zk/util/strings"
2021-01-12 18:58:14 +00:00
"github.com/tj/go-naturaldate"
2021-01-03 20:19:21 +00:00
)
// List displays notes matching a set of criteria.
type List struct {
2021-01-14 21:45:05 +00:00
Path []string `arg optional placeholder:"<glob>"`
Format string `help:"Pretty prints the list using the given format" short:"f" placeholder:"<template>"`
Match string `help:"Terms to search for in the notes" short:"m" placeholder:"<query>"`
Limit int `help:"Limit the number of results" short:"n" placeholder:"<count>"`
Created string `help:"Show only the notes created on the given date" placeholder:"<date>"`
CreatedBefore string `help:"Show only the notes created before the given date" placeholder:"<date>"`
CreatedAfter string `help:"Show only the notes created after the given date" placeholder:"<date>"`
Modified string `help:"Show only the notes modified on the given date" placeholder:"<date>"`
ModifiedBefore string `help:"Show only the notes modified before the given date" placeholder:"<date>"`
ModifiedAfter string `help:"Show only the notes modified after the given date" placeholder:"<date>"`
Exclude []string `help:"Excludes notes matching the given file path pattern from the list" short:"x" placeholder:"<glob>"`
Sort []string `help:"Sort the notes by the given criterion" short:"s" placeholder:"<term>"`
2021-01-23 12:29:14 +00:00
Interactive bool `help:"Further filter the list of notes interactively" short:"i"`
NoPager bool `help:"Do not pipe zk output into a pager" short:"P"`
2021-01-03 20:19:21 +00:00
}
func (cmd *List) Run(container *Container) error {
zk, err := zk.Open(".")
if err != nil {
return err
}
2021-01-23 12:29:14 +00:00
opts, err := cmd.FinderOpts(zk)
2021-01-12 19:54:08 +00:00
if err != nil {
2021-01-14 21:10:35 +00:00
return errors.Wrapf(err, "incorrect criteria")
2021-01-12 19:54:08 +00:00
}
2021-01-03 20:19:21 +00:00
db, err := container.Database(zk.DBPath())
if err != nil {
return err
}
2021-01-23 12:29:14 +00:00
wd, err := os.Getwd()
if err != nil {
return err
}
2021-01-23 12:29:14 +00:00
templates := container.TemplateLoader(zk.Config.Lang)
styler := container.Styler()
format := opt.NewNotEmptyString(cmd.Format)
formatter, err := note.NewFormatter(zk.Path, wd, format, templates, styler)
if err != nil {
return err
}
2021-01-03 20:19:21 +00:00
2021-01-23 12:29:14 +00:00
var notes []note.Match
err = db.WithTransaction(func(tx sqlite.Transaction) error {
notes, err = container.NoteFinder(tx).Find(*opts)
return err
})
if err != nil {
return err
}
2021-01-23 12:29:14 +00:00
count := len(notes)
if count > 0 {
err = container.Paginate(cmd.NoPager, zk.Config, func(out io.Writer) error {
2021-01-23 12:29:14 +00:00
for _, note := range notes {
ft, err := formatter.Format(note)
if err != nil {
return err
}
_, err = fmt.Fprintf(out, "%v\n", ft)
if err != nil {
return err
}
}
return nil
})
2021-01-23 12:29:14 +00:00
}
2021-01-23 12:29:14 +00:00
if err == nil {
fmt.Printf("\nFound %d %s\n", count, strings.Pluralize("result", count))
}
2021-01-12 19:54:08 +00:00
2021-01-23 12:29:14 +00:00
return err
2021-01-12 19:54:08 +00:00
}
2021-01-23 12:29:14 +00:00
func (cmd *List) FinderOpts(zk *zk.Zk) (*note.FinderOpts, error) {
2021-01-12 19:54:08 +00:00
filters := make([]note.Filter, 0)
2021-01-13 18:53:15 +00:00
paths, ok := relPaths(zk, cmd.Path)
if ok {
2021-01-12 19:54:08 +00:00
filters = append(filters, note.PathFilter(paths))
}
2021-01-13 18:53:15 +00:00
excludePaths, ok := relPaths(zk, cmd.Exclude)
if ok {
filters = append(filters, note.ExcludePathFilter(excludePaths))
}
2021-01-12 19:54:08 +00:00
if cmd.Match != "" {
filters = append(filters, note.MatchFilter(cmd.Match))
}
2021-01-12 19:54:08 +00:00
if cmd.Created != "" {
date, err := parseDate(cmd.Created)
if err != nil {
return nil, err
2021-01-03 20:19:21 +00:00
}
2021-01-12 19:54:08 +00:00
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateOn,
})
}
2021-01-03 20:19:21 +00:00
2021-01-12 19:54:08 +00:00
if cmd.CreatedBefore != "" {
date, err := parseDate(cmd.CreatedBefore)
if err != nil {
return nil, err
2021-01-12 18:58:14 +00:00
}
2021-01-12 19:54:08 +00:00
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateBefore,
})
}
2021-01-12 18:58:14 +00:00
2021-01-12 19:54:08 +00:00
if cmd.CreatedAfter != "" {
date, err := parseDate(cmd.CreatedAfter)
if err != nil {
return nil, err
2021-01-12 18:58:14 +00:00
}
2021-01-12 19:54:08 +00:00
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateAfter,
})
}
2021-01-12 18:58:14 +00:00
2021-01-12 19:54:08 +00:00
if cmd.Modified != "" {
date, err := parseDate(cmd.Modified)
if err != nil {
return nil, err
2021-01-12 18:58:14 +00:00
}
2021-01-12 19:54:08 +00:00
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateOn,
})
}
2021-01-12 18:58:14 +00:00
2021-01-12 19:54:08 +00:00
if cmd.ModifiedBefore != "" {
date, err := parseDate(cmd.ModifiedBefore)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateBefore,
})
}
2021-01-11 21:02:06 +00:00
2021-01-12 19:54:08 +00:00
if cmd.ModifiedAfter != "" {
date, err := parseDate(cmd.ModifiedAfter)
if err != nil {
return nil, err
2021-01-11 21:02:06 +00:00
}
2021-01-12 19:54:08 +00:00
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateAfter,
})
}
2021-01-11 21:02:06 +00:00
2021-01-23 12:29:14 +00:00
if cmd.Interactive {
filters = append(filters, note.InteractiveFilter(true))
}
2021-01-14 21:45:05 +00:00
sorters, err := note.SortersFromStrings(cmd.Sort)
2021-01-13 21:06:05 +00:00
if err != nil {
return nil, err
}
2021-01-23 12:29:14 +00:00
return &note.FinderOpts{
Filters: filters,
Sorters: sorters,
Limit: cmd.Limit,
2021-01-12 19:54:08 +00:00
}, nil
2021-01-03 20:19:21 +00:00
}
2021-01-09 16:14:54 +00:00
2021-01-13 18:53:15 +00:00
func relPaths(zk *zk.Zk, paths []string) ([]string, bool) {
relPaths := make([]string, 0)
for _, p := range paths {
path, err := zk.RelPath(p)
if err == nil {
relPaths = append(relPaths, path)
}
}
return relPaths, len(relPaths) > 0
}
2021-01-12 18:58:14 +00:00
func parseDate(date string) (time.Time, error) {
// FIXME: support years
return naturaldate.Parse(date, time.Now().UTC(), naturaldate.WithDirection(naturaldate.Past))
}