-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (160 loc) · 3.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
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
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/jedib0t/go-pretty/v6/table"
)
type STATUS uint8
const (
DOING STATUS = iota
ONTEST
TODO
DONE
)
const TASK_FILENAME = "tasks.json"
type Task struct {
Name string `json:"label"`
Status STATUS `json:"status"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
DeletedAt int64 `json:"deleted_at"`
}
type Tasks map[uint16]Task
func saveTasks(filename string, t Tasks) {
if len(t) == 0 || filename == "" {
fmt.Println("Filename and tasks are required")
return
}
tasksJSON, _ := json.MarshalIndent(t, "", " ")
os.WriteFile(filename, tasksJSON, 0644)
}
func (t *Tasks) add(name string) {
if name == "" {
fmt.Println("Task name is required")
os.Exit(1)
}
task := Task{
Name: name,
Status: TODO,
CreatedAt: time.Now().Unix(),
UpdatedAt: time.Now().Unix(),
}
(*t)[uint16(len(*t)+1)] = task
saveTasks(TASK_FILENAME, *t)
fmt.Printf("Task \"%s\" added \n", name)
}
func (t *Tasks) remove(id uint16) {
value, exists := (*t)[id]
if exists && value.DeletedAt == 0 {
if value.Status == DOING || value.Status == ONTEST {
fmt.Printf("Task [%d] \"%s\" still on progress \n", id, value.Name)
return
}
value.DeletedAt = time.Now().Unix()
(*t)[id] = value
saveTasks(TASK_FILENAME, *t)
fmt.Printf("Task [%d] \"%s\" removed \n", id, value.Name)
return
}
fmt.Printf("Task [%d] not found\n", id)
}
func getTaskStatus(status STATUS) string {
switch status {
case TODO:
return "TODO"
case DOING:
return "DOING"
case ONTEST:
return "ONTEST"
case DONE:
return "DONE"
default:
return "UNKNOWN"
}
}
func unixToTime(unix int64) string {
return time.Unix(unix, 0).Local().Format("2006-01-02 15:04:05")
}
func (t *Tasks) list(showDeleted bool) {
if (len(*t)) == 0 {
fmt.Println("No tasks")
return
}
taskRows := make([]table.Row, 100)
tableRow := table.Row{"ID", "Task", "Status", "P", "Created At", "Updated At"}
if showDeleted {
tableRow = append(tableRow, "Deleted At")
}
if !showDeleted {
for i, task := range *t {
if !showDeleted && task.DeletedAt != 0 {
continue
}
taskRows = append(taskRows, table.Row{i, task.Name, getTaskStatus(task.Status), task.Status, unixToTime(task.CreatedAt), unixToTime(task.UpdatedAt)})
}
} else {
for i, task := range *t {
var deleteTime string
if task.DeletedAt != 0 {
deleteTime = unixToTime(task.DeletedAt)
}
taskRows = append(taskRows, table.Row{i, task.Name, getTaskStatus(task.Status), task.Status, unixToTime(task.CreatedAt), unixToTime(task.UpdatedAt), deleteTime})
}
}
tab := table.NewWriter()
tab.SetOutputMirror(os.Stdout)
tab.AppendHeader(tableRow)
tab.AppendRows(taskRows)
tab.SortBy([]table.SortBy{
{Name: "P", Mode: table.AscNumeric},
{Name: "ID", Mode: table.AscNumeric},
})
tab.SetColumnConfigs([]table.ColumnConfig{
{Name: "Task", WidthMax: 40},
})
tab.Render()
}
func printHelp() {
fmt.Println("Usage: ")
fmt.Println(" -n <task> \t - add new task")
fmt.Println(" -d <id> \t - delete a task")
fmt.Println(" -l \t\t - show the tasks")
fmt.Println(" -la \t\t - show the tasks with deleted tasks")
fmt.Println(" -h \t\t - help")
os.Exit(0)
}
func main() {
// Create tasks
tasks := make(Tasks)
// Read tasks from file
tasksJSON, err := os.ReadFile(TASK_FILENAME)
if err != nil {
fmt.Println("No tasks")
} else {
json.Unmarshal(tasksJSON, &tasks)
}
if (len(os.Args)) < 2 {
printHelp()
}
switch os.Args[1] {
case "-n":
tasks.add(strings.Join(os.Args[2:], " "))
case "-l":
tasks.list(false)
case "-la":
tasks.list(true)
case "-d":
for i := 2; i < len(os.Args); i++ {
ai, _ := strconv.Atoi(os.Args[i])
tasks.remove(uint16(ai))
}
case "-h":
printHelp()
default:
printHelp()
}
}