feat(delete,update): added functions delete, update and updated list
args to print and take id properly
This commit is contained in:
1 parent
70be91c1ca
commit
07d926caa5
2 files changed
+118
-64
No files matched your search
@@ -27,7 +27,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -109,7 +111,11 @@ func addTask(fileName string, opts []string) (Task, error) {
|
|||||||
return task, err
|
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.CreatedAt = time.Now()
|
||||||
task.UpdatedAt = time.Now()
|
task.UpdatedAt = time.Now()
|
||||||
task.Status = StatusTodo
|
task.Status = StatusTodo
|
||||||
@@ -164,7 +170,6 @@ func listTasks(fileName string, status string, verbose bool) error {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -176,7 +181,51 @@ func mark(fileName string, idx int, status string) error {
|
|||||||
panic(err)
|
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)
|
saveAllTasks(fileName, tasks)
|
||||||
|
|
||||||
@@ -188,8 +237,10 @@ func main() {
|
|||||||
fileName := "tasks.json"
|
fileName := "tasks.json"
|
||||||
var verbose bool
|
var verbose bool
|
||||||
|
|
||||||
if len(args) == 3 && args[2] == "-v" {
|
for i := range args {
|
||||||
verbose = true
|
if args[i] == "-v" {
|
||||||
|
verbose = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if args[0] == "add" {
|
if args[0] == "add" {
|
||||||
@@ -199,34 +250,65 @@ func main() {
|
|||||||
}
|
}
|
||||||
appendToTasks(fileName, task)
|
appendToTasks(fileName, task)
|
||||||
}
|
}
|
||||||
if len(args) > 1 && args[0] == "list" {
|
|
||||||
switch args[1] {
|
if args[0] == "list" {
|
||||||
case "todo":
|
if len(args) > 1 {
|
||||||
err := listTasks(fileName, StatusTodo, verbose)
|
if args[1] == "todo" {
|
||||||
if err != nil {
|
err := listTasks(fileName, StatusTodo, verbose)
|
||||||
panic(err)
|
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":
|
} else {
|
||||||
err := listTasks(fileName, StatusDone, verbose)
|
err := listTasks(fileName, StatusAny, verbose)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
case "in-progress":
|
|
||||||
err := listTasks(fileName, StatusInProgress, verbose)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if args[0] == "list" {
|
} else if args[0] == "mark-in-progress" {
|
||||||
err := listTasks(fileName, StatusAny, verbose)
|
idx, err := strconv.Atoi(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
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])
|
str, err := strconv.Atoi(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
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
@@ -1,58 +1,30 @@
|
|||||||
[
|
[
|
||||||
{
|
|
||||||
"id": 0,
|
|
||||||
"description": "",
|
|
||||||
"status": "",
|
|
||||||
"createdAt": "0001-01-01T00:00:00Z",
|
|
||||||
"updatedAt": "0001-01-01T00:00:00Z"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"description": "create update to progress",
|
"description": "A new task for a finished (poorly coded) project",
|
||||||
"status": "todo",
|
"status": "todo",
|
||||||
"createdAt": "2026-11-06T11:41:53.762079-05:00",
|
"createdAt": "2026-11-11T10:42:52.487162-05:00",
|
||||||
"updatedAt": "2026-11-06T11:41:53.762079-05:00"
|
"updatedAt": "2026-11-11T10:42:52.487162-05:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 2,
|
"id": 2,
|
||||||
"description": "create update to progress 2",
|
"description": "Second task for goated project",
|
||||||
"status": "todo",
|
"status": "in-progress",
|
||||||
"createdAt": "2026-11-06T11:42:10.902711-05:00",
|
"createdAt": "2026-11-11T10:43:08.856044-05:00",
|
||||||
"updatedAt": "2026-11-06T11:42:10.902711-05:00"
|
"updatedAt": "2026-11-11T10:45:33.431582-05:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 3,
|
"id": 3,
|
||||||
"description": "create update to progress 3",
|
"description": "Yet another task",
|
||||||
"status": "todo",
|
"status": "todo",
|
||||||
"createdAt": "2026-11-06T11:43:35.099505-05:00",
|
"createdAt": "2026-11-11T10:43:15.862536-05:00",
|
||||||
"updatedAt": "2026-11-06T11:43:35.099506-05:00"
|
"updatedAt": "2026-11-11T10:43:15.862536-05:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 4,
|
"id": 4,
|
||||||
"description": "hello",
|
"description": "A better task desc",
|
||||||
"status": "todo",
|
"status": "done",
|
||||||
"createdAt": "2026-11-06T12:03:11.263354-05:00",
|
"createdAt": "2026-11-11T10:43:20.436202-05:00",
|
||||||
"updatedAt": "2026-11-06T12:03:11.263354-05:00"
|
"updatedAt": "2026-11-11T10:46:43.800644-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"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
Reference in new issue
Block a user