zk/cmd/list.go

171 lines
4.2 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"
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-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-12 19:54:08 +00:00
Path []string `arg optional placeholder:"PATH"`
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:"TERMS"`
Limit int `help:"Limit the number of results" short:"l" placeholder:"MAX"`
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"`
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 {
return errors.Wrapf(err, "incorrect arguments")
}
2021-01-03 20:19:21 +00:00
db, err := container.Database(zk.DBPath())
if err != nil {
return err
}
return db.WithTransaction(func(tx sqlite.Transaction) error {
2021-01-09 16:14:54 +00:00
notes := sqlite.NewNoteDAO(tx, container.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-12 19:54:08 +00:00
count, err := note.List(*opts, deps, printNote)
if err == nil {
fmt.Printf("\nFound %d result(s)\n", count)
}
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)
paths := make([]string, 0)
for _, p := range cmd.Path {
path, err := zk.RelPath(p)
if err == nil {
paths = append(paths, path)
}
2021-01-12 19:54:08 +00:00
}
if len(paths) > 0 {
filters = append(filters, note.PathFilter(paths))
}
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-12 19:54:08 +00:00
return &note.ListOpts{
Format: opt.NewNotEmptyString(cmd.Format),
FinderOpts: note.FinderOpts{
Filters: filters,
Limit: cmd.Limit,
},
}, nil
2021-01-03 20:19:21 +00:00
}
2021-01-09 16:14:54 +00:00
func printNote(note string) error {
_, err := fmt.Println(note)
return err
}
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))
}