aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-04-24 10:04:53 -0400
committerGrant Ammons <gammons@gmail.com>2016-04-24 10:04:53 -0400
commit6bb90375f4efb2f390a61ede0df70eea1ef1f50f (patch)
tree8ff0f6ae2c5da883175d763e1b6050e18da19b4a
parentaf8b1814aa3f5a63e913923449b29cac748fba57 (diff)
Read from user's home directory
-rw-r--r--file_store.go7
-rw-r--r--file_store_test.go9
2 files changed, 13 insertions, 3 deletions
diff --git a/file_store.go b/file_store.go
index 03704f9..3f71ba2 100644
--- a/file_store.go
+++ b/file_store.go
@@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
+ "os"
+ "os/user"
)
type FileStore struct {
@@ -12,18 +14,21 @@ type FileStore struct {
}
func NewFileStore() *FileStore {
- return &FileStore{FileLocation: "todos.json"}
+ 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)
}
}
diff --git a/file_store_test.go b/file_store_test.go
index b7132a8..8bb23e0 100644
--- a/file_store_test.go
+++ b/file_store_test.go
@@ -1,9 +1,14 @@
package todolist
-import "testing"
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
func TestFileStore(t *testing.T) {
+ assert := assert.New(t)
store := &FileStore{FileLocation: "todos.json"}
store.Load()
- store.Data[0]
+ assert.Equal(store.Data[0].Subject, "this is the first subject", "")
}