Extract filtering and sorting flags

pull/6/head
Mickaël Menu 3 years ago
parent 6c95ff43c1
commit 5cffc71da6
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -0,0 +1,151 @@
package cmd
import (
"time"
"github.com/mickael-menu/zk/core/note"
"github.com/mickael-menu/zk/core/zk"
"github.com/tj/go-naturaldate"
)
// Filtering holds filtering options to select notes.
type Filtering struct {
Path []string `arg optional placeholder:"<glob>"`
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:"Only the notes created on the given date" placeholder:"<date>"`
CreatedBefore string `help:"Only the notes created before the given date" placeholder:"<date>"`
CreatedAfter string `help:"Only the notes created after the given date" placeholder:"<date>"`
Modified string `help:"Only the notes modified on the given date" placeholder:"<date>"`
ModifiedBefore string `help:"Only the notes modified before the given date" placeholder:"<date>"`
ModifiedAfter string `help:"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>"`
Interactive bool `help:"Further filter the list of notes interactively" short:"i"`
}
// Sorting holds sorting options to order notes.
type Sorting struct {
Sort []string `help:"Sort the notes by the given criterion" short:"s" placeholder:"<term>"`
}
// NewFinderOpts creates an instance of note.FinderOpts from a set of user flags.
func NewFinderOpts(zk *zk.Zk, filtering Filtering, sorting Sorting) (*note.FinderOpts, error) {
filters := make([]note.Filter, 0)
paths, ok := relPaths(zk, filtering.Path)
if ok {
filters = append(filters, note.PathFilter(paths))
}
excludePaths, ok := relPaths(zk, filtering.Exclude)
if ok {
filters = append(filters, note.ExcludePathFilter(excludePaths))
}
if filtering.Match != "" {
filters = append(filters, note.MatchFilter(filtering.Match))
}
if filtering.Created != "" {
date, err := parseDate(filtering.Created)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateOn,
})
}
if filtering.CreatedBefore != "" {
date, err := parseDate(filtering.CreatedBefore)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateBefore,
})
}
if filtering.CreatedAfter != "" {
date, err := parseDate(filtering.CreatedAfter)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateAfter,
})
}
if filtering.Modified != "" {
date, err := parseDate(filtering.Modified)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateOn,
})
}
if filtering.ModifiedBefore != "" {
date, err := parseDate(filtering.ModifiedBefore)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateBefore,
})
}
if filtering.ModifiedAfter != "" {
date, err := parseDate(filtering.ModifiedAfter)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateAfter,
})
}
if filtering.Interactive {
filters = append(filters, note.InteractiveFilter(true))
}
sorters, err := note.SortersFromStrings(sorting.Sort)
if err != nil {
return nil, err
}
return &note.FinderOpts{
Filters: filters,
Sorters: sorters,
Limit: filtering.Limit,
}, nil
}
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
}
func parseDate(date string) (time.Time, error) {
// FIXME: support years
return naturaldate.Parse(date, time.Now().UTC(), naturaldate.WithDirection(naturaldate.Past))
}

@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"time"
"github.com/mickael-menu/zk/adapter/sqlite"
"github.com/mickael-menu/zk/core/note"
@ -12,25 +11,14 @@ import (
"github.com/mickael-menu/zk/util/errors"
"github.com/mickael-menu/zk/util/opt"
"github.com/mickael-menu/zk/util/strings"
"github.com/tj/go-naturaldate"
)
// List displays notes matching a set of criteria.
type List struct {
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>"`
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"`
Format string `help:"Pretty prints the list using the given format" short:"f" placeholder:"<template>"`
NoPager bool `help:"Do not pipe zk output into a pager" short:"P"`
Filtering `embed`
Sorting `embed`
}
func (cmd *List) Run(container *Container) error {
@ -39,7 +27,7 @@ func (cmd *List) Run(container *Container) error {
return err
}
opts, err := cmd.FinderOpts(zk)
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
if err != nil {
return errors.Wrapf(err, "incorrect criteria")
}
@ -93,124 +81,3 @@ func (cmd *List) Run(container *Container) error {
return err
}
func (cmd *List) FinderOpts(zk *zk.Zk) (*note.FinderOpts, error) {
filters := make([]note.Filter, 0)
paths, ok := relPaths(zk, cmd.Path)
if ok {
filters = append(filters, note.PathFilter(paths))
}
excludePaths, ok := relPaths(zk, cmd.Exclude)
if ok {
filters = append(filters, note.ExcludePathFilter(excludePaths))
}
if cmd.Match != "" {
filters = append(filters, note.MatchFilter(cmd.Match))
}
if cmd.Created != "" {
date, err := parseDate(cmd.Created)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateOn,
})
}
if cmd.CreatedBefore != "" {
date, err := parseDate(cmd.CreatedBefore)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateBefore,
})
}
if cmd.CreatedAfter != "" {
date, err := parseDate(cmd.CreatedAfter)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateCreated,
Direction: note.DateAfter,
})
}
if cmd.Modified != "" {
date, err := parseDate(cmd.Modified)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateOn,
})
}
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,
})
}
if cmd.ModifiedAfter != "" {
date, err := parseDate(cmd.ModifiedAfter)
if err != nil {
return nil, err
}
filters = append(filters, note.DateFilter{
Date: date,
Field: note.DateModified,
Direction: note.DateAfter,
})
}
if cmd.Interactive {
filters = append(filters, note.InteractiveFilter(true))
}
sorters, err := note.SortersFromStrings(cmd.Sort)
if err != nil {
return nil, err
}
return &note.FinderOpts{
Filters: filters,
Sorters: sorters,
Limit: cmd.Limit,
}, nil
}
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
}
func parseDate(date string) (time.Time, error) {
// FIXME: support years
return naturaldate.Parse(date, time.Now().UTC(), naturaldate.WithDirection(naturaldate.Past))
}

Loading…
Cancel
Save