test pttodo.NewRootFromFile accepts both []Todo and Root

This commit is contained in:
Bel LaPointe
2023-11-06 12:54:46 -07:00
parent 92e8e14c08
commit 3e1f58c7b9
3 changed files with 66 additions and 48 deletions

View File

@@ -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()