2021-01-23 20:15:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-10 20:30:03 +00:00
|
|
|
"os"
|
2021-01-23 20:15:36 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2021-02-07 16:48:23 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/fzf"
|
2021-01-23 20:15:36 +00:00
|
|
|
"github.com/mickael-menu/zk/adapter/sqlite"
|
|
|
|
"github.com/mickael-menu/zk/core/note"
|
2021-02-07 16:48:23 +00:00
|
|
|
"github.com/mickael-menu/zk/core/zk"
|
2021-01-23 20:15:36 +00:00
|
|
|
"github.com/mickael-menu/zk/util/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Edit opens notes matching a set of criteria with the user editor.
|
|
|
|
type Edit struct {
|
2021-02-10 19:53:25 +00:00
|
|
|
Force bool `short:f help:"Do not confirm before editing many notes at the same time."`
|
|
|
|
|
2021-02-07 16:48:23 +00:00
|
|
|
Filtering
|
|
|
|
Sorting
|
2021-01-23 20:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-10 20:30:03 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:15:36 +00:00
|
|
|
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "incorrect criteria")
|
|
|
|
}
|
|
|
|
|
2021-03-08 20:38:32 +00:00
|
|
|
db, _, err := container.Database(zk, false)
|
2021-01-23 20:15:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var notes []note.Match
|
|
|
|
err = db.WithTransaction(func(tx sqlite.Transaction) error {
|
2021-02-07 16:48:23 +00:00
|
|
|
finder := container.NoteFinder(tx, fzf.NoteFinderOpts{
|
|
|
|
AlwaysFilter: true,
|
2021-02-13 21:16:11 +00:00
|
|
|
PreviewCmd: zk.Config.Tool.FzfPreview,
|
2021-02-07 16:48:23 +00:00
|
|
|
NewNoteDir: cmd.newNoteDir(zk),
|
2021-02-10 20:30:03 +00:00
|
|
|
BasePath: zk.Path,
|
|
|
|
CurrentPath: wd,
|
2021-02-07 16:48:23 +00:00
|
|
|
})
|
|
|
|
notes, err = finder.Find(*opts)
|
2021-01-23 20:15:36 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-02-07 16:48:23 +00:00
|
|
|
if err == note.ErrCanceled {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-23 20:15:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count := len(notes)
|
|
|
|
|
|
|
|
if count > 0 {
|
2021-01-30 17:39:04 +00:00
|
|
|
if !cmd.Force && count > 5 {
|
2021-02-10 19:53:25 +00:00
|
|
|
confirmed, skipped := container.Terminal.Confirm(fmt.Sprintf("Are you sure you want to open %v notes in the editor?", count), false)
|
|
|
|
if skipped {
|
|
|
|
return fmt.Errorf("too many notes to be opened in the editor, aborting…")
|
|
|
|
} else if !confirmed {
|
2021-01-23 20:15:36 +00:00
|
|
|
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-02-07 16:48:23 +00:00
|
|
|
|
2021-01-24 11:10:13 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("Found 0 note")
|
2021-01-23 20:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-02-07 16:48:23 +00:00
|
|
|
|
|
|
|
// newNoteDir returns the directory in which to create a new note when the fzf
|
|
|
|
// binding is triggered.
|
|
|
|
func (cmd *Edit) newNoteDir(zk *zk.Zk) *zk.Dir {
|
|
|
|
switch len(cmd.Path) {
|
|
|
|
case 0:
|
|
|
|
dir := zk.RootDir()
|
|
|
|
return &dir
|
|
|
|
case 1:
|
|
|
|
dir, err := zk.DirAt(cmd.Path[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
default:
|
|
|
|
// More than one directory, it's ambiguous for the "new note" fzf binding.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|