chore: splitting monolith into packages
This commit is contained in:
20
commands/add_task.go
Normal file
20
commands/add_task.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gotask-cli/models"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddTask(desc string, backlog []models.Task) []models.Task {
|
||||||
|
newtask := models.Task{
|
||||||
|
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)
|
||||||
|
return backlog
|
||||||
|
}
|
||||||
9
commands/help.go
Normal file
9
commands/help.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Help() {
|
||||||
|
fmt.Println("Hang in there, will do the help stuff some day !")
|
||||||
|
}
|
||||||
10
commands/list_tasks.go
Normal file
10
commands/list_tasks.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gotask-cli/models"
|
||||||
|
"gotask-cli/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ListTasks(backlog []models.Task) {
|
||||||
|
utils.PrintTasksAsATable(backlog)
|
||||||
|
}
|
||||||
126
gotask-cli.go
126
gotask-cli.go
@@ -1,143 +1,37 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/jedib0t/go-pretty/v6/table"
|
"gotask-cli/commands"
|
||||||
)
|
"gotask-cli/utils"
|
||||||
|
|
||||||
type Status int
|
|
||||||
|
|
||||||
const (
|
|
||||||
todo Status = iota
|
|
||||||
doing
|
|
||||||
done
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const path string = "./tasks.json"
|
const path string = "./tasks.json"
|
||||||
const tempPath string = "./tasks.save"
|
|
||||||
|
|
||||||
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: 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() {
|
func main() {
|
||||||
initBacklog()
|
backlog := utils.InitBacklog(path)
|
||||||
if len(os.Args[1:]) >= 1 {
|
if len(os.Args[1:]) >= 1 {
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "help":
|
case "help":
|
||||||
fmt.Println("S.O.S!")
|
commands.Help()
|
||||||
case "add":
|
case "add":
|
||||||
if len(os.Args[1:]) >= 2 {
|
if len(os.Args[1:]) >= 2 {
|
||||||
addTask(os.Args[2])
|
backlog = commands.AddTask(os.Args[2], backlog)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Missing argument")
|
fmt.Println("Missing argument")
|
||||||
|
commands.Help()
|
||||||
}
|
}
|
||||||
case "list":
|
case "list":
|
||||||
printTasksAsATable(backlog)
|
commands.ListTasks(backlog)
|
||||||
default:
|
default:
|
||||||
fmt.Println("NO !")
|
fmt.Println("NO !")
|
||||||
}
|
}
|
||||||
saveBacklog()
|
utils.SaveBacklog(backlog, path)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Missing argument")
|
fmt.Println("Missing argument")
|
||||||
|
commands.Help()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
models/task.go
Normal file
23
models/task.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type status int
|
||||||
|
|
||||||
|
const (
|
||||||
|
todo status = iota
|
||||||
|
doing
|
||||||
|
done
|
||||||
|
)
|
||||||
|
|
||||||
|
var StatusName = map[status]string{
|
||||||
|
todo: "todo",
|
||||||
|
doing: "doing",
|
||||||
|
done: "done",
|
||||||
|
}
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
Id uint
|
||||||
|
Description string
|
||||||
|
Status status
|
||||||
|
Created int64
|
||||||
|
Updated int64
|
||||||
|
}
|
||||||
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