From 63893587cb41a1980318b60bf05fafbf433a24ab Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Sun, 24 Apr 2016 10:22:33 -0400 Subject: Organize things a bit better --- todolist/file_store.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 todolist/file_store.go (limited to 'todolist/file_store.go') diff --git a/todolist/file_store.go b/todolist/file_store.go new file mode 100644 index 0000000..3f71ba2 --- /dev/null +++ b/todolist/file_store.go @@ -0,0 +1,34 @@ +package todolist + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "os/user" +) + +type FileStore struct { + FileLocation string + Data []Todo +} + +func NewFileStore() *FileStore { + usr, _ := user.Current() + return &FileStore{FileLocation: usr.HomeDir + "/.todos.json"} +} + +func (f *FileStore) Load() { + data, err := ioutil.ReadFile(f.FileLocation) + if err != nil { + fmt.Println("Error reading file", err) + os.Exit(1) + } + + jerr := json.Unmarshal(data, &f.Data) + if jerr != nil { + fmt.Println("Error reading json data", jerr) + os.Exit(1) + } + +} -- cgit v1.3