-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
312 lines (265 loc) · 6 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package tessen
import (
"fmt"
"os"
"time"
"github.com/coryb/optigo"
ui "github.com/gizak/termui"
"github.com/op/go-logging"
)
var exitNow = false
var defaultRefreshInterval = 30
var refreshEnabled = true
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 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 Source struct {
Name string
Provider string
Endpoint string
Options map[string]string
CachedData interface{}
}
var currentPage Navigable
var previousPage Navigable
var queryPage *QueryPage
var helpPage *HelpPage
var queryResultsPage *QueryResultsPage
var commandBar *CommandBar
func changePage() {
switch currentPage.(type) {
case *QueryPage:
log.Debugf("changePage: QueryPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *QueryResultsPage:
log.Debugf("changePage: QueryResultsPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *ShowDetailPage:
log.Debugf("changePage: ShowDetailPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
case *HelpPage:
log.Debugf("changePage: HelpPage %s (%p)", currentPage.Id(), currentPage)
currentPage.Create()
}
}
var (
log = logging.MustGetLogger("tessen")
format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
)
var cliOpts map[string]interface{}
var sources []*Source
func getSources(opts map[string]interface{}) []*Source {
if _, ok := opts["sources"]; !ok {
log.Fatal("No sources specified, exiting")
}
sources = make([]*Source, 0)
for _, s := range opts["sources"].([]interface{}) {
source := s.(map[interface{}]interface{})
name := source["name"].(string)
endpoint := source["endpoint"].(string)
provider := source["provider"].(string)
options := make(map[string]string)
if source["options"] != nil {
for k, v := range source["options"].(map[interface{}]interface{}) {
options[k.(string)] = v.(string)
}
}
sources = append(sources, &Source{name, provider, endpoint, options, nil})
}
return sources
}
func collectSource(s *Source, seconds int) {
timer := time.NewTimer(time.Duration(seconds) * time.Second)
<-timer.C
var err error
if s.Provider == "uchiwa" {
log.Debugf("Collecting uchiwa data")
s.CachedData, err = FetchUchiwaEvents(s)
} else if s.Provider == "pagerduty" {
log.Debugf("Collecting pagerduty data")
s.CachedData, err = FetchPagerDutyEvents(s)
} else if s.Provider == "chronos" {
log.Debugf("Collecting chronos data")
s.CachedData, err = FetchChronosJobs(s)
} else {
log.Errorf("Cannot collect from source %q, unimplemented backend type %q", s.Name, s.Provider)
}
if err != nil {
log.Errorf("Error fetching source data for %q: %q\n", s.Name, err)
}
}
func FindSourceByName(name string) *Source {
for _, s := range sources {
if s.Name == name {
return s
}
}
return nil
}
func Run() {
var err error
logging.SetLevel(logging.NOTICE, "")
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:
tessen
General Options:
-h --help Show this usage
-v --verbose Increase output logging
--version Print version
`)
printer(output)
}
commands := map[string]string{}
cliOpts = make(map[string]interface{})
setopt := func(name string, value interface{}) {
cliOpts[name] = value
}
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("")+1, "")
},
"l|listen=s": setopt,
"noui": setopt,
})
if err := op.ProcessAll(os.Args[1:]); err != nil {
log.Error("%s", err)
usage(false)
}
args := op.Args
var command string
if len(args) > 0 {
if alias, ok := commands[args[0]]; ok {
command = alias
args = args[1:]
} else {
command = "view"
args = args[0:]
}
} else {
command = "toplevel"
}
opts := getOpts()
sources := getSources(opts)
if _, ok := opts["noui"]; !ok {
err = ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
registerEventHandlers()
queryPage = new(QueryPage)
helpPage = new(HelpPage)
commandBar = new(CommandBar)
switch command {
case "toplevel":
currentPage = queryPage
default:
log.Error("Unknown command %s", command)
os.Exit(1)
}
}
for _, s := range sources {
collectSource(s, 0)
}
for _, s := range sources {
go func() {
for {
collectSource(s, defaultRefreshInterval)
// TODO: we need a way of calling this only if the data has changed, or if the
// 'currentPage' relates to this dataset.
if obj, ok := currentPage.(Refresher); ok {
if refreshEnabled {
obj.Refresh()
}
}
}
}()
}
if l, ok := opts["listen"]; ok {
log.Debugf("Starting http dashboard on %s", l.(string))
go func() {
log.Fatal(StartHttpDashboard(l.(string)))
}()
}
for exitNow != true {
if err != nil {
log.Errorf("%s", err)
os.Exit(1)
}
if _, ok := opts["noui"]; !ok {
currentPage.Create()
ui.Loop()
}
log.Debug("End of exitNow loop")
}
log.Debug("Normal exit, woohoo!")
}