@@ -9,28 +9,14 @@ import (
99 "path/filepath"
1010 "strings"
1111
12- "github.com/goccy/go-yaml"
1312 "github.com/spf13/viper"
1413
1514 "github.com/DanWlker/remind/internal/config"
15+ "github.com/DanWlker/remind/internal/shared"
1616)
1717
1818type EditTextFunc func (todo string , index int ) (string , error )
1919
20- func FGetTodoFromReader (r io.Reader ) ([]TodoEntity , error ) {
21- var items []TodoEntity
22- dec := yaml .NewDecoder (r )
23-
24- err := dec .Decode (& items )
25- if errors .Is (err , io .EOF ) {
26- return nil , nil
27- } else if err != nil {
28- return nil , fmt .Errorf ("dec.Decode: %w" , err )
29- }
30-
31- return items , nil
32- }
33-
3420// This does not create the file if it doesn't exist
3521func GetTodoFromFile (fileFullPath string ) (items []TodoEntity , err error ) {
3622 f , err := os .Open (fileFullPath )
@@ -43,22 +29,13 @@ func GetTodoFromFile(fileFullPath string) (items []TodoEntity, err error) {
4329 }
4430 }()
4531
46- items , err = FGetTodoFromReader (f )
32+ items , err = shared. FGetStructFromYaml [ TodoEntity ] (f )
4733 if err != nil {
48- return nil , fmt .Errorf ("FGetTodoFromReader : %w" , err )
34+ return nil , fmt .Errorf ("FGetStructFromYaml : %w" , err )
4935 }
5036 return items , nil
5137}
5238
53- func FWriteTodoToFile (w io.Writer , todoList []TodoEntity ) error {
54- enc := yaml .NewEncoder (w )
55- if err := enc .Encode (todoList ); err != nil {
56- return fmt .Errorf ("enc.Encode: %w" , err )
57- }
58-
59- return nil
60- }
61-
6239func WriteTodoToFile (fileFullPath string , todoList []TodoEntity ) (err error ) {
6340 // Opens with
6441 // Write - WRONLY
@@ -77,8 +54,8 @@ func WriteTodoToFile(fileFullPath string, todoList []TodoEntity) (err error) {
7754 }
7855 }()
7956
80- if err = FWriteTodoToFile (f , todoList ); err != nil {
81- return fmt .Errorf ("FWriteTodoToFile : %w" , err )
57+ if err = shared . FWriteStructToYaml (f , todoList ); err != nil {
58+ return fmt .Errorf ("FWriteStructToYaml : %w" , err )
8259 }
8360
8461 return nil
0 commit comments