split dump.go from main.go in cli
This commit is contained in:
106
cmd/pttodo-cli/dump.go
Normal file
106
cmd/pttodo-cli/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
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -49,7 +48,7 @@ func _main() error {
|
|||||||
if err := edit(config); err != nil {
|
if err := edit(config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return dump(os.Stdout, config.targets, config.tags, config.search, config.root)
|
return dump(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() config {
|
func getConfig() config {
|
||||||
@@ -99,7 +98,7 @@ func verifyRoot(root pttodo.Root) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyFile(path string) error {
|
func verifyFile(path string) error {
|
||||||
if err := dump(io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
|
if err := _dump(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
|
||||||
@@ -160,95 +159,6 @@ func marshalRootToTempFile(root pttodo.Root) (string, error) {
|
|||||||
return filepath, err
|
return filepath, err
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func filePathReader(path string) (io.Reader, error) {
|
func filePathReader(path string) (io.Reader, error) {
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
return os.Stdin, nil
|
return os.Stdin, nil
|
||||||
Reference in New Issue
Block a user