diff options
| author | Grant Ammons <gammons@gmail.com> | 2016-05-05 15:31:47 -0400 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2016-05-05 15:31:47 -0400 |
| commit | 89d111b7f0a05331ce87cc373108a164af23109e (patch) | |
| tree | d244c59acde86aedeee644ef13dcdef4c7469308 | |
| parent | 4fa6d42eda49de8e5e5de88d4aaaa93f83d3fe65 (diff) | |
Handle case where todos.json cant be loaded
| -rw-r--r-- | todolist/file_store.go | 12 | ||||
| -rw-r--r-- | todolist/store.go | 1 |
2 files changed, 11 insertions, 2 deletions
diff --git a/todolist/file_store.go b/todolist/file_store.go index c7c23c7..a6f68b6 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -84,8 +84,10 @@ func (f *FileStore) IndexOf(todoToFind *Todo) int { func (f *FileStore) Load() { data, err := ioutil.ReadFile(f.FileLocation) if err != nil { - fmt.Println("Error reading file", err) - os.Exit(1) + fmt.Println("No todo file found at ~/.todos.json!") + fmt.Println("Initializing new todo repo at ~/.todos.json") + f.Initialize() + return } jerr := json.Unmarshal(data, &f.Data) @@ -95,6 +97,12 @@ func (f *FileStore) Load() { } } +func (f *FileStore) Initialize() { + if err := ioutil.WriteFile(f.FileLocation, []byte("[]"), 0644); err != nil { + fmt.Println("Error writing json file", err) + } +} + func (f *FileStore) Save() { data, _ := json.Marshal(f.Data) if err := ioutil.WriteFile(f.FileLocation, []byte(data), 0644); err != nil { diff --git a/todolist/store.go b/todolist/store.go index 6892f58..b488408 100644 --- a/todolist/store.go +++ b/todolist/store.go @@ -1,6 +1,7 @@ package todolist type Store interface { + Initialize() Load() Save() Todos() []*Todo |
