-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
257 lines (216 loc) · 5.04 KB
/
main.go
File metadata and controls
257 lines (216 loc) · 5.04 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"os"
"fmt"
"strings"
"strconv"
"errors"
"os/exec"
"github.com/ArjArav98/Issue/src/api"
"github.com/ArjArav98/Issue/src/format"
"github.com/ArjArav98/Issue/src/parse"
"github.com/ArjArav98/Issue/src/config"
)
func main () {
args := os.Args[1:]
if noFurtherArguments(args) {
printError(errors.New("Command needs at least one argument"))
showHelpPrompt()
return
}
/*======*/
/* INIT */
/*======*/
if args[0] == "init" {
createEmptyConfigFile()
return
}
/*=========*/
/* VERSION */
/*=========*/
if args[0] == "version" {
fmt.Println("0.1.0 (Beta)")
return
}
/*======*/
/* HELP */
/*======*/
if args[0] == "help" {
showHelpMenu()
return
}
/*======*/
/* SHOW */
/*======*/
if args[0] == "show" {
if noFurtherArguments(args[1:]) {
printError(errors.New("Command needs at least one more argument"))
showHelpPrompt()
return
}
if onlyOneFurtherArgument(args[1:]) {
if argumentNotNumeric(args[1]) {
printError(errors.New("The last argument must be a valid integer ID"))
showHelpPrompt()
return
}
// If only one argument, it must be issueID.
showIssueWithComments(args[1], true, true)
return
}
if args[1] == "--no-comments" {
showIssueWithComments(args[2], true, false)
return
}
if args[1] == "--only-comments" {
showIssueWithComments(args[2], false, true)
return
}
showIssueWithComments(args[1], true, true)
return
}
/*======*/
/* LIST */
/*======*/
if args[0] == "list" {
if noFurtherArguments(args[1:]) {
showAllIssues(args[1:])
return
}
showAllIssues(args[1:])
return
} else {
printError(errors.New("Command not recognised"))
showHelpPrompt()
}
}
/*********************/
/* COMMAND FUNCTIONS */
/*********************/
func showIssueWithComments (issueIdString string, showIssue bool, showComments bool) {
/*== @section ===========*/
/*=======================*/
issueId, err := strconv.ParseInt(issueIdString, 10, 32)
if err != nil {
printError(errors.New("Issue ID must be a valid number"))
return
}
/*== @section ===========*/
/*=======================*/
issue, err := api.GetIssue(uint64(issueId))
if err != nil {
printError(err)
return
}
comments, err := api.GetComments(issue.Iid, issue.ProjectId)
if err != nil {
printError(err)
return
}
/*== @section ===========*/
/*=======================*/
var output strings.Builder
if showIssue {
output.Write( []byte(format.BeautifyIssue(issue)) )
}
if showComments {
output.Write( []byte(format.BeautifyComments(comments)) )
}
pipeInputToLess(output.String())
}
func showAllIssues (searchArgs []string) {
/*== @section ===========*/
/*=======================*/
queryParams, err := parse.CliArgumentsToQueryParams(searchArgs)
if err!=nil {
printError(err)
return
}
/*== @section ===========*/
/*=======================*/
issues, err := api.GetIssues(queryParams)
if err!= nil {
printError(err)
return
}
/*== @section ===========*/
/*=======================*/
pipeInputToLess(format.BeautifyIssueList(issues))
}
func createEmptyConfigFile () {
err := config.CreateEmptyTemplateFile()
if err!=nil {
printError(err)
}
}
func showHelpMenu () {
fmt.Println(`USAGE: issue [COMMAND] [ARGS]
+----------+
| COMMANDS |
+----------+
list : lists all issues
ARGS: Optional OPTIONS and SEARCH PARAMS.
OPTIONS: --my-open-issues
--my-issues
SEARCH
PARAMS: --assignee_id (integer/Any/None)
--assignee_username (comma-separated-strings)
--created_after (datetime)
--created_before (datetime)
--updated_after (datetime)
--updated_before (datetime)
--labels (comma-separated-strings)
--search (string)
--order_by (created_at/updated_at/)
--state (opened/closed)
SAMPLE
CMDS: issue list --my-open-issues --labels backend,doing
issue list --assignee_username sauron123 --assignee_username frodo99
show : displays an issue in detail
ARGS: Optional OPTIONS and required ISSUE ID.
OPTIONS: --no-comments
--only-comments
ISSUE
ID: Any positive integer
SAMPLE
CMDS: issue show 42
issue show --no-comments 666
init : generates an empty config in current directory
version : displays current version
help : dispays the help menu
`)
}
/*-----------------*/
/* CHECK FUNCTIONS */
/*-----------------*/
func noFurtherArguments (args []string) bool {
return len(args) == 0
}
func onlyOneFurtherArgument (args []string) bool {
return len(args) == 1
}
func argumentNotNumeric (argument string) bool {
_, err := strconv.ParseFloat(argument, 10)
return err != nil
}
/*-----------------*/
/* ERROR FUNCTIONS */
/*-----------------*/
func printError (err error) {
fmt.Printf("ERROR: %v.\n", err)
}
func showHelpPrompt () {
fmt.Println("Enter 'issue help' to see correct usage for commands.")
}
/*------------------*/
/* OUTPUT FUNCTIONS */
/*------------------*/
func pipeInputToLess (input string) {
cmd := exec.Command("less")
cmd.Stdin = strings.NewReader(input)
cmd.Stdout = os.Stdout
err := cmd.Run()
if err!=nil {
printError(err)
}
}