2021-01-23 20:15:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/mickael-menu/zk/adapter/sqlite"
|
|
|
|
"github.com/mickael-menu/zk/core/note"
|
|
|
|
"github.com/mickael-menu/zk/util/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Edit opens notes matching a set of criteria with the user editor.
|
|
|
|
type Edit struct {
|
|
|
|
Filtering `embed`
|
|
|
|
Sorting `embed`
|
|
|
|
Force bool `help:"Don't confirm before editing many notes at the same time" short:"f"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *Edit) Run(container *Container) error {
|
2021-01-24 11:10:13 +00:00
|
|
|
zk, err := container.OpenZk()
|
2021-01-23 20:15:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "incorrect criteria")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := container.Database(zk.DBPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var notes []note.Match
|
|
|
|
err = db.WithTransaction(func(tx sqlite.Transaction) error {
|
|
|
|
notes, err = container.NoteFinder(tx).Find(*opts)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count := len(notes)
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
if !cmd.Force && count > 2 {
|
2021-01-23 20:29:22 +00:00
|
|
|
if !container.Terminal.Confirm(
|
2021-01-23 20:15:36 +00:00
|
|
|
fmt.Sprintf("Are you sure you want to open %v notes in the editor?", count),
|
|
|
|
"Open all the notes",
|
|
|
|
"Don't open any note",
|
|
|
|
) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paths := make([]string, 0)
|
|
|
|
for _, note := range notes {
|
|
|
|
absPath := filepath.Join(zk.Path, note.Path)
|
|
|
|
paths = append(paths, absPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
note.Edit(zk, paths...)
|
2021-01-24 11:10:13 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("Found 0 note")
|
2021-01-23 20:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|