diff options
| author | Quey-Liang Kao <s101062801@m101.nthu.edu.tw> | 2017-03-05 20:58:23 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-05 20:58:23 -0600 |
| commit | 39d01ff369695cc9a3ac92cfea3c7f9169aceb0a (patch) | |
| tree | 4bd7f76cccf5567f0d812212c6df0b0cce9512eb | |
| parent | be32789ec53ebe0a2141cf4d13696280ef79ee7f (diff) | |
| parent | 369097a97856c70ab213428957c86abc9937a069 (diff) | |
Merge pull request #32 from NonerKao/master
Global repo file as the default
| -rw-r--r-- | todolist/file_store.go | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/todolist/file_store.go b/todolist/file_store.go index 5413295..4a7f0e0 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,22 +14,36 @@ type FileStore struct { } func NewFileStore() *FileStore { - return &FileStore{FileLocation: ".todos.json", Loaded: false} + localrepo := ".todos.json" + usr, _ := user.Current() + homerepo := fmt.Sprintf("%s/.todos.json", usr.HomeDir) + _, err1 := os.Stat(localrepo) + _, err2 := os.Stat(homerepo) + + if err1 == nil { + return &FileStore{FileLocation: localrepo, Loaded: false} + } else if err2 == nil { + return &FileStore{FileLocation: homerepo, Loaded: false} + } else { + fmt.Println("No todo file found!") + fmt.Println("You may run 'todo init' to initialize an empty repo in working directory.") + os.Exit(1) + return nil + } } func (f *FileStore) Load() ([]*Todo, error) { data, err := ioutil.ReadFile(f.FileLocation) if err != nil { - fmt.Println("No todo file found!") - fmt.Println("Initialize a new todo repo by running 'todo init'") + fmt.Println("Error reading", f.FileLocation, "by", err) return nil, err - os.Exit(0) + os.Exit(1) } var todos []*Todo jerr := json.Unmarshal(data, &todos) if jerr != nil { - fmt.Println("Error reading json data", jerr) + fmt.Println("Error reading", f.FileLocation, "by", jerr) return nil, jerr os.Exit(1) } |
