chore: splitting monolith into packages

This commit is contained in:
2026-04-02 17:22:55 +02:00
parent 3d61c78c35
commit be1dd05d9b
9 changed files with 171 additions and 116 deletions

7
utils/err_checker.go Normal file
View File

@@ -0,0 +1,7 @@
package utils
func Check(e error) {
if e != nil {
panic(e)
}
}

32
utils/read_backlog.go Normal file
View File

@@ -0,0 +1,32 @@
package utils
import (
"encoding/json"
"errors"
"gotask-cli/models"
"os"
)
func InitBacklog(path string) []models.Task {
if _, err := os.Stat(path); err == nil {
} else if errors.Is(err, os.ErrNotExist) {
f, err := os.Create(path)
Check(err)
defer f.Close()
} else {
Check(err)
os.Exit(1)
}
return loadBacklog(path)
}
func loadBacklog(path string) []models.Task {
var backlog []models.Task
content, err := os.ReadFile(path)
Check(err)
err = json.Unmarshal([]byte(content), &backlog)
Check(err)
return backlog
}

31
utils/table_render.go Normal file
View File

@@ -0,0 +1,31 @@
package utils
import (
"gotask-cli/models"
"os"
"time"
"github.com/jedib0t/go-pretty/v6/table"
)
func PrintTasksAsATable(tasks []models.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,
models.StatusName[v.Status],
time.Unix(v.Created, 0).Format(time.DateTime),
updated,
})
}
t.Render()
}

29
utils/write_backlog.go Normal file
View File

@@ -0,0 +1,29 @@
package utils
import (
"encoding/json"
"errors"
"os"
"gotask-cli/models"
)
const tempPath string = "./tasks.save"
func SaveBacklog(backlog []models.Task, path string) {
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)
}