-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
305 lines (266 loc) · 6.63 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
)
const jsonFile = "tasks.json"
// Task struct represents a task.
type Task struct {
ID int64 `json:"id"`
Description string `json:"description"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TaskData holds task arrays for unmarshaling from JSON.
type TaskData struct {
ID []int64 `json:"id"`
Description []string `json:"description"`
Status []string `json:"status"`
CreatedAt []time.Time `json:"created_at"`
UpdatedAt []time.Time `json:"updated_at"`
}
func add(task string) {
fileTask, err := os.ReadFile(jsonFile)
if err != nil {
if os.IsNotExist(err) {
writeNewData(task)
return
}
fmt.Println("Error:", err)
os.Exit(1)
}
var data TaskData
err = json.Unmarshal(fileTask, &data)
if err != nil {
fmt.Println("Error parsing JSON:", err)
os.Exit(1)
}
lastID := int64(0)
if len(data.ID) > 0 {
lastID = data.ID[len(data.ID)-1]
}
data.Description = append(data.Description, task)
data.Status = append(data.Status, "not done")
data.ID = append(data.ID, lastID+1)
data.CreatedAt = append(data.CreatedAt, time.Now())
data.UpdatedAt = append(data.UpdatedAt, time.Now())
writeToFile(data)
fmt.Printf("Task added (ID: %d)\n", lastID+1)
}
func list() {
fileTask, err := os.ReadFile(jsonFile)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
var data TaskData
err = json.Unmarshal(fileTask, &data)
if err != nil {
fmt.Println("Error parsing JSON:", err)
os.Exit(1)
}
if len(data.ID) != len(data.Description) || len(data.ID) != len(data.Status) {
fmt.Println("Data mismatch.")
os.Exit(1)
}
var tasks []Task
for i := 0; i < len(data.ID); i++ {
tasks = append(tasks, Task{
ID: int64(i + 1),
Description: data.Description[i],
Status: data.Status[i],
CreatedAt: data.CreatedAt[i],
UpdatedAt: data.UpdatedAt[i],
})
}
var filteredTasks []Task
if len(os.Args) < 3 || os.Args[2] == "" {
filteredTasks = tasks
} else {
switch os.Args[2] {
case "done":
for _, task := range tasks {
if task.Status == "done" {
filteredTasks = append(filteredTasks, task)
}
}
case "todo":
for _, task := range tasks {
if task.Status == "not done" {
filteredTasks = append(filteredTasks, task)
}
}
case "in-progress":
for _, task := range tasks {
if task.Status == "in-progress" {
filteredTasks = append(filteredTasks, task)
}
}
default:
fmt.Println("Invalid filter. Use: done, todo, or in-progress.")
os.Exit(1)
}
}
for _, task := range filteredTasks {
status := "✘"
if task.Status == "done" {
status = "✔"
} else if task.Status == "in-progress" {
status = "▶"
}
fmt.Printf(" %d) %-30s [%s]\n", task.ID, task.Description, status)
}
}
func update(index int, task string) {
fileTask, err := os.ReadFile(jsonFile)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
var data TaskData
err = json.Unmarshal(fileTask, &data)
if err != nil {
fmt.Println("Error parsing JSON:", err)
os.Exit(1)
}
if index < 0 || index >= len(data.ID) {
fmt.Println("Invalid index:", index)
return
}
data.Description[index-1] = task
data.Status[index-1] = "not done"
data.UpdatedAt[index-1] = time.Now()
writeToFile(data)
fmt.Printf("Task updated (Index: %d)\n", index)
}
func updateStatus(index int) {
fileTask, err := os.ReadFile(jsonFile)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
var data TaskData
err = json.Unmarshal(fileTask, &data)
if err != nil {
fmt.Println("Error parsing JSON:", err)
os.Exit(1)
}
if index-1 < 0 || index-1 >= len(data.ID) {
fmt.Println("Invalid index:", index)
return
}
switch os.Args[1] {
case "mark-done":
data.Status[index-1] = "done"
case "mark-in-progress":
data.Status[index-1] = "in-progress"
}
data.UpdatedAt[index-1] = time.Now()
writeToFile(data)
fmt.Printf("Task updated (Index: %d)\n", index)
}
func delete(index int) {
// Adjust for 1-based indexing if necessary
zeroBasedIndex := index - 1
// Read file data
fileTask, err := os.ReadFile(jsonFile)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
// Unmarshal file data into a struct
var data TaskData
err = json.Unmarshal(fileTask, &data)
if err != nil {
fmt.Println("Error parsing JSON:", err)
os.Exit(1)
}
// Check if the adjusted index is valid (0-based index)
if zeroBasedIndex < 0 || zeroBasedIndex >= len(data.ID) {
fmt.Println("Invalid index:", index)
return
}
// Perform deletion based on the adjusted index
data.ID = append(data.ID[:zeroBasedIndex], data.ID[zeroBasedIndex+1:]...)
data.Description = append(data.Description[:zeroBasedIndex], data.Description[zeroBasedIndex+1:]...)
data.Status = append(data.Status[:zeroBasedIndex], data.Status[zeroBasedIndex+1:]...)
data.CreatedAt = append(data.CreatedAt[:zeroBasedIndex], data.CreatedAt[zeroBasedIndex+1:]...)
data.UpdatedAt = append(data.UpdatedAt[:zeroBasedIndex], data.UpdatedAt[zeroBasedIndex+1:]...)
// Write the updated data back to the file
writeToFile(data)
// Print confirmation message
fmt.Printf("Task deleted (Index: %d)\n", index)
}
func writeNewData(task string) {
data := TaskData{
ID: []int64{1},
Description: []string{task},
Status: []string{"not done"},
CreatedAt: []time.Time{time.Now()},
UpdatedAt: []time.Time{time.Now()},
}
writeToFile(data)
}
func writeToFile(data TaskData) {
updatedData, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("Error marshalling data:", err)
os.Exit(1)
}
err = os.WriteFile(jsonFile, updatedData, 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
os.Exit(1)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide a command.")
os.Exit(1)
}
command := os.Args[1]
switch command {
case "add":
task := strings.Join(os.Args[2:], " ")
add(task)
case "list":
list()
case "update":
index, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
task := strings.Join(os.Args[3:], " ")
update(index, task)
case "delete":
index, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
delete(index)
case "mark-done":
index, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
updateStatus(index)
case "mark-in-progress":
index, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
updateStatus(index)
default:
fmt.Println("Unknown command:", command)
os.Exit(1)
}
}