add pttodo.NewRootFromFile, pttodo.NewTodosFromFile

This commit is contained in:
Bel LaPointe
2023-11-06 12:14:27 -07:00
parent 4e69646e88
commit 760c822323
2 changed files with 51 additions and 0 deletions

View File

@@ -4,7 +4,10 @@ import (
"encoding/base64"
"fmt"
"hash/crc32"
"os"
"time"
yaml "gopkg.in/yaml.v2"
)
type Todo struct {
@@ -17,6 +20,24 @@ type Todo struct {
writeTS bool
}
func NewTodosFromFile(p string) ([]Todo, error) {
f, err := os.Open(p)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
defer f.Close()
var result []Todo
if err := yaml.NewDecoder(f).Decode(&result); err != nil {
return nil, err
}
return result, nil
}
func (todo Todo) ID() string {
hash := crc32.NewIEEE()
fmt.Fprintf(hash, "%d:%s", 0, todo.Todo)