-
Notifications
You must be signed in to change notification settings - Fork 48
/
cmd_explore_views.go
462 lines (431 loc) · 13.5 KB
/
cmd_explore_views.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//go:build !lambdabinary
// +build !lambdabinary
package sparta
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"time"
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
awsv2CFTypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
awsv2Lambda "github.com/aws/aws-sdk-go-v2/service/lambda"
broadcast "github.com/dustin/go-broadcast"
tcell "github.com/gdamore/tcell/v2"
prettyjson "github.com/hokaccha/go-prettyjson"
spartaCWLogs "github.com/mweagle/Sparta/v3/aws/cloudwatch/logs"
"github.com/rivo/tview"
"github.com/rs/zerolog"
)
var (
progressEmoji = []string{"🌍", "🌎", "🌏"}
windowsProgressEmoji = []string{"◐", "◓", "◑", "◒"}
)
////////////////////////////////////////////////////////////////////////////////
//
// Settings
var mu sync.Mutex
const (
settingSelectedARN = "functionARN"
settingSelectedEvent = "selectedEvent"
)
func settingsFile() string {
return filepath.Join(ScratchDirectory, "explore-settings.json")
}
func saveSetting(key string, value string) {
settingsMap := loadSettings()
settingsMap[string(key)] = value
output, outputErr := json.MarshalIndent(settingsMap, "", " ")
if outputErr != nil {
return
}
mu.Lock()
/* #nosec */
writtenErr := ioutil.WriteFile(settingsFile(), output, os.ModePerm)
if writtenErr != nil {
fmt.Printf("Failed to save settings: %s", writtenErr.Error())
}
mu.Unlock()
}
func loadSettings() map[string]string {
defaultSettings := make(map[string]string)
settingsFile := settingsFile()
mu.Lock()
/* #nosec */
bytes, bytesErr := ioutil.ReadFile(settingsFile)
mu.Unlock()
if bytesErr != nil {
return defaultSettings
}
/* #nosec */
umarshalErr := json.Unmarshal(bytes, &defaultSettings)
if umarshalErr != nil {
fmt.Printf("Failed to unmarshal: %s", umarshalErr.Error())
}
return defaultSettings
}
// Settings
//
////////////////////////////////////////////////////////////////////////////////
func writePrettyString(writer io.Writer, input string) {
colorWriter := tview.ANSIWriter(writer)
var jsonData map[string]interface{}
jsonErr := json.Unmarshal([]byte(input), &jsonData)
if jsonErr == nil {
// pretty print it to colors...
prettyString, prettyStringErr := prettyjson.Marshal(jsonData)
if prettyStringErr == nil {
/* #nosec */
_, writeErr := io.WriteString(colorWriter, string(prettyString))
if writeErr != nil {
fmt.Printf("Failed to writeString: %s", writeErr.Error())
}
} else {
/* #nosec */
_, writeErr := io.WriteString(colorWriter, input)
if writeErr != nil {
fmt.Printf("Failed to writeString: %s", writeErr.Error())
}
}
} else {
/* #nosec */
_, writeErr := io.WriteString(colorWriter, strings.TrimSpace(input))
if writeErr != nil {
fmt.Printf("Failed to writeString: %s", writeErr.Error())
}
}
/* #nosec */
_, writeErr := io.WriteString(writer, "\n")
if writeErr != nil {
fmt.Printf("Failed to writeString: %s", writeErr.Error())
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Select the function to test
//
func newFunctionSelector(awsConfig awsv2.Config,
stackResources []awsv2CFTypes.StackResource,
app *tview.Application,
lambdaAWSInfos []*LambdaAWSInfo,
settings map[string]string,
onChangeBroadcaster broadcast.Broadcaster,
logger *zerolog.Logger) (tview.Primitive, []tview.Primitive) {
lambdaARN := func(stackID string, logicalName string) string {
// stackID: arn:aws:cloudformation:us-west-2:123412341234:stack/MyHelloWorldStack-mweagle/54339e80-6686-11e8-90cd-503f20f2ad82
// lambdaARN: arn:aws:lambda:us-west-2:123412341234:function:MyHelloWorldStack-mweagle_Hello_World
stackParts := strings.Split(stackID, ":")
lambdaARNParts := []string{
"arn:aws:lambda:",
stackParts[3],
":",
stackParts[4],
":function:",
logicalName,
}
return strings.Join(lambdaARNParts, "")
}
// Ok, walk the resources and assemble all the ARNs for the lambda functions
lambdaFunctionARNs := []string{}
for _, eachResource := range stackResources {
if *eachResource.ResourceType == "AWS::Lambda::Function" {
logger.Debug().
Str("Resource", *eachResource.LogicalResourceId).
Msg("Found provisioned Lambda function")
lambdaFunctionARNs = append(lambdaFunctionARNs,
lambdaARN(*eachResource.StackId, *eachResource.PhysicalResourceId))
}
}
sort.Strings(lambdaFunctionARNs)
selectedARN := settings[settingSelectedARN]
selectedIndex := 0
for index, eachARN := range lambdaFunctionARNs {
if eachARN == selectedARN {
selectedIndex = index
break
}
}
dropdown := tview.NewDropDown().
SetCurrentOption(selectedIndex).
SetLabel("Function ARN: ").
SetOptions(lambdaFunctionARNs, nil)
dropdown.SetBorder(true).SetTitle("Select Function")
dropdownSelectFunc := func(text string, selectedIndex int) {
if selectedIndex != -1 {
saveSetting(settingSelectedARN, text)
logger.Debug().Msgf("Selected: %s", selectedARN)
onChangeBroadcaster.Submit(text)
}
}
dropdown.SetSelectedFunc(dropdownSelectFunc)
// Populate it...
return dropdown, []tview.Primitive{dropdown}
}
////////////////////////////////////////////////////////////////////////////////
//
// Select the event to use to invoke the function
//
func newEventInputSelector(ctx context.Context,
awsConfig awsv2.Config,
app *tview.Application,
lambdaAWSInfos []*LambdaAWSInfo,
settings map[string]string,
inputExtensionsFilters []string,
functionSelectedBroadcaster broadcast.Broadcaster,
logger *zerolog.Logger) (tview.Primitive, []tview.Primitive) {
divider := strings.Repeat("━", 20)
activeFunction := ""
ch := make(chan interface{})
functionSelectedBroadcaster.Register(ch)
go func() {
for {
funcSelected := <-ch
activeFunction = funcSelected.(string)
}
}()
lambdaSvc := awsv2Lambda.NewFromConfig(awsConfig)
// First walk the directory for anything that looks
// like a JSON file...
curDir, curDirErr := os.Getwd()
if curDirErr != nil {
return nil, nil
}
jsonFiles := []string{}
walkerFunc := func(path string, info os.FileInfo, err error) error {
for _, eachMatch := range inputExtensionsFilters {
if strings.HasSuffix(strings.ToLower(filepath.Ext(path)), eachMatch) &&
!strings.Contains(path, ScratchDirectory) {
relPath := strings.TrimPrefix(path, curDir)
jsonFiles = append(jsonFiles, relPath)
logger.Debug().
Str("RelativePath", relPath).
Msg("Event file found")
}
}
return nil
}
logger.Debug().
Str("RelativePath", curDir).
Msg("Walking directory")
walkErr := filepath.Walk(curDir, walkerFunc)
if walkErr != nil {
logger.Error().
Err(walkErr).
Str("Directory", curDir).
Msg("Failed to find JSON files in directory")
return nil, nil
}
// Create all the views...
var selectedJSONData []byte
selectedInput := 0
eventSelected := settings[settingSelectedEvent]
for index, eachJSONFile := range jsonFiles {
if eventSelected == eachJSONFile {
selectedInput = index
break
}
}
eventDataView := tview.NewTextView().SetScrollable(true).SetDynamicColors(true)
dropdown := tview.NewDropDown().
SetCurrentOption(selectedInput).
SetLabel("Event: ").
SetOptions(jsonFiles, nil)
selectEventData := func(text string, selectedIndex int) {
// What's the selected item?
if selectedIndex == -1 {
return
}
logger.Debug().Str("EventInputPath", text).Msg("Event data source")
eventDataView.Clear()
// Save it...
saveSetting(settingSelectedEvent, text)
fullPath := curDir + text
/* #nosec */
jsonFile, jsonFileErr := ioutil.ReadFile(fullPath)
if jsonFileErr != nil {
writePrettyString(eventDataView, jsonFileErr.Error())
} else {
writePrettyString(eventDataView, string(jsonFile))
}
selectedJSONData = jsonFile
}
dropdown.SetSelectedFunc(selectEventData)
submitButton := tview.NewButton("Submit")
submitButton.SetBackgroundColorActivated(tcell.ColorDarkGreen)
submitButton.SetLabelColorActivated(tcell.ColorWhite)
submitButton.SetBackgroundColor(tcell.ColorGray)
submitButton.SetLabelColor(tcell.ColorDarkGreen)
submitButton.SetSelectedFunc(func() {
logger.Debug().Str("ActiveFunction", activeFunction).Msg("Invoking function")
// Submit it to lambda
if activeFunction != "" {
lambdaInput := &awsv2Lambda.InvokeInput{
FunctionName: awsv2.String(activeFunction),
Payload: selectedJSONData,
}
invokeOutput, invokeOutputErr := lambdaSvc.Invoke(ctx, lambdaInput)
if invokeOutputErr != nil {
logger.Error().
Err(invokeOutputErr).
Msg("Failed to invoke Lambda function")
} else if invokeOutput.FunctionError != nil {
logger.Error().
Str("Error", *invokeOutput.FunctionError).
Msg("Lambda function produced an error")
} else {
var m interface{}
jsonErr := json.Unmarshal(invokeOutput.Payload, &m)
var responseData interface{}
if jsonErr == nil {
responseData = m
} else {
responseData = string(invokeOutput.Payload)
}
logger.Info().
Interface("payload", responseData).
Msg(divider + " AWS Lambda Response " + divider)
}
}
})
// Ok, so what we need now is a flexbox with a row,
flexRow := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(dropdown, 0, 4, false).
AddItem(submitButton, 10, 1, false)
flex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(flexRow, 1, 0, false).
AddItem(eventDataView, 0, 1, false)
flex.SetBorder(true).SetTitle("Select Event Input")
return flex, []tview.Primitive{dropdown, submitButton, eventDataView}
}
////////////////////////////////////////////////////////////////////////////////
//
// Tail the cloudwatch logs for the active function
//
func newCloudWatchLogTailView(ctx context.Context,
awsConfig awsv2.Config,
app *tview.Application,
lambdaAWSInfos []*LambdaAWSInfo,
settings map[string]string,
functionSelectedBroadcaster broadcast.Broadcaster,
logger *zerolog.Logger) (tview.Primitive, []tview.Primitive) {
osEmojiSet := progressEmoji
switch runtime.GOOS {
case "windows":
osEmojiSet = windowsProgressEmoji
}
// Great - so what we need to do is listen for both the selected function
// and a change in input. If we have values for both, then
// go ahead and issue the request. We can do this with two
// go-routines. The first one is just a go-routine that listens for cloudwatch log events
// for the selected function.
ch := make(chan interface{})
functionSelectedBroadcaster.Register(ch)
// So what we need here is a "Last event timestamp" entry and then the actual
// content...
cloudwatchLogInfoView := tview.NewTextView().SetDynamicColors(true)
cloudwatchLogInfoView.SetBorder(true)
logEventDataView := tview.NewTextView().SetDynamicColors(true)
logEventDataView.SetScrollable(true)
progressEmojiView := tview.NewTextView()
// Ok, for this we need two colums, with the first column
// being the
flexView := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(cloudwatchLogInfoView, 0, 1, false), 3, 0, false).
AddItem(logEventDataView, 0, 1, false).
AddItem(progressEmojiView, 1, 0, false)
flexView.SetBorder(true).SetTitle("CloudWatch Logs")
updateCloudWatchLogInfoView := func(logGroupName string, latestTS int64) {
// Ref: https://godoc.org/github.com/rivo/tview#hdr-Colors
// Color tag definition: [<foreground>:<background>:<flags>]
cloudwatchLogInfoView.Clear()
ts := ""
if latestTS != 0 {
ts = time.Unix(latestTS, 0).Format(time.RFC3339)
}
msg := fmt.Sprintf("[-:-:b]LogGroupName[-:-:-]: [-:-:d]%s",
logGroupName)
if ts != "" {
msg += fmt.Sprintf(" ([-:-:b]Latest Event[-:-:-]: [-:-:d]%s)", ts)
}
writePrettyString(cloudwatchLogInfoView, msg)
}
updateCloudWatchLogInfoView("", 0)
// When we get a new function then
var selectedFunction string
go func() {
var doneChan chan bool
var ticker *time.Ticker
lastTime := int64(0)
animationIndex := 0
for {
funcSelected := <-ch
if selectedFunction == funcSelected.(string) {
continue
}
selectedFunction = funcSelected.(string)
logEventDataView.Clear()
if doneChan != nil {
doneChan <- true
progressEmojiView.Clear()
}
if ticker != nil {
ticker.Stop()
}
ticker = time.NewTicker(time.Millisecond * 333)
lambdaARN := selectedFunction
lambdaParts := strings.Split(lambdaARN, ":")
logGroupName := fmt.Sprintf("/aws/lambda/%s", lambdaParts[len(lambdaParts)-1])
logger.Debug().
Str("Name", logGroupName).
Msg("CloudWatch LogGroupName")
// Put this as the label in the view...
doneChan = make(chan bool)
messages := spartaCWLogs.TailWithContext(ctx,
doneChan,
awsConfig,
logGroupName,
"",
logger)
// Go read it...
go func() {
for {
select {
case event := <-messages:
{
lastTime = *event.Timestamp / 1000
updateCloudWatchLogInfoView(logGroupName, lastTime)
writePrettyString(logEventDataView, *event.Message)
logger.Debug().
Str("EventID", *event.EventId).
Msg("Event received")
logEventDataView.ScrollToEnd()
app.Draw()
}
case <-ticker.C:
/* #nosec */
animationIndex = (animationIndex + 1) % len(osEmojiSet)
progressEmojiView.Clear()
progressText := fmt.Sprintf("%s Waiting for events...", osEmojiSet[animationIndex])
/* #nosec */
_, writeErr := io.WriteString(progressEmojiView, progressText)
if writeErr != nil {
fmt.Printf("Failed to write string: %s", writeErr.Error())
}
// Update the other stuff
updateCloudWatchLogInfoView(logGroupName, lastTime)
app.Draw()
}
}
}()
}
}()
return flexView, []tview.Primitive{logEventDataView}
}