Skip to content

Commit

Permalink
Merge pull request #42 from NonerKao/master
Browse files Browse the repository at this point in the history
Global repo as the back-up plan
  • Loading branch information
Grant Ammons authored Mar 14, 2017
2 parents 1773d9f + 652a3b0 commit a7f4fa6
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion todolist/file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
)

type FileStore struct {
Expand All @@ -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!")
Expand All @@ -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.")
Expand Down

0 comments on commit a7f4fa6

Please sign in to comment.