Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bdee5d9a0 | ||
|
|
7c84d7650c | ||
|
|
29646808f9 | ||
|
|
8276308d52 | ||
|
|
8c050d1d89 | ||
|
|
e8a83176aa | ||
|
|
7d98e6f0fc | ||
|
|
82a615919c | ||
|
|
50913b8913 | ||
|
|
42d532ee61 | ||
|
|
24316fe690 | ||
|
|
8058304219 | ||
|
|
7f24b3f337 | ||
|
|
fc12e0550d | ||
|
|
05f5244cd1 | ||
|
|
c559c8eba6 | ||
|
|
dd7ac8d786 |
@@ -11,6 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@@ -24,6 +25,16 @@ const (
|
|||||||
DUMP_DONE = "done"
|
DUMP_DONE = "done"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
targets []string
|
||||||
|
mergeme string
|
||||||
|
root string
|
||||||
|
tags []string
|
||||||
|
search string
|
||||||
|
edit bool
|
||||||
|
dry bool
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := _main(); err != nil {
|
if err := _main(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -31,33 +42,45 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func _main() error {
|
func _main() error {
|
||||||
|
config := getConfig()
|
||||||
|
if config.mergeme != "" {
|
||||||
|
if err := merge(config.dry, config.targets[0], config.mergeme); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config.edit {
|
||||||
|
if err := edit(config.dry, config.targets); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dump(config.dry, os.Stdout, config.targets, config.tags, config.search, config.root)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfig() config {
|
||||||
defaultFilepath, ok := os.LookupEnv("PTTODO_FILE")
|
defaultFilepath, ok := os.LookupEnv("PTTODO_FILE")
|
||||||
if !ok {
|
if !ok {
|
||||||
defaultFilepath = "-"
|
defaultFilepath = "./todo.yaml"
|
||||||
}
|
}
|
||||||
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file")
|
|
||||||
filepathToMergeIn := flag.String("g", "", "path to yaml file to merge into -f")
|
var config config
|
||||||
root := flag.String("root", DUMP_TODO, "path to pretty print ("+fmt.Sprint([]string{DUMP_ALL, DUMP_TODO, DUMP_SCHEDULED, DUMP_DONE})+")")
|
var target string
|
||||||
tags := flag.String("tags", "", "csv of all tags to find, -tag to invert")
|
flag.StringVar(&target, "f", defaultFilepath, "($PTTODO_FILE) path to yaml file or dir (starting with root then alphabetical for dir)")
|
||||||
search := flag.String("search", "", "fts case insensitive")
|
flag.StringVar(&config.mergeme, "g", "", "path to yaml file to merge into -f (modifies f)")
|
||||||
e := flag.Bool("e", false, "edit file")
|
flag.StringVar(&config.root, "root", DUMP_TODO, "path to pretty print ("+fmt.Sprint([]string{DUMP_ALL, DUMP_TODO, DUMP_SCHEDULED, DUMP_DONE})+")")
|
||||||
dry := flag.Bool("dry", false, "dry run")
|
var tagss string
|
||||||
|
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -tag to invert")
|
||||||
|
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
||||||
|
flag.BoolVar(&config.edit, "e", false, "edit file")
|
||||||
|
flag.BoolVar(&config.dry, "dry", false, "dry run")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *filepathToMergeIn != "" {
|
|
||||||
if err := merge(*dry, *filepath, *filepathToMergeIn); err != nil {
|
config.tags = strings.Split(tagss, ",")
|
||||||
return err
|
config.targets = []string{target}
|
||||||
}
|
if stat, err := os.Stat(target); err == nil && stat.IsDir() {
|
||||||
|
config.targets, _ = listDir(target)
|
||||||
}
|
}
|
||||||
if *e {
|
|
||||||
if err := edit(*dry, *filepath); err != nil {
|
return config
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var tagslist []string
|
|
||||||
if *tags != "" {
|
|
||||||
tagslist = strings.Split(*tags, ",")
|
|
||||||
}
|
|
||||||
return dump(*dry, os.Stdout, *filepath, tagslist, *search, *root)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyRoot(root pttodo.Root) error {
|
func verifyRoot(root pttodo.Root) error {
|
||||||
@@ -79,16 +102,102 @@ func verifyRoot(root pttodo.Root) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyFile(path string) error {
|
func verifyFile(path string) error {
|
||||||
if err := dump(true, io.Discard, path, nil, "", DUMP_ALL); err != nil {
|
if err := dump(true, io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
|
||||||
return fmt.Errorf("failed verifying file %s: %w", path, err)
|
return fmt.Errorf("failed verifying file %s: %w", path, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func edit(dry bool, filepath string) error {
|
func edit(dry bool, filepaths []string) error {
|
||||||
var tempFile string
|
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
||||||
cp := func() error {
|
if err != nil {
|
||||||
f, err := ioutil.TempFile(os.TempDir(), path.Base(filepath))
|
return err
|
||||||
|
}
|
||||||
|
originals := map[string]pttodo.Root{}
|
||||||
|
for _, target := range filepaths {
|
||||||
|
var original pttodo.Root
|
||||||
|
if b, err := ioutil.ReadFile(target); err != nil && !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(b, &original); err != nil {
|
||||||
|
return err
|
||||||
|
} else if c, err := yaml.Marshal(original.Todo); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := ioutil.WriteFile(path.Join(tempDir, path.Base(target)), c, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
originals[target] = original
|
||||||
|
}
|
||||||
|
if err := vimd(tempDir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, target := range filepaths {
|
||||||
|
for {
|
||||||
|
err := func() error {
|
||||||
|
var todos []pttodo.Todo
|
||||||
|
if b, err := ioutil.ReadFile(path.Join(tempDir, path.Base(target))); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(b, &todos); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}()
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Printf("%s, press <Enter> to resume editing", err)
|
||||||
|
b := make([]byte, 1)
|
||||||
|
if _, err := os.Stdin.Read(b); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := vimd(tempDir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if dry {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, target := range filepaths {
|
||||||
|
b, err := ioutil.ReadFile(path.Join(tempDir, path.Base(target)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var newTodos []pttodo.Todo
|
||||||
|
if err := yaml.Unmarshal(b, &newTodos); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
original := originals[target]
|
||||||
|
|
||||||
|
newTodoIDs := map[string]struct{}{}
|
||||||
|
for i := range newTodos {
|
||||||
|
newTodoIDs[newTodos[i].ID()] = struct{}{}
|
||||||
|
}
|
||||||
|
for i := range original.Todo {
|
||||||
|
if _, ok := newTodoIDs[original.Todo[i].ID()]; !ok {
|
||||||
|
original.Done = append(original.Done, original.Todo[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
original.Todo = newTodos
|
||||||
|
|
||||||
|
original.AutoMove()
|
||||||
|
c, err := yaml.Marshal(original)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func _edit(dry bool, filepaths []string) error {
|
||||||
|
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cpOne := func(filepath string) error {
|
||||||
|
f, err := os.Create(path.Join(tempDir, path.Base(filepath)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -103,49 +212,20 @@ func edit(dry bool, filepath string) error {
|
|||||||
g.Close()
|
g.Close()
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
tempFile = f.Name() + ".yaml"
|
return nil
|
||||||
return os.Rename(f.Name(), tempFile)
|
|
||||||
}
|
}
|
||||||
vi := func() error {
|
cp := func() error {
|
||||||
bin := "vim"
|
for _, filepath := range filepaths {
|
||||||
if editor := os.Getenv("EDITOR"); editor != "" {
|
if err := cpOne(filepath); err != nil {
|
||||||
bin = editor
|
return err
|
||||||
}
|
}
|
||||||
editorbin, err := exec.LookPath(bin)
|
|
||||||
if err != nil {
|
|
||||||
editorbin, err = exec.LookPath("vi")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
args := []string{editorbin, tempFile}
|
|
||||||
cpid, err := syscall.ForkExec(
|
|
||||||
editorbin,
|
|
||||||
args,
|
|
||||||
&syscall.ProcAttr{
|
|
||||||
Dir: "",
|
|
||||||
Env: os.Environ(),
|
|
||||||
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
|
|
||||||
Sys: nil,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
proc, err := os.FindProcess(cpid)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
state, err := proc.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if exitCode := state.ExitCode(); exitCode != 0 {
|
|
||||||
return fmt.Errorf("bad exit code on vim: %d, state: %+v", exitCode, state)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
verify := func() error {
|
vi := func() error {
|
||||||
|
return vimd(tempDir)
|
||||||
|
}
|
||||||
|
verifyOne := func(tempFile string) error {
|
||||||
for {
|
for {
|
||||||
err := verifyFile(tempFile)
|
err := verifyFile(tempFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -162,13 +242,44 @@ func edit(dry bool, filepath string) error {
|
|||||||
}
|
}
|
||||||
return verifyFile(tempFile)
|
return verifyFile(tempFile)
|
||||||
}
|
}
|
||||||
save := func() error {
|
verify := func() error {
|
||||||
|
for _, filepath := range filepaths {
|
||||||
|
tempFile := path.Join(tempDir, path.Base(filepath))
|
||||||
|
if err := verifyOne(tempFile); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
saveOne := func(filepath string) error {
|
||||||
|
tempFile := path.Join(tempDir, path.Base(filepath))
|
||||||
|
var rootTemp, rootOld pttodo.Root
|
||||||
|
if a, err := ioutil.ReadFile(tempFile); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(a, &rootTemp); err != nil {
|
||||||
|
return err
|
||||||
|
} else if b, err := ioutil.ReadFile(filepath); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(b, &rootOld); err != nil {
|
||||||
|
return err
|
||||||
|
} else if rootTemp.Equals(rootOld) {
|
||||||
|
//log.Printf("no changes to %s", filepath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if dry {
|
if dry {
|
||||||
log.Printf("would've saved %s as %s", tempFile, filepath)
|
log.Printf("would've saved %s as %s", tempFile, filepath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return os.Rename(tempFile, filepath)
|
return os.Rename(tempFile, filepath)
|
||||||
}
|
}
|
||||||
|
save := func() error {
|
||||||
|
for _, filepath := range filepaths {
|
||||||
|
if err := saveOne(filepath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, foo := range []func() error{cp, vi, verify, save} {
|
for _, foo := range []func() error{cp, vi, verify, save} {
|
||||||
if err := foo(); err != nil {
|
if err := foo(); err != nil {
|
||||||
@@ -176,12 +287,54 @@ func edit(dry bool, filepath string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !dry {
|
if !dry {
|
||||||
os.Remove(tempFile)
|
os.RemoveAll(tempDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func vimd(d string) error {
|
||||||
|
bin := "vim"
|
||||||
|
editorbin, err := exec.LookPath(bin)
|
||||||
|
if err != nil {
|
||||||
|
editorbin, err = exec.LookPath("vi")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
args := []string{editorbin, "-p"}
|
||||||
|
files, err := listDir(d)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
args = append(args, files...)
|
||||||
|
cpid, err := syscall.ForkExec(
|
||||||
|
editorbin,
|
||||||
|
args,
|
||||||
|
&syscall.ProcAttr{
|
||||||
|
Dir: d,
|
||||||
|
Env: os.Environ(),
|
||||||
|
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
|
||||||
|
Sys: nil,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proc, err := os.FindProcess(cpid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
state, err := proc.Wait()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if exitCode := state.ExitCode(); exitCode != 0 {
|
||||||
|
return fmt.Errorf("bad exit code on vim: %d, state: %+v", exitCode, state)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func merge(dry bool, filepath string, mergeTargetFilePath string) error {
|
func merge(dry bool, filepath string, mergeTargetFilePath string) error {
|
||||||
baseReader, err := filePathReader(filepath)
|
baseReader, err := filePathReader(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -241,21 +394,48 @@ func marshalRootToTempFile(root pttodo.Root) (string, error) {
|
|||||||
return filepath, err
|
return filepath, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func dump(dry bool, writer io.Writer, filepath string, tags []string, search, rootDisplay string) error {
|
func dump(dry bool, writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
||||||
reader, err := filePathReader(filepath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(reader)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var root pttodo.Root
|
var root pttodo.Root
|
||||||
if err := yaml.Unmarshal(b, &root); err != nil {
|
|
||||||
return err
|
for _, filepath := range filepaths {
|
||||||
|
reader, err := filePathReader(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := ioutil.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var root2, root2post pttodo.Root
|
||||||
|
if err := yaml.Unmarshal(b, &root2); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal(b, &root2post); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
root2.MoveScheduledToTodo()
|
||||||
|
|
||||||
|
if !dry {
|
||||||
|
if !root2.Equals(root2post) {
|
||||||
|
log.Printf("refreshing %s", filepath)
|
||||||
|
b3, err := yaml.Marshal(root2)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath, b3, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//log.Printf("not refreshing %s", filepath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
root.MergeIn(root2)
|
||||||
}
|
}
|
||||||
|
|
||||||
root.MoveScheduledToTodo()
|
root.MoveScheduledToTodo()
|
||||||
|
|
||||||
var v interface{} = root
|
var v interface{} = root
|
||||||
@@ -296,21 +476,13 @@ func dump(dry bool, writer io.Writer, filepath string, tags []string, search, ro
|
|||||||
}
|
}
|
||||||
v = todos
|
v = todos
|
||||||
}
|
}
|
||||||
|
|
||||||
b2, err := yaml.Marshal(v)
|
b2, err := yaml.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(writer, "%s\n", b2)
|
fmt.Fprintf(writer, "%s\n", b2)
|
||||||
|
return nil
|
||||||
if dry {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
b3, err := yaml.Marshal(root)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return os.WriteFile(filepath, b3, os.ModePerm)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func filePathReader(path string) (io.Reader, error) {
|
func filePathReader(path string) (io.Reader, error) {
|
||||||
@@ -326,3 +498,24 @@ func filePathReader(path string) (io.Reader, error) {
|
|||||||
}
|
}
|
||||||
return reader, nil
|
return reader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listDir(dname string) ([]string, error) {
|
||||||
|
entries, err := os.ReadDir(dname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paths := make([]string, 0, len(entries))
|
||||||
|
for i := range entries {
|
||||||
|
if entries[i].IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paths = append(paths, path.Join(dname, entries[i].Name()))
|
||||||
|
}
|
||||||
|
sort.Slice(paths, func(i, j int) bool {
|
||||||
|
if path.Base(paths[i]) == "root.yaml" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return paths[i] < paths[j]
|
||||||
|
})
|
||||||
|
return paths, nil
|
||||||
|
}
|
||||||
|
|||||||
29
cmd/pttodo-cli/cli_test.go
Normal file
29
cmd/pttodo-cli/cli_test.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListDir(t *testing.T) {
|
||||||
|
d := t.TempDir()
|
||||||
|
want := []string{}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
p := path.Join(d, strconv.Itoa(i))
|
||||||
|
ioutil.WriteFile(p, []byte{}, os.ModePerm)
|
||||||
|
want = append(want, p)
|
||||||
|
}
|
||||||
|
os.Mkdir(path.Join(d, "d"), os.ModePerm)
|
||||||
|
|
||||||
|
got, err := listDir(d)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if fmt.Sprint(want) != fmt.Sprint(got) {
|
||||||
|
t.Fatal(want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
4
cmd/pttodo-cli/testdata/1.yaml
vendored
Normal file
4
cmd/pttodo-cli/testdata/1.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
todo:
|
||||||
|
- "1"
|
||||||
|
scheduled: []
|
||||||
|
done: []
|
||||||
4
cmd/pttodo-cli/testdata/2.yaml
vendored
Normal file
4
cmd/pttodo-cli/testdata/2.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
todo:
|
||||||
|
- "2"
|
||||||
|
scheduled: []
|
||||||
|
done: []
|
||||||
4
cmd/pttodo-cli/testdata/3.yaml
vendored
Normal file
4
cmd/pttodo-cli/testdata/3.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
todo:
|
||||||
|
- "3"
|
||||||
|
scheduled: []
|
||||||
|
done: []
|
||||||
4
cmd/pttodo-cli/testdata/root.yaml
vendored
Normal file
4
cmd/pttodo-cli/testdata/root.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
todo:
|
||||||
|
- root
|
||||||
|
scheduled: []
|
||||||
|
done: []
|
||||||
20
cmd/pttodo-cli/testdata/test.yaml
vendored
Executable file
20
cmd/pttodo-cli/testdata/test.yaml
vendored
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
todo:
|
||||||
|
- b
|
||||||
|
scheduled:
|
||||||
|
- todo: abc
|
||||||
|
schedule: "2022-05-01"
|
||||||
|
ts: Wed Mar 23 08:03:45 MDT 2022
|
||||||
|
- todo: def
|
||||||
|
schedule: "2022-05-01"
|
||||||
|
ts: Wed Mar 23 08:04:05 MDT 2022
|
||||||
|
done:
|
||||||
|
- todo: other
|
||||||
|
ts: Wed Mar 23 09:44:57 MDT 2022
|
||||||
|
- todo: b
|
||||||
|
ts: Wed Mar 23 09:45:13 MDT 2022
|
||||||
|
- todo: a
|
||||||
|
ts: Wed Mar 23 09:45:15 MDT 2022
|
||||||
|
- todo: a
|
||||||
|
ts: Wed Mar 23 09:45:24 MDT 2022
|
||||||
|
- todo: a
|
||||||
|
ts: Wed Mar 23 09:45:31 MDT 2022
|
||||||
8
go.mod
8
go.mod
@@ -6,3 +6,11 @@ require (
|
|||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e // indirect
|
||||||
|
github.com/emersion/go-imap v1.2.0 // indirect
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
|
||||||
|
golang.org/x/text v0.3.7 // indirect
|
||||||
|
)
|
||||||
|
|
||||||
|
|||||||
12
go.sum
12
go.sum
@@ -1,5 +1,17 @@
|
|||||||
|
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e h1:mQTN05gz0rDZSABqKMzAPMb5ATWcvvdMljRzEh0LjBo=
|
||||||
|
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e/go.mod h1:alXX+s7a4cKaIprgjeEboqi4Tm7XR/HXEwUTxUV/ywU=
|
||||||
|
github.com/emersion/go-imap v1.2.0 h1:lyUQ3+EVM21/qbWE/4Ya5UG9r5+usDxlg4yfp3TgHFA=
|
||||||
|
github.com/emersion/go-imap v1.2.0/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
|
||||||
|
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||||
|
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
|||||||
@@ -6,6 +6,37 @@ type Root struct {
|
|||||||
Done []Todo
|
Done []Todo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (root Root) Equals(other Root) bool {
|
||||||
|
for i, slice := range [][2][]Todo{
|
||||||
|
[2][]Todo{root.Todo, other.Todo},
|
||||||
|
[2][]Todo{root.Scheduled, other.Scheduled},
|
||||||
|
[2][]Todo{root.Done, other.Done},
|
||||||
|
} {
|
||||||
|
_ = i
|
||||||
|
if !equalTodoSlices(slice[0], slice[1]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (root *Root) AutoMove() {
|
||||||
|
root.MoveScheduledToTodo()
|
||||||
|
root.MoveTodoToScheduled()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (root *Root) MoveTodoToScheduled() {
|
||||||
|
for i := len(root.Todo) - 1; i >= 0; i-- {
|
||||||
|
if root.Todo[i].Schedule != "" && !root.Todo[i].Triggered() {
|
||||||
|
root.Scheduled = append(root.Scheduled, root.Todo[i])
|
||||||
|
for j := i; j < len(root.Todo)-1; j++ {
|
||||||
|
root.Todo[j] = root.Todo[j+1]
|
||||||
|
}
|
||||||
|
root.Todo = root.Todo[:len(root.Todo)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (root *Root) MoveScheduledToTodo() {
|
func (root *Root) MoveScheduledToTodo() {
|
||||||
for i := len(root.Scheduled) - 1; i >= 0; i-- {
|
for i := len(root.Scheduled) - 1; i >= 0; i-- {
|
||||||
if root.Scheduled[i].Triggered() {
|
if root.Scheduled[i].Triggered() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package pttodo
|
package pttodo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -24,6 +25,8 @@ func TestJSONRoot(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
} else if fmt.Sprint(root) != fmt.Sprint(root2) {
|
} else if fmt.Sprint(root) != fmt.Sprint(root2) {
|
||||||
t.Fatal(root, root2)
|
t.Fatal(root, root2)
|
||||||
|
} else if !root.Equals(root2) {
|
||||||
|
t.Fatal(root2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +98,8 @@ func TestRootMoveScheduledToTodo(t *testing.T) {
|
|||||||
got.MoveScheduledToTodo()
|
got.MoveScheduledToTodo()
|
||||||
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
|
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
|
||||||
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
|
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
|
||||||
|
} else if !got.Equals(want) {
|
||||||
|
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -144,6 +149,8 @@ todo:
|
|||||||
root0.MergeIn(root1)
|
root0.MergeIn(root1)
|
||||||
if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) {
|
if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) {
|
||||||
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
|
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
|
||||||
|
} else if !root0.Equals(rootWant) {
|
||||||
|
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,17 +164,19 @@ func TestRootMarshalYAMLWriteTS(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(string(got)) != strings.TrimSpace(`
|
got = bytes.TrimSpace(got)
|
||||||
|
want := strings.TrimSpace(`
|
||||||
todo:
|
todo:
|
||||||
- todo
|
- todo
|
||||||
scheduled:
|
scheduled:
|
||||||
- todo: sched
|
- todo: sched
|
||||||
schedule: "2099-01-01"
|
schedule: "2099-01-01"
|
||||||
ts: Wed Dec 31 19:00:02 EST 1969
|
ts: Wed Dec 31 17:00:02 MST 1969
|
||||||
done:
|
done:
|
||||||
- todo: done
|
- todo: done
|
||||||
ts: Wed Dec 31 19:00:03 EST 1969
|
ts: Wed Dec 31 17:00:03 MST 1969
|
||||||
`) {
|
`)
|
||||||
t.Fatal(string(got))
|
if string(got) != want {
|
||||||
|
t.Fatalf("want\n\t%q, got\n\t%q", want, string(got))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,8 +94,11 @@ func (due scheduleDue) next(time.Time) (time.Time, error) {
|
|||||||
// 2022-01-01
|
// 2022-01-01
|
||||||
type scheduleEZDate string
|
type scheduleEZDate string
|
||||||
|
|
||||||
var scheduleEZDatePattern = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}$`)
|
var scheduleEZDatePattern = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}(T[0-9]{2})?$`)
|
||||||
|
|
||||||
func (ezdate scheduleEZDate) next(time.Time) (time.Time, error) {
|
func (ezdate scheduleEZDate) next(time.Time) (time.Time, error) {
|
||||||
return time.Parse("2006-01-02", string(ezdate))
|
if len(ezdate) == len("20xx-xx-xxTxx") {
|
||||||
|
return time.ParseInLocation("2006-01-02T15", string(ezdate), time.Local)
|
||||||
|
}
|
||||||
|
return time.ParseInLocation("2006-01-02", string(ezdate), time.Local)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,11 +51,17 @@ func TestJSONSchedule(t *testing.T) {
|
|||||||
func TestSchedulerFactory(t *testing.T) {
|
func TestSchedulerFactory(t *testing.T) {
|
||||||
start := time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC)
|
start := time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC)
|
||||||
someDay := time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)
|
someDay := time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)
|
||||||
|
someHour := time.Date(2001, 2, 3, 15, 0, 0, 0, time.UTC)
|
||||||
cases := map[string]struct {
|
cases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
want interface{}
|
want interface{}
|
||||||
next time.Time
|
next time.Time
|
||||||
}{
|
}{
|
||||||
|
"ezdate with hour": {
|
||||||
|
input: `2000-01-03T15`,
|
||||||
|
want: scheduleEZDate(`2000-01-03T15`),
|
||||||
|
next: someHour,
|
||||||
|
},
|
||||||
"long dur": {
|
"long dur": {
|
||||||
input: `2h1m`,
|
input: `2h1m`,
|
||||||
want: scheduleDuration("2h1m"),
|
want: scheduleDuration("2h1m"),
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package pttodo
|
package pttodo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash/crc32"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,6 +17,18 @@ type Todo struct {
|
|||||||
writeTS bool
|
writeTS bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (todo Todo) ID() string {
|
||||||
|
hash := crc32.NewIEEE()
|
||||||
|
fmt.Fprintf(hash, "%d:%s", 0, todo.Todo)
|
||||||
|
fmt.Fprintf(hash, "%d:%s", 1, todo.Details)
|
||||||
|
fmt.Fprintf(hash, "%d:%s", 2, todo.Schedule)
|
||||||
|
fmt.Fprintf(hash, "%d:%s", 3, todo.Tags)
|
||||||
|
for i := range todo.Subtasks {
|
||||||
|
fmt.Fprintf(hash, "%d:%s", 4, todo.Subtasks[i].ID())
|
||||||
|
}
|
||||||
|
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
func (todo Todo) Triggered() bool {
|
func (todo Todo) Triggered() bool {
|
||||||
last := todo.TS
|
last := todo.TS
|
||||||
next, err := todo.Schedule.Next(last.time())
|
next, err := todo.Schedule.Next(last.time())
|
||||||
@@ -43,3 +57,37 @@ func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
alt := (*Alt)(todo)
|
alt := (*Alt)(todo)
|
||||||
return unmarshal(alt)
|
return unmarshal(alt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (todo Todo) Equals(other Todo) bool {
|
||||||
|
if !equalTodoSlices(todo.Subtasks, other.Subtasks) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if todo.TS != other.TS {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if todo.Tags != other.Tags {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if todo.Schedule != other.Schedule {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if todo.Details != other.Details {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if todo.Todo != other.Todo {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func equalTodoSlices(a, b []Todo) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range a {
|
||||||
|
if !a[i].Equals(b[i]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -125,3 +125,33 @@ func TestMarshalTodo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTodoID(t *testing.T) {
|
||||||
|
cases := map[string]Todo{
|
||||||
|
"empty": Todo{},
|
||||||
|
"todo": Todo{Todo: "abc"},
|
||||||
|
"details": Todo{Details: "abc"},
|
||||||
|
"todo,details": Todo{Todo: "abc", Details: "abc"},
|
||||||
|
}
|
||||||
|
|
||||||
|
got := map[string]bool{}
|
||||||
|
for name, todod := range cases {
|
||||||
|
todo := todod
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
if _, ok := got[todo.ID()]; ok {
|
||||||
|
t.Error("dupe", todo.ID())
|
||||||
|
}
|
||||||
|
got[todo.ID()] = true
|
||||||
|
t.Logf("%s: %+v", todo.ID(), todo)
|
||||||
|
todo2 := todo
|
||||||
|
todo2.TS = 1
|
||||||
|
if todo.ID() != todo2.ID() {
|
||||||
|
t.Error("ts changed")
|
||||||
|
}
|
||||||
|
todo2.writeTS = true
|
||||||
|
if todo.ID() != todo2.ID() {
|
||||||
|
t.Error("writets changed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func (ts *TS) UnmarshalYAML(unmarshaller func(interface{}) error) error {
|
|||||||
}
|
}
|
||||||
var s string
|
var s string
|
||||||
if err := unmarshaller(&s); err == nil {
|
if err := unmarshaller(&s); err == nil {
|
||||||
t, err := time.Parse(time.UnixDate, s)
|
t, err := time.ParseInLocation(time.UnixDate, s, time.Local)
|
||||||
*ts = TS(t.Unix())
|
*ts = TS(t.Unix())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
34
todo.yaml
34
todo.yaml
@@ -1,20 +1,11 @@
|
|||||||
todo:
|
todo:
|
||||||
- when merging, check for complete/incomplete and cross reference todo vs scheduled
|
- when merging, check for complete/incomplete and cross reference todo vs scheduled
|
||||||
vs done
|
vs done
|
||||||
- how to defer a scheduled task?
|
|
||||||
- merge multi todo files for 'inbox' style with some dedupe (probably best to just
|
|
||||||
do this via calling pttodo-cli from script or somethin) (probably at an off hour
|
|
||||||
so no collisions from live editing)
|
|
||||||
- click on links when dumped via cli
|
- click on links when dumped via cli
|
||||||
- what do about todo-now vs todo vs watch vs later... could do many files and manage
|
scheduled: []
|
||||||
outside binary, but search would suck. Good would be vendor lockin, and that's UI
|
done:
|
||||||
problem
|
- xactions emails extend ledger,todo-server with pttodo - schedule merge inbox style
|
||||||
- todo: when to run scheduled modifier? like, syncthing could have conflicts if I
|
- xactions emails extend ledger,todo-server with pttodo
|
||||||
modify only file on remote
|
|
||||||
details: |
|
|
||||||
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting
|
|
||||||
- but thats a step back from current todo solution
|
|
||||||
tags: stuffToTry,secondTag
|
|
||||||
- todo: ez edit on many platforms, even offline and mobile
|
- todo: ez edit on many platforms, even offline and mobile
|
||||||
details: |
|
details: |
|
||||||
mobile view + complete method
|
mobile view + complete method
|
||||||
@@ -23,10 +14,19 @@ todo:
|
|||||||
web server access to ops
|
web server access to ops
|
||||||
is a web ui for mobile best solution?
|
is a web ui for mobile best solution?
|
||||||
let git be smart-ish and keep latest? would provide versioning OOTB without touching raw
|
let git be smart-ish and keep latest? would provide versioning OOTB without touching raw
|
||||||
- xactions emails extend ledger,todo-server with pttodo
|
- todo: when to run scheduled modifier? like, syncthing could have conflicts if I
|
||||||
- xactions emails extend ledger,todo-server with pttodo - schedule merge inbox style
|
modify only file on remote
|
||||||
scheduled: []
|
details: |
|
||||||
done:
|
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting
|
||||||
|
- but thats a step back from current todo solution
|
||||||
|
tags: stuffToTry,secondTag
|
||||||
|
- what do about todo-now vs todo vs watch vs later... could do many files and manage
|
||||||
|
outside binary, but search would suck. Good would be vendor lockin, and that's UI
|
||||||
|
problem
|
||||||
|
- merge multi todo files for 'inbox' style with some dedupe (probably best to just
|
||||||
|
do this via calling pttodo-cli from script or somethin) (probably at an off hour
|
||||||
|
so no collisions from live editing)
|
||||||
|
- how to defer a scheduled task?
|
||||||
- todo: add tag anti-search, like -tag=-notMe
|
- todo: add tag anti-search, like -tag=-notMe
|
||||||
ts: Tue Jan 4 06:56:03 EST 2022
|
ts: Tue Jan 4 06:56:03 EST 2022
|
||||||
- todo: if in todo, then omit ts field
|
- todo: if in todo, then omit ts field
|
||||||
|
|||||||
Reference in New Issue
Block a user