diff options
| author | Grant Ammons <grant@pipelinedealsco.com> | 2016-09-19 05:27:44 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-09-19 05:27:44 -0400 |
| commit | c3c8817de6228557ba865465e3d639a104578587 (patch) | |
| tree | b5e004011606f1cc44eba196bff8f1bd33e795b7 | |
| parent | aa0580af8066b030c720a20b5e79e98a53d1ec11 (diff) | |
| parent | 144ac3243b4920d267f224732d9deff0f9f2cc52 (diff) | |
Merge pull request #9 from Bunchhieng/master
Only allow to open web app if .todos.json exists in current directory
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | .todos.json | 2 | ||||
| -rw-r--r-- | todo.go | 12 | ||||
| -rw-r--r-- | todolist/app.go | 9 | ||||
| -rw-r--r-- | todolist/file_store.go | 6 | ||||
| -rw-r--r-- | todolist/file_store_test.go | 4 | ||||
| -rw-r--r-- | todolist/filter_test.go | 12 | ||||
| -rw-r--r-- | todolist/grouper_test.go | 6 | ||||
| -rw-r--r-- | todolist/todo_list_test.go | 18 |
9 files changed, 49 insertions, 23 deletions
@@ -1,2 +1,5 @@ .goxc.local.json dist + +# OSX metadata +.DS_Store 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}] @@ -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() { 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) |
