-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (66 loc) · 1.76 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"time"
)
type Todo struct {
Name string
Completed bool
CreatedAt time.Time
CompletedAt time.Time
}
func createTodo(todoName string, todos *[]Todo) {
todo := Todo{
Name: todoName,
Completed: false,
CreatedAt: time.Now(),
CompletedAt: time.Time{},
}
*todos = append(*todos, todo)
}
func deleteTodo(todoIndex int, todos *[]Todo) {
*todos = append((*todos)[:todoIndex], (*todos)[todoIndex+1:]...)
}
func completeTodo(todoIndex int, todos *[]Todo) {
(*todos)[todoIndex].Completed = true
(*todos)[todoIndex].CompletedAt = time.Now()
}
func listTodos(todos *[]Todo) {
for index, todo := range *todos {
fmt.Printf("Task id: %d - Name: %s - Completed: %t - Created at: %d/%d/%d - Completed at: %d/%d/%d \n", index, todo.Name, todo.Completed, todo.CreatedAt.Year(), todo.CreatedAt.Month(), todo.CreatedAt.Day(), todo.CreatedAt.Year(), todo.CreatedAt.Month(), todo.CreatedAt.Day())
}
}
func main() {
add := flag.String("a", "", "Add a todo")
delete := flag.Int("d", -1, "Delete a todo")
complete := flag.Int("c", -1, "Complete a todo")
list := flag.Bool("l", false, "List all todos")
flag.Parse()
var todos []Todo
data, err := os.ReadFile("todos.json")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
os.Create("todos.json")
}
}
json.Unmarshal(data, &todos)
switch {
case len(*add) > 0:
createTodo(*add, &todos)
fmt.Println("Task", *add, "created!")
case *delete > -1:
deleteTodo(*delete, &todos)
fmt.Println("Task", *delete, "deleted!")
case *complete > -1:
completeTodo(*complete, &todos)
fmt.Println("Task", *complete, "completed!")
case *list:
listTodos(&todos)
}
data, _ = json.Marshal(todos)
os.WriteFile("todos.json", data, 0755)
}