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
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:09:10 +00:00
|
|
|
db, err := container.Database(zk)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx, err := db.Begin()
|
|
|
|
defer tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-03 16:09:10 +00:00
|
|
|
indexer, err := sqlite.NewNoteIndexer(tx, zk.Path, container.Logger)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-03 16:09:10 +00:00
|
|
|
err = note.Index(*dir, indexer, container.Logger)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|