-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.go
177 lines (155 loc) · 3.29 KB
/
t.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
)
type Task struct {
description string
}
type TaskList struct {
tasks []*Task
}
func (t *TaskList) Add(taskDescription string) {
if t.tasks == nil {
t.tasks = make([]*Task, 0)
}
task := Task{description: taskDescription}
t.tasks = append(t.tasks, &task)
}
func (t *TaskList) List() []string {
tasks := make([]*Task, 0)
for _, t := range t.tasks {
tasks = append(tasks, t)
}
list := make([]string, 0)
for i, task := range t.tasks {
list = append(list, fmt.Sprintf("%d - %s", i, task.description))
}
return list
}
func (t *TaskList) Finish(taskId int) error {
if t.tasks == nil {
return errors.New("No tasks found")
}
if len(t.tasks) <= taskId {
return errors.New("No task for id found")
}
newTasks := make([]*Task, 0)
for i, task := range t.tasks {
if i != taskId {
newTasks = append(newTasks, task)
}
}
t.tasks = newTasks
return nil
}
func (t *TaskList) Edit(taskId int, newDescription string) error {
if t.tasks == nil {
return errors.New("No tasks found")
}
if len(t.tasks) <= taskId {
return errors.New("No task for id found")
}
t.tasks[taskId].description = newDescription
return nil
}
func (t *TaskList) MarshalText() ([]byte, error) {
list := make([]string, 0)
for _, task := range t.tasks {
list = append(list, task.description)
}
return []byte(strings.Join(list, "\n")), nil
}
func (t *TaskList) UnmarshalText(text []byte) error {
in := string(text)
list := strings.Split(in, "\n")
t.tasks = make([]*Task, 0)
for _, taskDescription := range list {
if taskDescription != "" {
task := Task{description: taskDescription}
t.tasks = append(t.tasks, &task)
}
}
return nil
}
var tasklist *TaskList
var taskFilePath string
var helpText string = `t manages tasks in your command line
Usage:
List all tasks:
t
Add task:
t "Buy milk"
Edit a given task:
t -e 0 "Buy two milk bottles"
Finish a task:
t -f 0
`
func usage() {
fmt.Printf("%s", helpText)
}
func main() {
flag.Usage = usage
var (
editTask = flag.Int("e", -1, "edit the tasklist")
finishTask = flag.Int("f", -1, "finish task #")
)
flag.Parse()
tasklist = &(TaskList{})
taskFilePath = getTaskFilePath()
file, err := os.Open(taskFilePath)
if err != nil {
if !os.IsNotExist(err) {
fmt.Print(err)
}
}
defer file.Close()
if file != nil {
taskBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Print(err.Error())
}
err = tasklist.UnmarshalText(taskBytes)
if err != nil {
fmt.Print(err.Error())
}
}
text := strings.Join(flag.Args(), " ")
if *editTask != -1 {
tasklist.Edit(*editTask, text)
tasklist.write(true)
} else if *finishTask != -1 {
tasklist.Finish(*finishTask)
tasklist.write(true)
} else {
if len(flag.Args()) > 0 {
tasklist.Add(text)
tasklist.write(true)
} else {
for _, task := range tasklist.List() {
fmt.Println(task)
}
}
}
}
func (t *TaskList) write(deleteIfEmpty bool) error {
marshaledList, _ := tasklist.MarshalText()
err := ioutil.WriteFile(taskFilePath, marshaledList, 0644)
if err != nil {
return err
}
return nil
}
func getTaskFilePath() string {
tasksFilePath := os.Getenv("T_TASKS_FILE")
if tasksFilePath == "" {
user, _ := user.Current()
tasksFilePath = user.HomeDir + "/tasks"
}
return tasksFilePath
}