aboutsummaryrefslogtreecommitdiffstats
path: root/file_store.go
blob: 3f71ba27f93ffd3372384294ccad0cdbc18d6dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)
	}

}