feat: global structure for the project

This commit is contained in:
2026-04-01 18:06:27 +02:00
parent 534fba2235
commit c3ac141c33
3 changed files with 156 additions and 0 deletions

126
gotask-cli.go Normal file
View File

@@ -0,0 +1,126 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/jedib0t/go-pretty/v6/table"
)
type Status int
const (
todo Status = iota
doing
done
)
const path string = "./tasks.json"
var statusName = map[Status]string{
todo: "todo",
doing: "doing",
done: "done",
}
type task struct {
Id uint
Description string
Status Status
Created int64
Updated int64
}
var backlog []task
func initBacklog() {
if _, err := os.Stat(path); err == nil {
loadBacklog()
} else if errors.Is(err, os.ErrNotExist) {
// path does not exist
f, err := os.Create(path)
check(err)
defer f.Close()
loadBacklog()
} else {
check(err)
os.Exit(1)
}
}
func loadBacklog() {
content, err := os.ReadFile(path)
check(err)
err = json.Unmarshal([]byte(content), &backlog)
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func printTasksAsATable(tasks []task) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Id", "Description", "Status", "Created", "Updated"})
for _, v := range tasks {
var updated string
if v.Updated == -1 {
updated = ""
} else {
updated = time.Unix(v.Updated, 0).Format(time.DateTime)
}
t.AppendRow([]interface{}{
v.Id,
v.Description,
statusName[v.Status],
time.Unix(v.Created, 0).Format(time.DateTime),
updated,
})
}
t.Render()
}
func addTask(desc string) {
newtask := task{
Id: 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)
}
func main() {
initBacklog()
if len(os.Args[1:]) >= 1 {
switch os.Args[1] {
case "help":
fmt.Println("S.O.S!")
case "add":
if len(os.Args[1:]) >= 2 {
addTask(os.Args[2])
} else {
fmt.Println("Missing argument")
}
case "list":
printTasksAsATable(backlog)
default:
fmt.Println("NO !")
}
// saveBacklog()
} else {
fmt.Println("Missing argument")
}
}