add -dedupe to move dupe TODO and SCHEDULED to DONE
parent
1413d02764
commit
45b2060083
|
|
@ -22,6 +22,7 @@ type config struct {
|
||||||
addTags string
|
addTags string
|
||||||
dryRun bool
|
dryRun bool
|
||||||
archive string
|
archive string
|
||||||
|
dedupe bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() config {
|
func getConfig() config {
|
||||||
|
|
@ -39,6 +40,7 @@ func getConfig() config {
|
||||||
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -x to invert")
|
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -x to invert")
|
||||||
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
||||||
flag.BoolVar(&config.edit, "e", false, "edit file")
|
flag.BoolVar(&config.edit, "e", false, "edit file")
|
||||||
|
flag.BoolVar(&config.dedupe, "dedupe", false, "dedupe file")
|
||||||
flag.BoolVar(&config.dryRun, "dry-run", false, "dry run")
|
flag.BoolVar(&config.dryRun, "dry-run", false, "dry run")
|
||||||
flag.StringVar(&config.add, "add", "", "todo to add")
|
flag.StringVar(&config.add, "add", "", "todo to add")
|
||||||
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
module pttodo-cli
|
module pttodo-cli
|
||||||
|
|
||||||
go 1.17
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gitea.inhome.blapointe.com/gogs/pttodo v0.0.0-20231109151914-5d74b458d542
|
gitea.inhome.blapointe.com/gogs/pttodo v0.0.0-20231109151914-5d74b458d542
|
||||||
|
|
|
||||||
52
cmd/main.go
52
cmd/main.go
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"slices"
|
||||||
|
|
||||||
"gitea.inhome.blapointe.com/gogs/pttodo/pttodo"
|
"gitea.inhome.blapointe.com/gogs/pttodo/pttodo"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
@ -32,6 +34,8 @@ func _main() error {
|
||||||
return merge(&config)
|
return merge(&config)
|
||||||
} else if config.archive != "" {
|
} else if config.archive != "" {
|
||||||
return archive(&config)
|
return archive(&config)
|
||||||
|
} else if config.dedupe {
|
||||||
|
return dedupe(&config)
|
||||||
} else {
|
} else {
|
||||||
if err := add(&config); err != nil {
|
if err := add(&config); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -43,6 +47,54 @@ func _main() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dedupe(config *config) error {
|
||||||
|
baseReader, err := filePathReader(config.target)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
baseB, err := ioutil.ReadAll(baseReader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var base pttodo.Root
|
||||||
|
if err := yaml.Unmarshal(baseB, &base); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
do := func(todos []pttodo.Todo) []pttodo.Todo {
|
||||||
|
i := len(todos) - 1
|
||||||
|
for i >= 0 {
|
||||||
|
j := slices.IndexFunc(todos, func(todo pttodo.Todo) bool {
|
||||||
|
return todo.Equals(todos[i])
|
||||||
|
})
|
||||||
|
if j >= 0 && j != i {
|
||||||
|
todos[j] = todos[i]
|
||||||
|
base.Done = append(base.Done, todos[j])
|
||||||
|
todos = todos[:i]
|
||||||
|
}
|
||||||
|
i -= 1
|
||||||
|
}
|
||||||
|
return todos
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Todo = do(base.Todo)
|
||||||
|
base.Scheduled = do(base.Scheduled)
|
||||||
|
|
||||||
|
if tmppath, err := marshalRootToTempFile(base); err != nil {
|
||||||
|
return err
|
||||||
|
} else if config.dryRun {
|
||||||
|
log.Println("===before===")
|
||||||
|
_dump(os.Stdout, []string{config.target}, config.tags, config.search, config.root)
|
||||||
|
log.Println("===after===")
|
||||||
|
_dump(os.Stdout, []string{tmppath}, config.tags, config.search, config.root)
|
||||||
|
} else if err := rename(tmppath, config.target); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func archive(config *config) error {
|
func archive(config *config) error {
|
||||||
if config.archive == "" {
|
if config.archive == "" {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,18 @@
|
||||||
todo:
|
todo:
|
||||||
- "1"
|
- "1"
|
||||||
scheduled: []
|
- "2"
|
||||||
done: []
|
- "1"
|
||||||
|
- "2"
|
||||||
|
- "2"
|
||||||
|
scheduled:
|
||||||
|
- todo: "10"
|
||||||
|
- todo: "20"
|
||||||
|
- todo: "10"
|
||||||
|
- todo: "20"
|
||||||
|
- todo: "20"
|
||||||
|
done:
|
||||||
|
- todo: "100"
|
||||||
|
- todo: "200"
|
||||||
|
- todo: "100"
|
||||||
|
- todo: "200"
|
||||||
|
- todo: "200"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue