zk/cmd/index.go

35 lines
796 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 {
notes := sqlite.NewNoteDAO(tx, container.Logger)
2021-01-16 18:37:10 +00:00
return note.Index(*dir, container.Parser(), notes, container.Logger)
2021-01-03 16:39:04 +00:00
})
2021-01-03 13:43:27 +00:00
}