-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.go
242 lines (228 loc) · 5.07 KB
/
cmds.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"strconv"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/kancli"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "tasks",
Short: "A CLI task management tool.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
var addCmd = &cobra.Command{
Use: "add NAME",
Short: "Add a new task with an optional project name",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
t, err := openDB(setupPath())
if err != nil {
return err
}
defer t.db.Close()
project, err := cmd.Flags().GetString("project")
if err != nil {
return err
}
if err := t.insert(args[0], project); err != nil {
return err
}
return nil
},
}
var whereCmd = &cobra.Command{
Use: "where",
Short: "Show where your tasks are stored",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
_, err := fmt.Println(setupPath())
return err
},
}
var deleteCmd = &cobra.Command{
Use: "delete ID",
Short: "Delete a task by ID",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
t, err := openDB(setupPath())
if err != nil {
return err
}
defer t.db.Close()
id, err := strconv.Atoi(args[0])
if err != nil {
return err
}
return t.delete(uint(id))
},
}
var updateCmd = &cobra.Command{
Use: "update ID",
Short: "Update a task by ID",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
t, err := openDB(setupPath())
if err != nil {
return err
}
defer t.db.Close()
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
project, err := cmd.Flags().GetString("project")
if err != nil {
return err
}
prog, err := cmd.Flags().GetInt("status")
if err != nil {
return err
}
id, err := strconv.Atoi(args[0])
if err != nil {
return err
}
var status string
switch prog {
case int(inProgress):
status = inProgress.String()
case int(done):
status = done.String()
default:
status = todo.String()
}
newTask := task{uint(id), name, project, status, time.Time{}}
return t.update(newTask)
},
}
var listCmd = &cobra.Command{
Use: "list",
Short: "List all your tasks",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
t, err := openDB(setupPath())
if err != nil {
return err
}
defer t.db.Close()
tasks, err := t.getTasks()
if err != nil {
return err
}
fmt.Print(setupTable(tasks))
return nil
},
}
func setupTable(tasks []task) *table.Table {
columns := []string{"ID", "Name", "Project", "Status", "Created At"}
var rows [][]string
for _, task := range tasks {
rows = append(rows, []string{
fmt.Sprintf("%d", task.ID),
task.Name,
task.Project,
task.Status,
task.Created.Format("2006-01-02"),
})
}
t := table.New().
Border(lipgloss.HiddenBorder()).
Headers(columns...).
Rows(rows...).
StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return lipgloss.NewStyle().
Foreground(lipgloss.Color("212")).
Border(lipgloss.NormalBorder()).
BorderTop(false).
BorderLeft(false).
BorderRight(false).
BorderBottom(true).
Bold(true)
}
if row%2 == 0 {
return lipgloss.NewStyle().Foreground(lipgloss.Color("246"))
}
return lipgloss.NewStyle()
})
return t
}
var kanbanCmd = &cobra.Command{
Use: "kanban",
Short: "Interact with your tasks in a Kanban board.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
t, err := openDB(setupPath())
if err != nil {
return err
}
defer t.db.Close()
todos, err := t.getTasksByStatus(todo.String())
if err != nil {
return err
}
ipr, err := t.getTasksByStatus(inProgress.String())
if err != nil {
return err
}
finished, err := t.getTasksByStatus(done.String())
if err != nil {
return err
}
todoCol := kancli.NewColumn(tasksToItems(todos), todo, true)
iprCol := kancli.NewColumn(tasksToItems(ipr), inProgress, false)
doneCol := kancli.NewColumn(tasksToItems(finished), done, false)
board := kancli.NewDefaultBoard([]kancli.Column{todoCol, iprCol, doneCol})
p := tea.NewProgram(board)
_, err = p.Run()
return err
},
}
// convert tasks to items for a list
func tasksToItems(tasks []task) []list.Item {
var items []list.Item
for _, t := range tasks {
items = append(items, t)
}
return items
}
func init() {
addCmd.Flags().StringP(
"project",
"p",
"",
"specify a project for your task",
)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(listCmd)
updateCmd.Flags().StringP(
"name",
"n",
"",
"specify a name for your task",
)
updateCmd.Flags().StringP(
"project",
"p",
"",
"specify a project for your task",
)
updateCmd.Flags().IntP(
"status",
"s",
int(todo),
"specify a status for your task",
)
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(whereCmd)
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(kanbanCmd)
}