zk/cmd/index.go

39 lines
831 B
Go
Raw Normal View History

2021-01-03 13:43:27 +00:00
package cmd
import (
"github.com/mickael-menu/zk/adapter/sqlite"
2021-01-03 16:09:10 +00:00
"github.com/mickael-menu/zk/core/note"
2021-01-03 13:43:27 +00:00
"github.com/mickael-menu/zk/core/zk"
)
// Index indexes the content of all the notes in the slip box.
type Index struct {
Directory string `arg optional type:"path" default:"." help:"Directory containing the notes to index"`
}
func (cmd *Index) Run(container *Container) error {
2021-01-03 16:09:10 +00:00
zk, err := zk.Open(".")
2021-01-03 13:43:27 +00:00
if err != nil {
return err
}
2021-01-03 16:09:10 +00:00
dir, err := zk.RequireDirAt(cmd.Directory)
2021-01-03 13:43:27 +00:00
if err != nil {
return err
}
db, err := container.Database(zk.DBPath())
2021-01-03 13:43:27 +00:00
if err != nil {
return err
}
2021-01-03 16:39:04 +00:00
return db.WithTransaction(func(tx sqlite.Transaction) error {
2021-01-03 20:19:21 +00:00
indexer, err := sqlite.NewNoteDAO(tx, zk.Path, container.Logger)
2021-01-03 16:39:04 +00:00
if err != nil {
return err
}
return note.Index(*dir, indexer, container.Logger)
})
2021-01-03 13:43:27 +00:00
}