From be1dd05d9b927abad5be486338a9cb25b85c8e59 Mon Sep 17 00:00:00 2001 From: Louison SARLIN--MAGNUS Date: Thu, 2 Apr 2026 17:22:55 +0200 Subject: [PATCH] chore: splitting monolith into packages --- commands/add_task.go | 20 +++++++ commands/help.go | 9 +++ commands/list_tasks.go | 10 ++++ gotask-cli.go | 126 ++++------------------------------------- models/task.go | 23 ++++++++ utils/err_checker.go | 7 +++ utils/read_backlog.go | 32 +++++++++++ utils/table_render.go | 31 ++++++++++ utils/write_backlog.go | 29 ++++++++++ 9 files changed, 171 insertions(+), 116 deletions(-) create mode 100644 commands/add_task.go create mode 100644 commands/help.go create mode 100644 commands/list_tasks.go create mode 100644 models/task.go create mode 100644 utils/err_checker.go create mode 100644 utils/read_backlog.go create mode 100644 utils/table_render.go create mode 100644 utils/write_backlog.go diff --git a/commands/add_task.go b/commands/add_task.go new file mode 100644 index 0000000..0ad42a7 --- /dev/null +++ b/commands/add_task.go @@ -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 +} diff --git a/commands/help.go b/commands/help.go new file mode 100644 index 0000000..1be19ec --- /dev/null +++ b/commands/help.go @@ -0,0 +1,9 @@ +package commands + +import ( + "fmt" +) + +func Help() { + fmt.Println("Hang in there, will do the help stuff some day !") +} diff --git a/commands/list_tasks.go b/commands/list_tasks.go new file mode 100644 index 0000000..66f3d13 --- /dev/null +++ b/commands/list_tasks.go @@ -0,0 +1,10 @@ +package commands + +import ( + "gotask-cli/models" + "gotask-cli/utils" +) + +func ListTasks(backlog []models.Task) { + utils.PrintTasksAsATable(backlog) +} diff --git a/gotask-cli.go b/gotask-cli.go index 59b4049..a4219e4 100644 --- a/gotask-cli.go +++ b/gotask-cli.go @@ -1,143 +1,37 @@ 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 + "gotask-cli/commands" + "gotask-cli/utils" ) 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() { - initBacklog() + backlog := utils.InitBacklog(path) if len(os.Args[1:]) >= 1 { switch os.Args[1] { case "help": - fmt.Println("S.O.S!") + commands.Help() case "add": if len(os.Args[1:]) >= 2 { - addTask(os.Args[2]) + backlog = commands.AddTask(os.Args[2], backlog) } else { fmt.Println("Missing argument") + commands.Help() } case "list": - printTasksAsATable(backlog) + commands.ListTasks(backlog) default: fmt.Println("NO !") } - saveBacklog() + utils.SaveBacklog(backlog, path) } else { fmt.Println("Missing argument") + commands.Help() + } } diff --git a/models/task.go b/models/task.go new file mode 100644 index 0000000..0e33e4a --- /dev/null +++ b/models/task.go @@ -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 +} diff --git a/utils/err_checker.go b/utils/err_checker.go new file mode 100644 index 0000000..60e274d --- /dev/null +++ b/utils/err_checker.go @@ -0,0 +1,7 @@ +package utils + +func Check(e error) { + if e != nil { + panic(e) + } +} diff --git a/utils/read_backlog.go b/utils/read_backlog.go new file mode 100644 index 0000000..f7c56cb --- /dev/null +++ b/utils/read_backlog.go @@ -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 +} diff --git a/utils/table_render.go b/utils/table_render.go new file mode 100644 index 0000000..a1c956c --- /dev/null +++ b/utils/table_render.go @@ -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() +} diff --git a/utils/write_backlog.go b/utils/write_backlog.go new file mode 100644 index 0000000..51627e6 --- /dev/null +++ b/utils/write_backlog.go @@ -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) +}