feat: atomicallyly saving updated backlog
This commit is contained in:
@@ -19,6 +19,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const path string = "./tasks.json"
|
const path string = "./tasks.json"
|
||||||
|
const tempPath string = "./tasks.save"
|
||||||
|
|
||||||
var statusName = map[Status]string{
|
var statusName = map[Status]string{
|
||||||
todo: "todo",
|
todo: "todo",
|
||||||
@@ -89,16 +90,33 @@ func printTasksAsATable(tasks []task) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addTask(desc string) {
|
func addTask(desc string) {
|
||||||
|
|
||||||
newtask := task{
|
newtask := task{
|
||||||
Id: 1,
|
Id: uint(len(backlog)) + 1,
|
||||||
Description: desc,
|
Description: desc,
|
||||||
Status: 0,
|
Status: 0,
|
||||||
Created: time.Now().Unix(),
|
Created: time.Now().Unix(),
|
||||||
Updated: -1}
|
Updated: -1}
|
||||||
|
|
||||||
fmt.Printf("New task \"%s\" added to do @ id=%d", newtask.Description, newtask.Id)
|
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() {
|
func main() {
|
||||||
@@ -113,13 +131,12 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Println("Missing argument")
|
fmt.Println("Missing argument")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "list":
|
case "list":
|
||||||
printTasksAsATable(backlog)
|
printTasksAsATable(backlog)
|
||||||
default:
|
default:
|
||||||
fmt.Println("NO !")
|
fmt.Println("NO !")
|
||||||
}
|
}
|
||||||
// saveBacklog()
|
saveBacklog()
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Missing argument")
|
fmt.Println("Missing argument")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user