feat(delete,update): added functions delete, update and updated list

args to print and take id properly
This commit is contained in:
DarthMooMancer committed 2026-11-11 10:48:27 -05:00
1 parent 70be91c1ca
commit 07d926caa5
2 files changed
+118 -64

No files matched your search

+104 -22
View File
@@ -27,7 +27,9 @@ import (
"errors"
"fmt"
"os"
"slices"
"strconv"
"strings"
"time"
)
@@ -109,7 +111,11 @@ func addTask(fileName string, opts []string) (Task, error) {
return task, err
}
task.ID = tasks[len(tasks)-1].ID + 1
if len(tasks) == 0 {
task.ID = 1
} else {
task.ID = tasks[len(tasks)-1].ID + 1
}
task.CreatedAt = time.Now()
task.UpdatedAt = time.Now()
task.Status = StatusTodo
@@ -164,7 +170,6 @@ func listTasks(fileName string, status string, verbose bool) error {
}
continue
}
}
return nil
@@ -176,7 +181,51 @@ func mark(fileName string, idx int, status string) error {
panic(err)
}
tasks[idx].Status = status
i := slices.IndexFunc(tasks, func(t Task) bool {
return t.ID == idx
})
if i != -1 {
tasks[i].Status = status
tasks[i].UpdatedAt = time.Now()
}
saveAllTasks(fileName, tasks)
return nil
}
func delete(fileName string, idx int) error {
tasks, err := loadTasks(fileName)
if err != nil {
panic(err)
}
i := slices.IndexFunc(tasks, func(t Task) bool {
return t.ID == idx
})
if i != -1 {
tasks = slices.Delete(tasks, i, i+1)
}
saveAllTasks(fileName, tasks)
return nil
}
func update(fileName string, desc string, idx int) error {
tasks, err := loadTasks(fileName)
if err != nil {
panic(err)
}
i := slices.IndexFunc(tasks, func(t Task) bool {
return t.ID == idx
})
if i != -1 {
tasks[i].Desc = desc
}
saveAllTasks(fileName, tasks)
@@ -188,8 +237,10 @@ func main() {
fileName := "tasks.json"
var verbose bool
if len(args) == 3 && args[2] == "-v" {
verbose = true
for i := range args {
if args[i] == "-v" {
verbose = true
}
}
if args[0] == "add" {
@@ -199,34 +250,65 @@ func main() {
}
appendToTasks(fileName, task)
}
if len(args) > 1 && args[0] == "list" {
switch args[1] {
case "todo":
err := listTasks(fileName, StatusTodo, verbose)
if err != nil {
panic(err)
if args[0] == "list" {
if len(args) > 1 {
if args[1] == "todo" {
err := listTasks(fileName, StatusTodo, verbose)
if err != nil {
panic(err)
}
} else if args[1] == "done" {
err := listTasks(fileName, StatusDone, verbose)
if err != nil {
panic(err)
}
} else if args[1] == "in-progress" {
err := listTasks(fileName, StatusInProgress, verbose)
if err != nil {
panic(err)
}
} else {
err := listTasks(fileName, StatusAny, verbose)
if err != nil {
panic(err)
}
}
case "done":
err := listTasks(fileName, StatusDone, verbose)
if err != nil {
panic(err)
}
case "in-progress":
err := listTasks(fileName, StatusInProgress, verbose)
} else {
err := listTasks(fileName, StatusAny, verbose)
if err != nil {
panic(err)
}
}
} else if args[0] == "list" {
err := listTasks(fileName, StatusAny, verbose)
} else if args[0] == "mark-in-progress" {
idx, err := strconv.Atoi(args[1])
if err != nil {
panic(err)
}
} else if args[0] == "mark-progress" {
err = mark(fileName, idx, StatusInProgress)
} else if args[0] == "mark-done" {
str, err := strconv.Atoi(args[1])
if err != nil {
panic(err)
}
err = mark(fileName, str, StatusInProgress)
err = mark(fileName, str, StatusDone)
} else if args[0] == "delete" {
idx, err := strconv.Atoi(args[1])
if err != nil {
panic(err)
}
err = delete(fileName, idx)
} else if args[0] == "update" {
idx, err := strconv.Atoi(args[1])
if err != nil {
panic(err)
}
err = update(fileName, strings.Join(args[2:], " "), idx)
if err != nil {
panic(err)
}
} else {
fmt.Printf("Invalid arguments: %s", args[0])
}
}
+14 -42
View File
@@ -1,58 +1,30 @@
[
{
"id": 0,
"description": "",
"status": "",
"createdAt": "0001-01-01T00:00:00Z",
"updatedAt": "0001-01-01T00:00:00Z"
},
{
"id": 1,
"description": "create update to progress",
"description": "A new task for a finished (poorly coded) project",
"status": "todo",
"createdAt": "2026-11-06T11:41:53.762079-05:00",
"updatedAt": "2026-11-06T11:41:53.762079-05:00"
"createdAt": "2026-11-11T10:42:52.487162-05:00",
"updatedAt": "2026-11-11T10:42:52.487162-05:00"
},
{
"id": 2,
"description": "create update to progress 2",
"status": "todo",
"createdAt": "2026-11-06T11:42:10.902711-05:00",
"updatedAt": "2026-11-06T11:42:10.902711-05:00"
"description": "Second task for goated project",
"status": "in-progress",
"createdAt": "2026-11-11T10:43:08.856044-05:00",
"updatedAt": "2026-11-11T10:45:33.431582-05:00"
},
{
"id": 3,
"description": "create update to progress 3",
"description": "Yet another task",
"status": "todo",
"createdAt": "2026-11-06T11:43:35.099505-05:00",
"updatedAt": "2026-11-06T11:43:35.099506-05:00"
"createdAt": "2026-11-11T10:43:15.862536-05:00",
"updatedAt": "2026-11-11T10:43:15.862536-05:00"
},
{
"id": 4,
"description": "hello",
"status": "todo",
"createdAt": "2026-11-06T12:03:11.263354-05:00",
"updatedAt": "2026-11-06T12:03:11.263354-05:00"
},
{
"id": 5,
"description": "test",
"status": "todo",
"createdAt": "2026-11-09T12:54:10.885221-05:00",
"updatedAt": "2026-11-09T12:54:10.885221-05:00"
},
{
"id": 6,
"description": "test_2",
"status": "in-progress",
"createdAt": "2026-11-09T13:25:02.880597-05:00",
"updatedAt": "2026-11-09T13:25:02.880597-05:00"
},
{
"id": 7,
"description": "test_3",
"status": "todo",
"createdAt": "2026-11-09T13:25:08.375329-05:00",
"updatedAt": "2026-11-09T13:25:08.37533-05:00"
"description": "A better task desc",
"status": "done",
"createdAt": "2026-11-11T10:43:20.436202-05:00",
"updatedAt": "2026-11-11T10:46:43.800644-05:00"
}
]