aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2017-03-14 16:35:39 -0400
committerGitHub <noreply@github.com>2017-03-14 16:35:39 -0400
commita7f4fa609e805b7c85dd3b1783182ef987d91679 (patch)
treecb9934a3685d091e7bab63c0bc20ea09934f0ecc /todolist
parent1773d9f07686552e919af27ec1390c1994b1ac7e (diff)
parent652a3b06b9de39e147168525c9e90c49760f11da (diff)
Merge pull request #42 from NonerKao/master0.5.2
Global repo as the back-up plan
Diffstat (limited to 'todolist')
-rw-r--r--todolist/file_store.go24
1 files changed, 23 insertions, 1 deletions
diff --git a/todolist/file_store.go b/todolist/file_store.go
index 5413295..a6fa0d7 100644
--- a/todolist/file_store.go
+++ b/todolist/file_store.go
@@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
+ "os/user"
)
type FileStore struct {
@@ -13,10 +14,27 @@ type FileStore struct {
}
func NewFileStore() *FileStore {
- return &FileStore{FileLocation: ".todos.json", Loaded: false}
+ 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)
+
+ if ferr == nil {
+ return localrepo
+ } else {
+ return homerepo
+ }
}
func (f *FileStore) Load() ([]*Todo, error) {
+ if f.FileLocation == "" {
+ f.FileLocation = getLocation()
+ }
+
data, err := ioutil.ReadFile(f.FileLocation)
if err != nil {
fmt.Println("No todo file found!")
@@ -38,6 +56,10 @@ func (f *FileStore) Load() ([]*Todo, error) {
}
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.")