chore: splitting monolith into packages
This commit is contained in:
7
utils/err_checker.go
Normal file
7
utils/err_checker.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package utils
|
||||
|
||||
func Check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
32
utils/read_backlog.go
Normal file
32
utils/read_backlog.go
Normal 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
31
utils/table_render.go
Normal 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
29
utils/write_backlog.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user