test pttodo.NewRootFromFile accepts both []Todo and Root
parent
92e8e14c08
commit
3e1f58c7b9
50
cmd/dump.go
50
cmd/dump.go
|
|
@ -3,10 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
||||||
|
|
@ -20,54 +17,13 @@ func dump(config config) error {
|
||||||
func _dump(writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
func _dump(writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
||||||
var root pttodo.Root
|
var root pttodo.Root
|
||||||
|
|
||||||
for _, p := range filepaths {
|
|
||||||
results, err := filepath.Glob(p + ".*")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, result := range results {
|
|
||||||
if result == p {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
filepaths = append(filepaths, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, filepath := range filepaths {
|
for _, filepath := range filepaths {
|
||||||
reader, err := filePathReader(filepath)
|
subroot, err := pttodo.NewRootFromFile(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
subroot.MoveScheduledToTodo()
|
||||||
b, err := ioutil.ReadAll(reader)
|
root.MergeIn(subroot)
|
||||||
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()
|
root.MoveScheduledToTodo()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package pttodo
|
package pttodo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
@ -13,6 +14,10 @@ type Root struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRootFromFile(p string) (Root, error) {
|
func NewRootFromFile(p string) (Root, error) {
|
||||||
|
if b, err := os.ReadFile(p); err == nil && len(bytes.TrimSpace(b)) == 0 {
|
||||||
|
return Root{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Open(p)
|
f, err := os.Open(p)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return Root{}, nil
|
return Root{}, nil
|
||||||
|
|
@ -24,8 +29,12 @@ func NewRootFromFile(p string) (Root, error) {
|
||||||
|
|
||||||
var result Root
|
var result Root
|
||||||
if err := yaml.NewDecoder(f).Decode(&result); err != nil {
|
if err := yaml.NewDecoder(f).Decode(&result); err != nil {
|
||||||
|
todos, err2 := NewTodosFromFile(p)
|
||||||
|
if err2 != nil {
|
||||||
return Root{}, err
|
return Root{}, err
|
||||||
}
|
}
|
||||||
|
result.Todo = todos
|
||||||
|
}
|
||||||
|
|
||||||
result.AutoMove()
|
result.AutoMove()
|
||||||
return result, nil
|
return result, nil
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -180,3 +182,54 @@ done:
|
||||||
t.Fatalf("want\n\t%q, got\n\t%q", want, string(got))
|
t.Fatalf("want\n\t%q, got\n\t%q", want, string(got))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRootFromFile(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
given string
|
||||||
|
want Root
|
||||||
|
}{
|
||||||
|
"empty": {},
|
||||||
|
"happy": {
|
||||||
|
given: `{"todo":["a", "b"],"scheduled":["c"], "done":[{"todo": "d"}]}`,
|
||||||
|
want: Root{
|
||||||
|
Todo: []Todo{
|
||||||
|
Todo{Todo: "a"},
|
||||||
|
Todo{Todo: "b"},
|
||||||
|
},
|
||||||
|
Scheduled: []Todo{
|
||||||
|
Todo{Todo: "c"},
|
||||||
|
},
|
||||||
|
Done: []Todo{
|
||||||
|
Todo{Todo: "d"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"todos": {
|
||||||
|
given: `["a", {"todo": "b"}]`,
|
||||||
|
want: Root{
|
||||||
|
Todo: []Todo{
|
||||||
|
{Todo: "a"},
|
||||||
|
{Todo: "b"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
d := t.TempDir()
|
||||||
|
p := path.Join(d, "input.yaml")
|
||||||
|
if err := os.WriteFile(p, []byte(c.given), os.ModePerm); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
got, err := NewRootFromFile(p)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) {
|
||||||
|
t.Errorf("want\n\t%+v, got\n\t%+v", c.want, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue