-
Notifications
You must be signed in to change notification settings - Fork 26
/
run.go
297 lines (252 loc) · 5.92 KB
/
run.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package jiraui
import (
"fmt"
"os"
"github.com/coryb/optigo"
"github.com/op/go-logging"
ui "gopkg.in/gizak/termui.v2"
)
var exitNow = false
type EditPager interface {
DeleteRuneBackward()
InsertRune(r rune)
Update()
Create()
}
type TicketCommander interface {
ActiveTicketId() string
Refresh()
}
type Searcher interface {
SetSearch(string)
Search()
}
type CommandBoxer interface {
SetCommandMode(bool)
ExecuteCommand()
CommandMode() bool
CommandBar() *CommandBar
Update()
}
type NextTicketer interface {
NextTicket()
}
type PrevTicketer interface {
PrevTicket()
}
type GoBacker interface {
GoBack()
}
type Refresher interface {
Refresh()
}
type ItemSelecter interface {
SelectItem()
}
type TicketEditer interface {
EditTicket()
}
type TicketCommenter interface {
CommentTicket()
}
type PagePager interface {
NextLine(int)
PreviousLine(int)
NextPara()
PreviousPara()
NextPage()
PreviousPage()
TopOfPage()
BottomOfPage()
IsPopulated() bool
Update()
}
type Navigable interface {
Create()
Update()
Id() string
}
type RankSelector interface {
MarkItemForRanking()
}
var currentPage Navigable
var previousPages []Navigable
var ticketQueryPage *QueryPage
var helpPage *HelpPage
var labelListPage *LabelListPage
var sortOrderPage *SortOrderPage
var passwordInputBox *PasswordInputBox
var commandBar *CommandBar
func changePage() {
if currentPage == nil {
currentPage = new(QueryPage)
}
switch currentPage.(type) {
case *QueryPage:
log.Debugf("changePage: QueryPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *TicketListPage:
log.Debugf("changePage: TicketListPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *SortOrderPage:
log.Debugf("changePage: SortOrderPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *LabelListPage:
log.Debugf("changePage: LabelListPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *TicketShowPage:
log.Debugf("changePage: TicketShowPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *HelpPage:
log.Debugf("changePage: HelpPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
}
}
const LOG_MODULE = "jiraui"
var (
log = logging.MustGetLogger(LOG_MODULE)
format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
)
var cliOpts map[string]interface{}
func Run() {
usage := func(ok bool) {
printer := fmt.Printf
if !ok {
printer = func(format string, args ...interface{}) (int, error) {
return fmt.Fprintf(os.Stderr, format, args...)
}
defer func() {
os.Exit(1)
}()
} else {
defer func() {
os.Exit(0)
}()
}
output := fmt.Sprintf(`
Usage:
jira-ui ls <Query Options>
jira-ui ISSUE
jira-ui
General Options:
-e --endpoint=URI URI to use for jira
-l --log=FILE FILE to use for log (default /dev/null)
-h --help Show this usage
-u --user=USER Username to use for authenticaion
-v --verbose Increase output logging
--skiplogin Skip the login check. You must have a valid session token (eg via 'jira login')
--version Print version
Ticket View Options:
-t --template=FILE Template file to use for viewing tickets
-m --max_wrap=VAL Maximum word-wrap width when viewing ticket text (0 disables)
Query Options:
-q --query=JQL Jira Query Language expression for the search
-f --queryfields=FIELDS Fields that are used in "list" view
`)
printer(output)
}
jiraCommands := map[string]string{
"list": "list",
"ls": "list",
"password": "password",
"passwd": "password",
}
cliOpts = make(map[string]interface{})
cliOpts["log"] = "/dev/null"
setopt := func(name string, value interface{}) {
cliOpts[name] = value
}
logging.SetLevel(logging.NOTICE, "jira")
logging.SetLevel(logging.NOTICE, LOG_MODULE)
op := optigo.NewDirectAssignParser(map[string]interface{}{
"h|help": usage,
"version": func() {
fmt.Println(fmt.Sprintf("version: %s", VERSION))
os.Exit(0)
},
"v|verbose+": func() {
logging.SetLevel(logging.GetLevel(LOG_MODULE)+1, LOG_MODULE)
},
"l|log=s": setopt,
"u|user=s": setopt,
"endpoint=s": setopt,
"q|query=s": setopt,
"f|queryfields=s": setopt,
"t|template=s": setopt,
"m|max_wrap=i": setopt,
"skip_login": setopt,
})
if err := op.ProcessAll(os.Args[1:]); err != nil {
log.Errorf("%s", err)
usage(false)
}
args := op.Args
f, err := os.Create(cliOpts["log"].(string))
if err != nil {
panic(err)
}
defer f.Close()
backend := logging.NewLogBackend(f, "", 0)
logging.SetBackend(backend)
var command string
if len(args) > 0 {
if alias, ok := jiraCommands[args[0]]; ok {
command = alias
args = args[1:]
} else {
command = "view"
args = args[0:]
}
} else {
command = "toplevel"
}
requireArgs := func(count int) {
if len(args) < count {
log.Errorf("Not enough arguments. %d required, %d provided", count, len(args))
usage(false)
}
}
if val, ok := cliOpts["skip_login"]; !ok || !val.(bool) {
err = ensureLoggedIntoJira()
if err != nil {
log.Error("Login failed. Aborting")
os.Exit(2)
}
}
err = ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
registerEventHandlers()
helpPage = new(HelpPage)
commandBar = new(CommandBar)
switch command {
case "list":
if query := cliOpts["query"]; query == nil {
log.Errorf("Must supply a --query option to %q", command)
os.Exit(1)
} else {
p := new(TicketListPage)
p.ActiveQuery.JQL = query.(string)
p.ActiveQuery.Name = "adhoc"
currentPage = p
}
case "view":
requireArgs(1)
p := new(TicketShowPage)
p.TicketId = args[0]
currentPage = p
case "toplevel":
currentPage = new(QueryPage)
case "password":
currentPage = new(PasswordInputBox)
default:
log.Errorf("Unknown command %s", command)
os.Exit(1)
}
for exitNow != true {
currentPage.Create()
ui.Loop()
}
}