-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
97 lines (82 loc) · 2.51 KB
/
main.go
File metadata and controls
97 lines (82 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"errors"
"fmt"
"os"
"strconv"
"task-cli/repository"
"task-cli/service"
"task-cli/util"
"task-cli/view"
)
func main() {
// Get the command arguments without the command line name
argumentsWithoutProg := os.Args[1:]
// Instanciating the Task Service to handle the tasks actions
taskService := service.NewTaskService(
// Instanciating the Task Repository to read/write the "task.json" file
repository.NewTaskRepository("./db/tasks.json"),
)
// Validate if the command was called with arguments, otherwise throw an error and exit
if len(argumentsWithoutProg) == 0 {
util.LogError(errors.New("you must provide an option"))
}
// Get the first argument and check if is valid
switch argument := argumentsWithoutProg[0]; argument {
case "add":
if len(argumentsWithoutProg) == 1 {
util.LogError(errors.New("description not provided"))
}
newTaskID, err := taskService.AddTask(argumentsWithoutProg[1])
util.LogError(err)
fmt.Printf("Task added successfully (ID: %d)\n", newTaskID)
case "update":
if len(argumentsWithoutProg) == 1 {
util.LogError(errors.New("task id not provided"))
}
taskID, err := strconv.Atoi(argumentsWithoutProg[1])
util.LogError(err)
description := argumentsWithoutProg[2]
if len(description) == 0 {
util.LogError(errors.New("description not provided"))
}
_, err = taskService.UpdateTaskDescription(taskID, description)
util.LogError(err)
case "delete":
if len(argumentsWithoutProg) == 1 {
util.LogError(errors.New("task id not provided"))
}
taskID, err := strconv.Atoi(argumentsWithoutProg[1])
util.LogError(err)
taskService.DeleteBy(taskID)
case "mark-in-progress":
if len(argumentsWithoutProg) == 1 {
util.LogError(errors.New("task id not provided"))
}
taskID, err := strconv.Atoi(argumentsWithoutProg[1])
util.LogError(err)
_, err = taskService.UpdateTaskStatus(taskID, "in-progress")
util.LogError(err)
case "mark-done":
if len(argumentsWithoutProg) == 1 {
util.LogError(errors.New("task id not provided"))
}
taskID, err := strconv.Atoi(argumentsWithoutProg[1])
util.LogError(err)
_, err = taskService.UpdateTaskStatus(taskID, "done")
util.LogError(err)
case "list":
if len(argumentsWithoutProg) > 1 {
tasks, err := taskService.GetByStatus(argumentsWithoutProg[1])
util.LogError(err)
view.PromptTableTasks(tasks)
return
}
tasks, err := taskService.GetAll()
util.LogError(err)
view.PromptTableTasks(tasks)
return
default:
util.LogError(errors.New("option provided not valid"))
}
}