/
list.go
52 lines (43 loc) · 927 Bytes
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package todo
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"strings"
"github.com/gonuts/commander"
"github.com/olekukonko/tablewriter"
)
func todoList(tasksFile string) *commander.Command {
list := func(cmd *commander.Command, args []string) error {
file, err := ioutil.ReadFile(tasksFile)
if err != nil {
panic(err)
}
dec := json.NewDecoder(strings.NewReader(string(file)))
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Task", "Date", "Status"})
for {
var t Task
if err := dec.Decode(&t); err == io.EOF {
break
} else if err != nil {
panic(err)
}
data := []string{t.ID, t.Todo, t.Date.Format("2006-01-12"), t.Status}
table.Append(data)
}
table.Render()
return nil
}
return &commander.Command{
Run: list,
UsageLine: "list",
Short: "Show the task list",
Long: `
Show the task list.
ex:
$ ./go-todo list
`,
}
}