zk/cmd/list.go

57 lines
1.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-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-09 16:14:54 +00:00
"github.com/mickael-menu/zk/util/opt"
2021-01-03 20:19:21 +00:00
)
// List displays notes matching a set of criteria.
type List struct {
2021-01-10 21:41:36 +00:00
Path []string `arg optional placeholder:"PATHS"`
Match string `help:"Terms to search for in the notes" placeholder:"TERMS"`
Format string `help:"Pretty prints the list using the given format" placeholder:"TEMPLATE"`
2021-01-03 20:19:21 +00:00
}
func (cmd *List) Run(container *Container) error {
zk, err := zk.Open(".")
if err != nil {
return err
}
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
filters := make([]note.Filter, 0)
2021-01-09 16:14:54 +00:00
if cmd.Match != "" {
filters = append(filters, note.MatchFilter(cmd.Match))
2021-01-03 20:19:21 +00:00
}
2021-01-09 16:14:54 +00:00
return note.List(
note.ListOpts{
Format: opt.NewNotEmptyString(cmd.Format),
Filters: filters,
},
note.ListDeps{
BasePath: zk.Path,
Finder: notes,
Templates: container.TemplateLoader(zk.Config.Lang),
},
printNote,
)
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
}