diff --git a/pttodo/root.go b/pttodo/root.go index c8bb814..ff7baee 100644 --- a/pttodo/root.go +++ b/pttodo/root.go @@ -13,6 +13,19 @@ type Root struct { Done []Todo } +func NewRootFromFiles(p ...string) (Root, error) { + var result Root + for _, p := range p { + subroot, err := NewRootFromFile(p) + if err != nil { + return Root{}, err + } + result.MergeIn(subroot) + } + result.AutoMove() + return result, nil +} + func NewRootFromFile(p string) (Root, error) { if b, err := os.ReadFile(p); err == nil && len(bytes.TrimSpace(b)) == 0 { return Root{}, nil diff --git a/pttodo/root_test.go b/pttodo/root_test.go index 43f5c70..c72815b 100644 --- a/pttodo/root_test.go +++ b/pttodo/root_test.go @@ -233,3 +233,27 @@ func TestRootFromFile(t *testing.T) { }) } } + +func TestRootFromFiles(t *testing.T) { + d := t.TempDir() + ps := []string{ + path.Join(d, "a"), + path.Join(d, "b"), + } + os.WriteFile(ps[0], []byte(`["a"]`), os.ModePerm) + os.WriteFile(ps[1], []byte(`["b"]`), os.ModePerm) + + got, err := NewRootFromFiles(ps...) + if err != nil { + t.Fatal(err) + } + want := Root{ + Todo: []Todo{ + {Todo: "a"}, + {Todo: "b"}, + }, + } + if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) { + t.Errorf("want\n\t%+v, got \n\t%+v", want, got) + } +}