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])
}
}