-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
393 lines (342 loc) · 9.6 KB
/
model.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-plugin-docs/schemamd"
)
const (
modePadding = 1
viewportWidth = 118
viewportHeight = 25
viewportPadding = 2
// wordwrapWidth adjusts relative to the viewport width to avoid
// rendering into viewport padding
wordwrapWidth = viewportWidth - 5
)
var (
cursorLineStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("212"))
cursorStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("212"))
helpStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("241"))
inputHeadingStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("212")).
Bold(true)
modeStyle = lipgloss.NewStyle().
PaddingRight(modePadding).
PaddingLeft(modePadding).
Background(lipgloss.Color("62")).
Bold(true)
viewportStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
PaddingRight(viewportPadding)
// viewportKeyMap sets custom key bindings for the viewport.
//
// The default keybindings (j, k, u, d, etc.) for navigation can cause
// the viewport to jump around during searches if not overridden.
viewportKeyMap = viewport.KeyMap{
PageDown: key.NewBinding(
key.WithKeys("pgdown"),
key.WithHelp("pgdn", "page down"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup"),
key.WithHelp("pgup", "page up"),
),
HalfPageUp: key.NewBinding(
key.WithKeys("ctrl+u"),
key.WithHelp("ctrl+u", "½ page up"),
),
HalfPageDown: key.NewBinding(
key.WithKeys("ctrl+d"),
key.WithHelp("ctrl+d", "½ page down"),
),
Up: key.NewBinding(
key.WithKeys("up"),
key.WithHelp("↑", "up"),
),
Down: key.NewBinding(
key.WithKeys("down"),
key.WithHelp("↓", "down"),
),
}
)
type mode string
const (
// loadingText is displayed in the viewport at startup and during reloads
loadingText = "Loading providers..."
// modeSchema expects searches for exact resource or data source names
// and returns the corresponding schema.
modeSchema mode = "Schema"
// modeResource expects searches for partial resource or data source names
// and returns a list of matching names from all configured providers.
modeResource mode = "Resource"
modeSchemaPlaceholder = "aws_instance"
modeResourcePlaceholder = "ec2"
)
// errMsg wraps an error in a tea.Msg to be handled by the model update method
type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
// schemaMsg is the tea.Msg structure returned when provider schemas are loaded
// from disk
type schemaMsg struct {
ps tfjson.ProviderSchemas
}
type model struct {
err error
searchMode mode
providerSchemas tfjson.ProviderSchemas
renderer *glamour.TermRenderer
textinput textinput.Model
viewport viewport.Model
}
func newModel() (*model, error) {
rend, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(wordwrapWidth),
)
if err != nil {
return nil, err
}
return &model{
err: nil,
searchMode: modeSchema,
renderer: rend,
textinput: newTextInput(),
viewport: newViewport(),
}, nil
}
func newTextInput() textinput.Model {
ti := textinput.New()
ti.Placeholder = modeSchemaPlaceholder
ti.Prompt = "➜ "
ti.CharLimit = 200
ti.TextStyle = cursorLineStyle
ti.PromptStyle = cursorStyle
ti.Cursor.Style = cursorStyle
ti.Focus()
return ti
}
func newViewport() viewport.Model {
vp := viewport.New(viewportWidth, viewportHeight)
vp.Style = viewportStyle
vp.KeyMap = viewportKeyMap
vp.SetContent(loadingText)
return vp
}
func (m model) Init() tea.Cmd {
return loadProviderSchemas
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
tiCmd tea.Cmd
vpCmd tea.Cmd
)
m.textinput, tiCmd = m.textinput.Update(msg)
m.viewport, vpCmd = m.viewport.Update(msg)
switch msg := msg.(type) {
case schemaMsg:
m.providerSchemas = msg.ps
m.viewport.SetContent(m.postLoadingViewportView())
tiCmd = tea.Batch(tiCmd, textinput.Blink)
case errMsg:
m.err = msg
return m, tea.Quit
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
case tea.KeyCtrlR:
m.viewport.SetContent(loadingText)
return m, loadProviderSchemas
case tea.KeyTab, tea.KeyShiftTab:
if m.searchMode == modeResource {
m.searchMode = modeSchema
m.textinput.Placeholder = modeSchemaPlaceholder
} else {
m.searchMode = modeResource
m.textinput.Placeholder = modeResourcePlaceholder
}
case tea.KeyEnter:
var content string
var err error
name := m.textinput.Value()
if m.searchMode == modeSchema {
content, err = m.searchSchemas(name)
} else if m.searchMode == modeResource {
content, err = m.searchResources(name)
}
if err != nil {
m.err = err
return m, tea.Quit
}
m.viewport.SetYOffset(0)
m.viewport.SetContent(content)
m.textinput.Reset()
}
}
return m, tea.Batch(tiCmd, vpCmd)
}
func (m model) View() string {
if m.err != nil {
return fmt.Sprintf("\nModel enountered an error: %v\n\n", m.err)
}
return fmt.Sprintf(`%s
%s
%s
%s %s
`,
m.headingView(),
m.textinput.View(),
m.viewport.View(),
m.modeView(),
m.helpView(),
)
}
func (m model) headingView() string {
s := "Enter a resource name:"
if m.searchMode == modeResource {
s = "Enter a search term:"
}
return inputHeadingStyle.Render(s)
}
func (m model) helpView() string {
return helpStyle.Render(" ↑/↓, PgUp/PgDn: Navigate • Tab: Toggle Mode • ctrl+c/esc: Quit")
}
func (m model) modeView() string {
return modeStyle.Render(fmt.Sprintf("Mode: %s", m.searchMode))
}
// postLoadingViewportView will appear once provider data is loaded
func (m model) postLoadingViewportView() string {
var names []string
for k := range m.providerSchemas.Schemas {
names = append(names, k)
}
sort.Strings(names)
return fmt.Sprintf("Ready to search. %d providers detected:\n\n- %s",
len(names), strings.Join(names, "\n- "))
}
// searchSchemas finds a resource or data source schema for
// the search term. Rendered markdown content is returned.
func (m model) searchSchemas(term string) (string, error) {
// TODO: aggregate results in the case of multiple matches?
// TODO: allow targeted searching between resources/data sources
var match *tfjson.Schema
for _, prov := range m.providerSchemas.Schemas {
if v, ok := prov.ResourceSchemas[term]; ok {
match = v
break
}
if v, ok := prov.DataSourceSchemas[term]; ok {
match = v
break
}
}
if match == nil {
return notFoundContent(term), nil
}
b := &strings.Builder{}
if err := schemamd.Render(match, b); err != nil {
return "", err
}
formatted := fmt.Sprintf("# %s\n\n%s\n\n%s", term, match.Block.Description, b.String())
return m.renderer.Render(formatted)
}
// searchSchemas finds all resources or data sources containing
// the search term. Rendered markdown content is returned.
func (m model) searchResources(term string) (string, error) {
matches := indexProviderSchemasWithFilter(m.providerSchemas, term)
if len(matches) == 0 {
return notFoundContent(term), nil
}
// TODO: move parsing into a template
b := &strings.Builder{}
for _, match := range matches {
b.WriteString(fmt.Sprintf("# %s\n\n", match.Name))
if len(match.Resources) > 0 {
b.WriteString("## Resources\n\n")
for _, r := range match.Resources {
b.WriteString(fmt.Sprintf("- `%s`\n", r))
}
}
if len(match.DataSources) > 0 {
b.WriteString("## Data Sources\n\n")
for _, ds := range match.DataSources {
b.WriteString(fmt.Sprintf("- `%s`\n", ds))
}
}
}
return m.renderer.Render(b.String())
}
// providerIndex stores resource and data source names for partial search
type providerIndex struct {
Name string
Resources []string
DataSources []string
}
// indexProviderSchemasWithFilters returns an index of provider resources
// and data sources which contain the search term
func indexProviderSchemasWithFilter(ps tfjson.ProviderSchemas, term string) []providerIndex {
var filteredIndex []providerIndex
for k, v := range ps.Schemas {
pi := providerIndex{Name: k}
for r := range v.ResourceSchemas {
if strings.Contains(r, term) {
pi.Resources = append(pi.Resources, r)
}
}
for ds := range v.DataSourceSchemas {
if strings.Contains(ds, term) {
pi.DataSources = append(pi.DataSources, ds)
}
}
if len(pi.Resources) > 0 || len(pi.DataSources) > 0 {
sort.Strings(pi.Resources)
sort.Strings(pi.DataSources)
filteredIndex = append(filteredIndex, pi)
}
}
return filteredIndex
}
// loadProviderSchemas handles fetching configured provider schemas. If
// a schemafile is specified, the schema is read from disk, otherwise,
// the schema is loaded on-demand by executing `terraform providers schema -json`.
// The resource and data source names are also indexed for each provider.
func loadProviderSchemas() tea.Msg {
var (
ps tfjson.ProviderSchemas
b []byte
err error
)
if schemafile == "" {
b, err = exec.Command("terraform", "providers", "schema", "-json").Output()
} else {
b, err = os.ReadFile(schemafile)
}
if err != nil {
return errMsg{err}
}
if err := ps.UnmarshalJSON(b); err != nil {
return errMsg{err}
}
if len(ps.Schemas) == 0 {
return errMsg{errors.New("no provider schemas found")}
}
return schemaMsg{ps}
}
func notFoundContent(term string) string {
return fmt.Sprintf("No matches found for '%s'.\n", term)
}