2021-01-03 20:19:21 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-01-09 16:14:54 +00:00
|
|
|
"fmt"
|
2021-01-16 13:13:57 +00:00
|
|
|
"io"
|
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"
|
2021-01-15 21:21:33 +00:00
|
|
|
"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-15 21:21:33 +00:00
|
|
|
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-12 19:54:08 +00:00
|
|
|
opts, err := cmd.ListOpts(zk)
|
|
|
|
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-15 21:21:33 +00:00
|
|
|
logger := container.Logger
|
|
|
|
|
2021-01-03 20:19:21 +00:00
|
|
|
return db.WithTransaction(func(tx sqlite.Transaction) error {
|
2021-01-15 21:21:33 +00:00
|
|
|
notes := sqlite.NewNoteDAO(tx, logger)
|
2021-01-03 20:19:21 +00:00
|
|
|
|
2021-01-12 19:54:08 +00:00
|
|
|
deps := note.ListDeps{
|
|
|
|
BasePath: zk.Path,
|
|
|
|
Finder: notes,
|
|
|
|
Templates: container.TemplateLoader(zk.Config.Lang),
|
2021-01-19 20:05:16 +00:00
|
|
|
Styler: container.Styler(),
|
2021-01-12 19:54:08 +00:00
|
|
|
}
|
2021-01-11 20:51:50 +00:00
|
|
|
|
2021-01-16 13:13:57 +00:00
|
|
|
count := 0
|
|
|
|
err = container.Paginate(cmd.NoPager, zk.Config, func(out io.Writer) error {
|
|
|
|
count, err = note.List(*opts, deps, out)
|
|
|
|
return err
|
|
|
|
})
|
2021-01-15 21:21:33 +00:00
|
|
|
|
2021-01-12 19:54:08 +00:00
|
|
|
if err == nil {
|
2021-01-15 21:21:33 +00:00
|
|
|
fmt.Printf("\nFound %d %s\n", count, strings.Pluralize("result", count))
|
2021-01-11 20:51:50 +00:00
|
|
|
}
|
2021-01-12 19:54:08 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *List) ListOpts(zk *zk.Zk) (*note.ListOpts, error) {
|
|
|
|
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-11 20:51:50 +00:00
|
|
|
|
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-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-12 19:54:08 +00:00
|
|
|
return ¬e.ListOpts{
|
|
|
|
Format: opt.NewNotEmptyString(cmd.Format),
|
|
|
|
FinderOpts: note.FinderOpts{
|
|
|
|
Filters: filters,
|
2021-01-13 21:06:05 +00:00
|
|
|
Sorters: sorters,
|
2021-01-12 19:54:08 +00:00
|
|
|
Limit: cmd.Limit,
|
|
|
|
},
|
|
|
|
}, 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))
|
|
|
|
}
|