From 08bfbb5ba4be9bf936c86c8cfd38cfe9835a13d4 Mon Sep 17 00:00:00 2001 From: Quey-Liang Kao Date: Sat, 4 Feb 2017 20:58:16 +0800 Subject: Global repo file as the default This patch implments the new workflow mentioned in #31, in which the program still tries to find a repo file in the working directory first, and it tries the global one in the home directory if no one is found. In this patch, the condition check of the existence of the repo file is done in `todolist/file_store.go` at the very early step before the input routing. --- todo.go | 4 ++++ todolist/app.go | 5 +++++ todolist/file_store.go | 22 +++++++++++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/todo.go b/todo.go index 2a45f28..f7d994d 100644 --- a/todo.go +++ b/todo.go @@ -110,6 +110,10 @@ func usage() { func routeInput(command string, input string) { app := todolist.NewApp() + if app == nil { + return + } + switch command { case "l", "list", "agenda": app.ListTodos(input) diff --git a/todolist/app.go b/todolist/app.go index 1ab6b20..e3d8cc7 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -15,6 +15,11 @@ type App struct { func NewApp() *App { app := &App{TodoList: &TodoList{}, TodoStore: NewFileStore()} + if app.TodoStore.FileLocation == "" { + fmt.Println("No todo file found!") + fmt.Println("You may run 'todo init' to initialize an empty repo in current directory.") + return nil + } return app } diff --git a/todolist/file_store.go b/todolist/file_store.go index 5413295..63738b2 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,33 @@ 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 { + return &FileStore{FileLocation: "", Loaded: false} + } } 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) } -- cgit v1.3 From 89b73b0df1a5c06cf2399bfb0d46a83e053c6561 Mon Sep 17 00:00:00 2001 From: Quey-Liang Kao Date: Sun, 19 Feb 2017 22:12:39 +0800 Subject: Refine previous commit --- todolist/app.go | 5 ----- todolist/file_store.go | 5 ++++- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/todolist/app.go b/todolist/app.go index e3d8cc7..1ab6b20 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -15,11 +15,6 @@ type App struct { func NewApp() *App { app := &App{TodoList: &TodoList{}, TodoStore: NewFileStore()} - if app.TodoStore.FileLocation == "" { - fmt.Println("No todo file found!") - fmt.Println("You may run 'todo init' to initialize an empty repo in current directory.") - return nil - } return app } diff --git a/todolist/file_store.go b/todolist/file_store.go index 63738b2..4a7f0e0 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -25,7 +25,10 @@ func NewFileStore() *FileStore { } else if err2 == nil { return &FileStore{FileLocation: homerepo, Loaded: false} } else { - return &FileStore{FileLocation: "", Loaded: false} + 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 } } -- cgit v1.3 From 369097a97856c70ab213428957c86abc9937a069 Mon Sep 17 00:00:00 2001 From: Quey-Liang Kao Date: Mon, 20 Feb 2017 02:16:31 +0800 Subject: Refine the commits --- todo.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/todo.go b/todo.go index f7d994d..2a45f28 100644 --- a/todo.go +++ b/todo.go @@ -110,10 +110,6 @@ func usage() { func routeInput(command string, input string) { app := todolist.NewApp() - if app == nil { - return - } - switch command { case "l", "list", "agenda": app.ListTodos(input) -- cgit v1.3