-
Notifications
You must be signed in to change notification settings - Fork 48
/
cmd_explore.go
184 lines (164 loc) · 4.79 KB
/
cmd_explore.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
//go:build !lambdabinary
// +build !lambdabinary
package sparta
import (
"context"
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
awsv2CF "github.com/aws/aws-sdk-go-v2/service/cloudformation"
broadcast "github.com/dustin/go-broadcast"
tcell "github.com/gdamore/tcell/v2"
spartaAWS "github.com/mweagle/Sparta/v3/aws"
"github.com/rivo/tview"
"github.com/rs/zerolog"
)
const (
broadcasterFunctionSelect = "functionSelect"
broadcasterFileSubmit = "fileSubmit"
)
////////////////////////////////////////////////////////////////////////////////
//
// Public
//
// ExploreWithInputFilter allows the caller to provide additional filters
// for the source files that will be used as inputs
func ExploreWithInputFilter(ctx context.Context,
serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api APIGateway,
site *S3Site,
inputExtensions []string,
s3BucketName string,
buildTags string,
linkerFlags string,
logger *zerolog.Logger) error {
// We need to setup the log output view so that we have a writer for the
// log output
logDataView := tview.NewTextView().
SetScrollable(true).
SetDynamicColors(true)
logDataView.SetChangedFunc(func() {
logDataView.ScrollToEnd()
})
logDataView.SetBorder(true).SetTitle("Output")
colorWriter := tview.ANSIWriter(logDataView)
newLogger := logger.Output(colorWriter).Level(logger.GetLevel())
logger = &newLogger
// Great - everybody get's an aws session
awsConfig, awsConfigErr := spartaAWS.NewConfig(ctx, logger)
if awsConfigErr != nil {
return awsConfigErr
}
// Go get the stack and put the ARNs in the list of things. For that
// we need to get the stack resources...
cfSvc := awsv2CF.NewFromConfig(awsConfig)
input := &awsv2CF.DescribeStackResourcesInput{
StackName: awsv2.String(serviceName),
}
stackResourceOutputs, stackResourceOutputsErr := cfSvc.DescribeStackResources(ctx, input)
if stackResourceOutputsErr != nil {
return stackResourceOutputsErr
}
// Load the settings
settingsMap := loadSettings()
// Make the channel map
channelMap := make(map[string]broadcast.Broadcaster)
channelMap[broadcasterFunctionSelect] = broadcast.NewBroadcaster(1)
channelMap[broadcasterFileSubmit] = broadcast.NewBroadcaster(1)
application := tview.NewApplication()
// Setup the rest of them...
focusTargets := []tview.Primitive{}
dropdown, selectorFocusable := newFunctionSelector(awsConfig,
stackResourceOutputs.StackResources,
application,
lambdaAWSInfos,
settingsMap,
channelMap[broadcasterFunctionSelect],
logger)
eventDropdown, eventFocusable := newEventInputSelector(ctx,
awsConfig,
application,
lambdaAWSInfos,
settingsMap,
inputExtensions,
channelMap[broadcasterFunctionSelect],
logger)
outputView, outputViewFocusable := newCloudWatchLogTailView(ctx,
awsConfig,
application,
lambdaAWSInfos,
settingsMap,
channelMap[broadcasterFunctionSelect],
logger)
// There are four primary views
if selectorFocusable != nil {
focusTargets = append(focusTargets, selectorFocusable...)
}
if eventFocusable != nil {
focusTargets = append(focusTargets, eventFocusable...)
}
if logDataView != nil {
focusTargets = append(focusTargets, logDataView)
}
if outputViewFocusable != nil {
focusTargets = append(focusTargets, outputViewFocusable...)
}
// Make it easy and use a Flex layout...
flex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(dropdown, 3, 0, true).
AddItem(tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(eventDropdown, 0, 1, false).
AddItem(logDataView, 0, 2, false), 0, 1, false).
AddItem(outputView, 0, 1, false)
// Run it...
application.SetRoot(flex, true).SetFocus(flex)
currentIndex := 0
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
logger.Debug().
Interface("Key", event.Key()).
Interface("Name", event.Name()).
Interface("Modifier", event.Modifiers()).
Msg("Input key")
switch event.Key() {
case tcell.KeyTab,
tcell.KeyBacktab:
direction := 1
if event.Key() == tcell.KeyBacktab {
direction = -1
}
nextIndex := (currentIndex + direction*1) % len(focusTargets)
application.SetFocus(focusTargets[nextIndex])
currentIndex = nextIndex
default:
// NOP
}
return event
})
return application.Run()
}
// Explore is an interactive command that brings up a GUI to test
// lambda functions previously deployed into AWS lambda. It's not supported in the
// AWS binary build
func Explore(serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api APIGateway,
site *S3Site,
s3BucketName string,
buildTags string,
linkerFlags string,
logger *zerolog.Logger) error {
return ExploreWithInputFilter(context.Background(),
serviceName,
serviceDescription,
lambdaAWSInfos,
api,
site,
[]string{"json"},
s3BucketName,
buildTags,
linkerFlags,
logger)
}