diff options
| author | Grant Ammons <gammons@gmail.com> | 2017-06-19 12:58:27 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-06-19 12:58:27 -0400 |
| commit | dafb74428d8a16b28533aa25f83f25ea255c18a3 (patch) | |
| tree | b5df1531dec03b2a2beb3a1ea05b42938684b937 /todolist/file_store.go | |
| parent | 4a91b284cc1840ad4dc4e60e831c8c6e3de283de (diff) | |
| parent | 0d815737ccb61a6664a9943516518decdddf0bb2 (diff) | |
Merge pull request #68 from gammons/code-cleanup
Fix parsing a todo with europe date format
Diffstat (limited to 'todolist/file_store.go')
| -rw-r--r-- | todolist/file_store.go | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/todolist/file_store.go b/todolist/file_store.go index a6fa0d7..2eee5c1 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -17,17 +17,21 @@ func NewFileStore() *FileStore { return &FileStore{FileLocation: "", Loaded: false} } -func getLocation() string { - localrepo := ".todos.json" - usr, _ := user.Current() - homerepo := fmt.Sprintf("%s/.todos.json", usr.HomeDir) - _, ferr := os.Stat(localrepo) +func (f *FileStore) Initialize() { + if f.FileLocation == "" { + f.FileLocation = ".todos.json" + } - if ferr == nil { - return localrepo - } else { - return homerepo + _, err := ioutil.ReadFile(f.FileLocation) + if err == nil { + fmt.Println("It looks like a .todos.json file already exists! Doing nothing.") + os.Exit(0) + } + if err := ioutil.WriteFile(f.FileLocation, []byte("[]"), 0644); err != nil { + fmt.Println("Error writing json file", err) + os.Exit(1) } + fmt.Println("Todo repo initialized.") } func (f *FileStore) Load() ([]*Todo, error) { @@ -39,42 +43,38 @@ func (f *FileStore) Load() ([]*Todo, error) { if err != nil { fmt.Println("No todo file found!") fmt.Println("Initialize a new todo repo by running 'todo init'") - return nil, err os.Exit(0) + return nil, err } var todos []*Todo jerr := json.Unmarshal(data, &todos) if jerr != nil { fmt.Println("Error reading json data", jerr) - return nil, jerr os.Exit(1) + return nil, jerr } f.Loaded = true return todos, nil } -func (f *FileStore) Initialize() { - if f.FileLocation == "" { - f.FileLocation = ".todos.json" - } - - _, err := ioutil.ReadFile(f.FileLocation) - if err == nil { - fmt.Println("It looks like a .todos.json file already exists! Doing nothing.") - os.Exit(0) - } - if err := ioutil.WriteFile(f.FileLocation, []byte("[]"), 0644); err != nil { - fmt.Println("Error writing json file", err) - os.Exit(1) - } - fmt.Println("Todo repo initialized.") -} - func (f *FileStore) Save(todos []*Todo) { data, _ := json.Marshal(todos) if err := ioutil.WriteFile(f.FileLocation, []byte(data), 0644); err != nil { fmt.Println("Error writing json file", err) } } + +func getLocation() string { + localrepo := ".todos.json" + usr, _ := user.Current() + homerepo := fmt.Sprintf("%s/.todos.json", usr.HomeDir) + _, ferr := os.Stat(localrepo) + + if ferr == nil { + return localrepo + } else { + return homerepo + } +} |
