feat: global structure for the project
This commit is contained in:
12
go.mod
Normal file
12
go.mod
Normal file
@@ -0,0 +1,12 @@
|
||||
module gotask-cli
|
||||
|
||||
go 1.25.7
|
||||
|
||||
require github.com/jedib0t/go-pretty/v6 v6.7.8
|
||||
|
||||
require (
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.21 // indirect
|
||||
golang.org/x/sys v0.42.0 // indirect
|
||||
golang.org/x/text v0.35.0 // indirect
|
||||
)
|
||||
18
go.sum
Normal file
18
go.sum
Normal file
@@ -0,0 +1,18 @@
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/jedib0t/go-pretty/v6 v6.7.8 h1:BVYrDy5DPBA3Qn9ICT+PokP9cvCv1KaHv2i+Hc8sr5o=
|
||||
github.com/jedib0t/go-pretty/v6 v6.7.8/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
|
||||
github.com/mattn/go-runewidth v0.0.21 h1:jJKAZiQH+2mIinzCJIaIG9Be1+0NR+5sz/lYEEjdM8w=
|
||||
github.com/mattn/go-runewidth v0.0.21/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
|
||||
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
126
gotask-cli.go
Normal file
126
gotask-cli.go
Normal 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user