From 82cab97ea019adeceb1f5844b7268dcbb1f55b0e Mon Sep 17 00:00:00 2001 From: Louison SARLIN--MAGNUS Date: Thu, 2 Apr 2026 14:34:37 +0200 Subject: [PATCH] feat: atomicallyly saving updated backlog --- gotask-cli.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/gotask-cli.go b/gotask-cli.go index bcee886..59b4049 100644 --- a/gotask-cli.go +++ b/gotask-cli.go @@ -19,6 +19,7 @@ const ( ) const path string = "./tasks.json" +const tempPath string = "./tasks.save" var statusName = map[Status]string{ todo: "todo", @@ -89,16 +90,33 @@ func printTasksAsATable(tasks []task) { } func addTask(desc string) { - newtask := task{ - Id: 1, + Id: uint(len(backlog)) + 1, Description: desc, Status: 0, Created: time.Now().Unix(), Updated: -1} fmt.Printf("New task \"%s\" added to do @ id=%d", newtask.Description, newtask.Id) + backlog = append(backlog, newtask) +} +func saveBacklog() { + if _, err := os.Stat(tempPath); err == nil { + err = os.Remove(tempPath) + } else if errors.Is(err, os.ErrNotExist) { + } else { + check(err) + os.Exit(1) + } + f, err := os.Create(tempPath) + check(err) + defer f.Close() + backlogJSON, _ := json.MarshalIndent(backlog, "", "\t") + f.Write(backlogJSON) + + err = os.Rename(tempPath, path) + check(err) } func main() { @@ -113,13 +131,12 @@ func main() { } else { fmt.Println("Missing argument") } - case "list": printTasksAsATable(backlog) default: fmt.Println("NO !") } - // saveBacklog() + saveBacklog() } else { fmt.Println("Missing argument") }