move pttodo-cli up from /cmd/
This commit is contained in:
106
cmd/dump.go
Normal file
106
cmd/dump.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func dump(config config) error {
|
||||
return _dump(os.Stdout, config.targets, config.tags, config.search, config.root)
|
||||
}
|
||||
|
||||
func _dump(writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
||||
var root pttodo.Root
|
||||
|
||||
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 !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()
|
||||
|
||||
var v interface{} = root
|
||||
switch rootDisplay {
|
||||
case DUMP_ALL:
|
||||
case DUMP_TODO:
|
||||
v = root.Todo
|
||||
case DUMP_SCHEDULED:
|
||||
v = root.Scheduled
|
||||
case DUMP_DONE:
|
||||
v = root.Done
|
||||
}
|
||||
if todos, ok := v.([]pttodo.Todo); ok {
|
||||
if len(tags) > 0 {
|
||||
result := make([]pttodo.Todo, 0, len(todos))
|
||||
for _, todo := range todos {
|
||||
skip := false
|
||||
for _, tag := range tags {
|
||||
positiveTag := strings.TrimLeft(tag, "-")
|
||||
hasTag := strings.Contains(todo.Tags, positiveTag)
|
||||
wantToHaveTag := !strings.HasPrefix(tag, "-")
|
||||
skip = skip || !(hasTag == wantToHaveTag)
|
||||
}
|
||||
if !skip {
|
||||
result = append(result, todo)
|
||||
}
|
||||
}
|
||||
todos = result
|
||||
}
|
||||
if len(search) > 0 {
|
||||
result := make([]pttodo.Todo, 0, len(todos))
|
||||
for _, todo := range todos {
|
||||
if strings.Contains(strings.ToLower(fmt.Sprint(todo)), strings.ToLower(search)) {
|
||||
result = append(result, todo)
|
||||
}
|
||||
}
|
||||
todos = result
|
||||
}
|
||||
v = todos
|
||||
}
|
||||
|
||||
b2, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(writer, "%s\n", b2)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user