From f00bb3e8ed6ea5876c54e33a28c08a19a639c6bb Mon Sep 17 00:00:00 2001 From: Bunchhieng Date: Sun, 11 Sep 2016 15:10:12 -0400 Subject: Only open browser if .todos.json exists --- .todos.json | 1 - todo.go | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.todos.json b/.todos.json index ab83a99..e69de29 100644 --- a/.todos.json +++ b/.todos.json @@ -1 +0,0 @@ -[{"id":1,"subject":"support for priorities +feature","projects":["feature"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":2,"subject":"fix bug with 'tod' not being recognized +bug","projects":["bug"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":3,"subject":"separate into packages/directories +refactor","projects":["refactor"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":4,"subject":"+documentation re-record how it works using this https://asciinema.org/","projects":["documentation"],"contexts":[],"due":"","completed":false,"archived":false},{"id":5,"subject":"+slideplayer nice quote support","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":6,"subject":"+slideplayer image filters using css3","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":7,"subject":"+slideplayer syntax highlighting and code blocks","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":8,"subject":"+slideplayer presenter notes","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false}] \ No newline at end of file diff --git a/todo.go b/todo.go index 74782ef..6560fb0 100644 --- a/todo.go +++ b/todo.go @@ -128,9 +128,21 @@ func routeInput(command string, input string) { case "init": app.InitializeRepo() case "web": - web := todolist.NewWebapp() - fmt.Println("Now serving todolist web.\nHead to http://localhost:7890 to see your todo list!") - open.Start("http://localhost:7890") - web.Run() + // Only open default browser if .todos.json exists + pwd, err := os.Getwd() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + if _, err := os.Stat(pwd + "/.todos.json"); err == nil { + web := todolist.NewWebapp() + fmt.Println("Now serving todolist web.\nHead to http://localhost:7890 to see your todo list!") + open.Start("http://localhost:7890") + web.Run() + } else { + fmt.Println("No todo file found!") + fmt.Println("Initialize a new todo repo by running 'todo init'") + os.Exit(1) + } } } -- cgit v1.3 From e7eb2b8f8cee7ebdf170173617a699e2e023db9b Mon Sep 17 00:00:00 2001 From: Bunchhieng Date: Sun, 11 Sep 2016 15:11:08 -0400 Subject: Only open browser if .todos.json exists --- .todos.json | 1 + todo.go | 1 + 2 files changed, 2 insertions(+) diff --git a/.todos.json b/.todos.json index e69de29..726fc95 100644 --- a/.todos.json +++ b/.todos.json @@ -0,0 +1 @@ +[{"id":1,"subject":"support for priorities +feature","projects":["feature"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":2,"subject":"fix bug with 'tod' not being recognized +bug","projects":["bug"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":3,"subject":"separate into packages/directories +refactor","projects":["refactor"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":4,"subject":"+documentation re-record how it works using this https://asciinema.org/","projects":["documentation"],"contexts":[],"due":"","completed":false,"archived":false},{"id":5,"subject":"+slideplayer nice quote support","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":6,"subject":"+slideplayer image filters using css3","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":7,"subject":"+slideplayer syntax highlighting and code blocks","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":8,"subject":"+slideplayer presenter notes","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false}] diff --git a/todo.go b/todo.go index 6560fb0..8a95142 100644 --- a/todo.go +++ b/todo.go @@ -134,6 +134,7 @@ func routeInput(command string, input string) { fmt.Println(err) os.Exit(1) } + if _, err := os.Stat(pwd + "/.todos.json"); err == nil { web := todolist.NewWebapp() fmt.Println("Now serving todolist web.\nHead to http://localhost:7890 to see your todo list!") -- cgit v1.3 From 695c8ad53213041d9904ef2c8648ecc10df69f5e Mon Sep 17 00:00:00 2001 From: Bunchhieng Date: Sun, 11 Sep 2016 15:11:48 -0400 Subject: Only open browser if .todos.json exists --- todo.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/todo.go b/todo.go index 8a95142..b71d215 100644 --- a/todo.go +++ b/todo.go @@ -130,11 +130,11 @@ func routeInput(command string, input string) { case "web": // Only open default browser if .todos.json exists pwd, err := os.Getwd() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - + if err != nil { + fmt.Println(err) + os.Exit(1) + } + if _, err := os.Stat(pwd + "/.todos.json"); err == nil { web := todolist.NewWebapp() fmt.Println("Now serving todolist web.\nHead to http://localhost:7890 to see your todo list!") -- cgit v1.3 From c94956df1ca37f90a42b5f66ccb63213253e4d3e Mon Sep 17 00:00:00 2001 From: Bunchhieng Date: Thu, 15 Sep 2016 10:13:42 -0400 Subject: Refactored code --- .todos.json | 2 +- todo.go | 12 ++++++++---- todolist/app.go | 9 +++++++-- todolist/file_store.go | 6 ++++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.todos.json b/.todos.json index ab83a99..726fc95 100644 --- a/.todos.json +++ b/.todos.json @@ -1 +1 @@ -[{"id":1,"subject":"support for priorities +feature","projects":["feature"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":2,"subject":"fix bug with 'tod' not being recognized +bug","projects":["bug"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":3,"subject":"separate into packages/directories +refactor","projects":["refactor"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":4,"subject":"+documentation re-record how it works using this https://asciinema.org/","projects":["documentation"],"contexts":[],"due":"","completed":false,"archived":false},{"id":5,"subject":"+slideplayer nice quote support","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":6,"subject":"+slideplayer image filters using css3","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":7,"subject":"+slideplayer syntax highlighting and code blocks","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":8,"subject":"+slideplayer presenter notes","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false}] \ No newline at end of file +[{"id":1,"subject":"support for priorities +feature","projects":["feature"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":2,"subject":"fix bug with 'tod' not being recognized +bug","projects":["bug"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":3,"subject":"separate into packages/directories +refactor","projects":["refactor"],"contexts":[],"due":"2016-07-08","completed":false,"archived":false},{"id":4,"subject":"+documentation re-record how it works using this https://asciinema.org/","projects":["documentation"],"contexts":[],"due":"","completed":false,"archived":false},{"id":5,"subject":"+slideplayer nice quote support","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":6,"subject":"+slideplayer image filters using css3","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":7,"subject":"+slideplayer syntax highlighting and code blocks","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false},{"id":8,"subject":"+slideplayer presenter notes","projects":["slideplayer"],"contexts":[],"due":"","completed":false,"archived":false}] diff --git a/todo.go b/todo.go index 74782ef..2b95c98 100644 --- a/todo.go +++ b/todo.go @@ -128,9 +128,13 @@ func routeInput(command string, input string) { case "init": app.InitializeRepo() case "web": - web := todolist.NewWebapp() - fmt.Println("Now serving todolist web.\nHead to http://localhost:7890 to see your todo list!") - open.Start("http://localhost:7890") - web.Run() + if err := app.Load(); err != nil { + os.Exit(1) + } else { + web := todolist.NewWebapp() + fmt.Println("Now serving todolist web.\nHead to http://localhost:7890 to see your todo list!") + open.Start("http://localhost:7890") + web.Run() + } } } diff --git a/todolist/app.go b/todolist/app.go index 7f64366..44d97f5 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -153,8 +153,13 @@ func (a *App) getGroups(input string, todos []*Todo) *GroupedTodos { return grouped } -func (a *App) Load() { - a.TodoList.Load(a.TodoStore.Load()) +func (a *App) Load() error { + todos, err := a.TodoStore.Load() + if err != nil { + return err + } + a.TodoList.Load(todos) + return nil } func (a *App) Save() { diff --git a/todolist/file_store.go b/todolist/file_store.go index 61e6e7c..5413295 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -16,11 +16,12 @@ func NewFileStore() *FileStore { return &FileStore{FileLocation: ".todos.json", Loaded: false} } -func (f *FileStore) Load() []*Todo { +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'") + return nil, err os.Exit(0) } @@ -28,11 +29,12 @@ func (f *FileStore) Load() []*Todo { jerr := json.Unmarshal(data, &todos) if jerr != nil { fmt.Println("Error reading json data", jerr) + return nil, jerr os.Exit(1) } f.Loaded = true - return todos + return todos, nil } func (f *FileStore) Initialize() { -- cgit v1.3 From 144ac3243b4920d267f224732d9deff0f9f2cc52 Mon Sep 17 00:00:00 2001 From: Bunchhieng Date: Thu, 15 Sep 2016 10:32:37 -0400 Subject: Fixed build failed --- .gitignore | 3 +++ todolist/file_store_test.go | 4 ++-- todolist/filter_test.go | 12 ++++++++---- todolist/grouper_test.go | 6 ++++-- todolist/todo_list_test.go | 18 ++++++++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 22d2c23..9de3318 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .goxc.local.json dist + +# OSX metadata +.DS_Store diff --git a/todolist/file_store_test.go b/todolist/file_store_test.go index 0bd194d..8de79ec 100644 --- a/todolist/file_store_test.go +++ b/todolist/file_store_test.go @@ -9,12 +9,12 @@ import ( func TestFileStore(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} - todos := store.Load() + todos, _ := store.Load() assert.Equal(todos[0].Subject, "this is the first subject", "") } func TestSave(t *testing.T) { store := &FileStore{FileLocation: "todos.json"} - todos := store.Load() + todos, _ := store.Load() store.Save(todos) } diff --git a/todolist/filter_test.go b/todolist/filter_test.go index 59c0daa..e4d3fed 100644 --- a/todolist/filter_test.go +++ b/todolist/filter_test.go @@ -10,7 +10,8 @@ func TestFilterArchived(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) filter := NewFilter(list.Todos()) archived := filter.filterArchived("l archived") assert.Equal(1, len(archived)) @@ -21,7 +22,8 @@ func TestFilterUnarchivedByDefault(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) filter := NewFilter(list.Todos()) unarchived := filter.filterArchived("l") assert.Equal(1, len(unarchived)) @@ -32,7 +34,8 @@ func TestGetArchived(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) filter := NewFilter(list.Todos()) archived := filter.getArchived() assert.Equal(1, len(archived)) @@ -43,7 +46,8 @@ func TestGetUnarchived(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) filter := NewFilter(list.Todos()) unarchived := filter.getUnarchived() assert.Equal(1, len(unarchived)) diff --git a/todolist/grouper_test.go b/todolist/grouper_test.go index 4b79703..12a1018 100644 --- a/todolist/grouper_test.go +++ b/todolist/grouper_test.go @@ -11,7 +11,8 @@ func TestGroupByContext(t *testing.T) { store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) grouper := &Grouper{} grouped := grouper.GroupByContext(list.Todos()) @@ -25,7 +26,8 @@ func TestGroupByProject(t *testing.T) { store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) grouper := &Grouper{} grouped := grouper.GroupByProject(list.Todos()) diff --git a/todolist/todo_list_test.go b/todolist/todo_list_test.go index 941106d..8a40b14 100644 --- a/todolist/todo_list_test.go +++ b/todolist/todo_list_test.go @@ -17,7 +17,8 @@ func TestIndexOf(t *testing.T) { todo := &Todo{Subject: "Grant"} store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) assert.Equal(-1, list.IndexOf(todo)) assert.Equal(0, list.IndexOf(list.Data[0])) @@ -27,7 +28,8 @@ func TestDelete(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) assert.Equal(2, len(list.Data)) list.Delete(1) assert.Equal(1, len(list.Data)) @@ -37,7 +39,8 @@ func TestComplete(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) assert.Equal(false, list.FindById(1).Completed) list.Complete(1) assert.Equal(true, list.FindById(1).Completed) @@ -47,7 +50,8 @@ func TestArchive(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) assert.Equal(false, list.FindById(2).Archived) list.Archive(2) assert.Equal(true, list.FindById(2).Archived) @@ -56,7 +60,8 @@ func TestUnarchive(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) assert.Equal(true, list.FindById(1).Archived) list.Unarchive(1) assert.Equal(false, list.FindById(1).Archived) @@ -66,7 +71,8 @@ func TestUncomplete(t *testing.T) { assert := assert.New(t) store := &FileStore{FileLocation: "todos.json"} list := &TodoList{} - list.Load(store.Load()) + todos, _ := store.Load() + list.Load(todos) assert.Equal(true, list.FindById(2).Completed) list.Uncomplete(2) assert.Equal(false, list.FindById(2).Completed) -- cgit v1.3