diff --git a/cmd/dump.go b/cmd/dump.go index 8c91eb2..1a7adf1 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -3,10 +3,7 @@ package main import ( "fmt" "io" - "io/ioutil" - "log" "os" - "path/filepath" "strings" "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 { 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 { - reader, err := filePathReader(filepath) + subroot, err := pttodo.NewRootFromFile(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) + subroot.MoveScheduledToTodo() + root.MergeIn(subroot) } root.MoveScheduledToTodo() diff --git a/pttodo/root.go b/pttodo/root.go index b314959..c8bb814 100644 --- a/pttodo/root.go +++ b/pttodo/root.go @@ -1,6 +1,7 @@ package pttodo import ( + "bytes" "os" yaml "gopkg.in/yaml.v2" @@ -13,6 +14,10 @@ type Root struct { } 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) if os.IsNotExist(err) { return Root{}, nil @@ -24,7 +29,11 @@ func NewRootFromFile(p string) (Root, error) { var result Root if err := yaml.NewDecoder(f).Decode(&result); err != nil { - return Root{}, err + todos, err2 := NewTodosFromFile(p) + if err2 != nil { + return Root{}, err + } + result.Todo = todos } result.AutoMove() diff --git a/pttodo/root_test.go b/pttodo/root_test.go index 29453be..43f5c70 100644 --- a/pttodo/root_test.go +++ b/pttodo/root_test.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/json" "fmt" + "os" + "path" "strconv" "strings" "testing" @@ -180,3 +182,54 @@ done: 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) + } + }) + } +}