This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
task.go
209 lines (181 loc) · 5.9 KB
/
task.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
package commands
import (
"fmt"
"os"
"strconv"
"github.com/robdimsdale/wundergo"
"github.com/spf13/cobra"
)
const (
assigneeIDLongFlag = "assingeeID"
recurrenceTypeLongFlag = "recurrenceType"
recurrenceCountLongFlag = "recurrenceCount"
dueDateLongFlag = "dueDate"
starredLongFlag = "starred"
)
var (
// Flags
assigneeID uint
recurrenceType string
recurrenceCount uint
dueDate string
starred bool
// Commands
cmdTasks = &cobra.Command{
Use: "tasks",
Short: "gets all tasks",
Long: `tasks gets the user's tasks.
`,
Run: func(cmd *cobra.Command, args []string) {
// Currently sending completed=false is the same as not sending completed
// Checking for whether the flag has changed protects us from potentially
// breaking changes, i.e. if the tasks endpoint decides to return all tasks,
// not just non-completed ones.
if listID == 0 {
if cmd.Flags().Changed(completedLongFlag) {
renderOutput(newClient(cmd).CompletedTasks(completed))
} else {
renderOutput(newClient(cmd).Tasks())
}
} else {
if cmd.Flags().Changed(completedLongFlag) {
renderOutput(newClient(cmd).CompletedTasksForListID(listID, completed))
} else {
renderOutput(newClient(cmd).TasksForListID(listID))
}
}
},
}
cmdTask = &cobra.Command{
Use: "task <task-id>",
Short: "gets the task for the provided task id",
Long: `task gets a task specified by <task-id>
`,
Run: func(cmd *cobra.Command, args []string) {
renderOutput(task(cmd, args))
},
}
cmdCreateTask = &cobra.Command{
Use: "create-task",
Short: "creates a task with the specified args",
Long: `create-task creates a task with the specified args
`,
Run: func(cmd *cobra.Command, args []string) {
renderOutput(newClient(cmd).CreateTask(
title,
listID,
assigneeID,
completed,
recurrenceType,
recurrenceCount,
dueDate,
starred,
))
},
}
cmdUpdateTask = &cobra.Command{
Use: "update-task",
Short: "updates a task with the specified args",
Long: `update-task obtains the current state of the task,
and updates fields with the provided flags.
`,
Run: func(cmd *cobra.Command, args []string) {
task, err := task(cmd, args)
if err != nil {
handleError(err)
}
if cmd.Flags().Changed(listIDLongFlag) {
task.ListID = listID
}
if cmd.Flags().Changed(titleLongFlag) {
task.Title = title
}
if cmd.Flags().Changed(assigneeIDLongFlag) {
task.AssigneeID = assigneeID
}
if cmd.Flags().Changed(completedLongFlag) {
task.Completed = completed
}
if cmd.Flags().Changed(recurrenceTypeLongFlag) {
task.RecurrenceType = recurrenceType
}
if cmd.Flags().Changed(recurrenceCountLongFlag) {
task.RecurrenceCount = recurrenceCount
}
if cmd.Flags().Changed(dueDateLongFlag) {
task.DueDate = dueDate
}
if cmd.Flags().Changed(starredLongFlag) {
task.Starred = starred
}
renderOutput(newClient(cmd).UpdateTask(task))
},
}
cmdDeleteTask = &cobra.Command{
Use: "delete-task <task-id>",
Short: "deletes the task for the provided task id",
Long: `delete-task deletes the task specified by <task-id>
`,
Run: func(cmd *cobra.Command, args []string) {
task, err := task(cmd, args)
if err != nil {
fmt.Printf("error getting task: %v\n\n", err)
cmd.Usage()
os.Exit(2)
}
err = newClient(cmd).DeleteTask(task)
if err != nil {
handleError(err)
}
fmt.Printf("task %d deleted successfully\n", task.ID)
},
}
cmdDeleteAllTasks = &cobra.Command{
Use: "delete-all-tasks",
Short: "deletes all tasks",
Long: `delete-all-tasks deletes all tasks.
`,
Run: func(cmd *cobra.Command, args []string) {
err := newClient(cmd).DeleteAllTasks()
if err != nil {
handleError(err)
}
fmt.Printf("all tasks deleted successfully\n")
},
}
)
func init() {
cmdTasks.Flags().UintVarP(&listID, listIDLongFlag, listIDShortFlag, 0, "filter by listID")
cmdTasks.Flags().BoolVar(&completed, completedLongFlag, false, "filter for completed tasks")
cmdCreateTask.Flags().UintVarP(&listID, listIDLongFlag, listIDShortFlag, 0, "id of list to which task will belong")
cmdCreateTask.Flags().StringVar(&title, titleLongFlag, "", "title of task")
cmdCreateTask.Flags().UintVar(&assigneeID, assigneeIDLongFlag, 0, "id of task assignee")
cmdCreateTask.Flags().BoolVar(&completed, completedLongFlag, false, "whether task is completed")
cmdCreateTask.Flags().StringVar(&recurrenceType, recurrenceTypeLongFlag, "", "recurrence type")
cmdCreateTask.Flags().UintVar(&recurrenceCount, recurrenceCountLongFlag, 0, "id of task assignee")
cmdCreateTask.Flags().StringVar(&dueDate, dueDateLongFlag, "", "due date of task")
cmdCreateTask.Flags().BoolVar(&starred, starredLongFlag, false, "whether task is starred")
cmdUpdateTask.Flags().UintVarP(&listID, listIDLongFlag, listIDShortFlag, 0, "id of list to which task will belong")
cmdUpdateTask.Flags().StringVar(&title, titleLongFlag, "", "title of task")
cmdUpdateTask.Flags().UintVar(&assigneeID, assigneeIDLongFlag, 0, "id of task assignee")
cmdUpdateTask.Flags().BoolVar(&completed, completedLongFlag, false, "whether task is completed")
cmdUpdateTask.Flags().StringVar(&recurrenceType, recurrenceTypeLongFlag, "", "recurrence type")
cmdUpdateTask.Flags().UintVar(&recurrenceCount, recurrenceCountLongFlag, 0, "id of task assignee")
cmdUpdateTask.Flags().StringVar(&dueDate, dueDateLongFlag, "", "due date of task")
cmdUpdateTask.Flags().BoolVar(&starred, starredLongFlag, false, "whether task is starred")
}
func task(cmd *cobra.Command, args []string) (wundergo.Task, error) {
if len(args) != 1 {
fmt.Printf("incorrect number of arguments provided\n\n")
cmd.Usage()
os.Exit(2)
}
idInt, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("error parsing taskID: %v\n\n", err)
cmd.Usage()
os.Exit(2)
}
id := uint(idInt)
return newClient(cmd).Task(id)
}