-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_helper_data.go
More file actions
515 lines (479 loc) · 17.9 KB
/
code_helper_data.go
File metadata and controls
515 lines (479 loc) · 17.9 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package main
import (
"fmt"
"strings"
"github.com/rapidforge-io/rapidforge/utils"
)
type codeHelperItemSpec struct {
ID string
Title string
Kind string
Contexts []string
Languages []string
Keywords []string
Summary string
Content string
Snippets map[string]string
SourceID string
QuickRef bool
}
type codeHelperSectionSpec struct {
ID string
Title string
ItemIDs []string
}
type codeHelperSourceSpec struct {
ID string
Label string
Description string
Searchable bool
ExternalURLTemplate string
}
func helperSnippets(pairs ...string) map[string]string {
snippets := map[string]string{}
for index := 0; index+1 < len(pairs); index += 2 {
if pairs[index+1] == "" {
continue
}
snippets[pairs[index]] = pairs[index+1]
}
return snippets
}
func (spec codeHelperItemSpec) toMap() map[string]any {
context := "all"
if len(spec.Contexts) == 1 {
context = spec.Contexts[0]
}
item := map[string]any{
"id": spec.ID,
"title": spec.Title,
"kind": spec.Kind,
"context": context,
"contexts": spec.Contexts,
"language": spec.Languages,
"keywords": spec.Keywords,
"summary": spec.Summary,
"content": spec.Content,
"quickRef": spec.QuickRef,
}
if len(spec.Snippets) > 0 {
item["snippet"] = spec.Snippets["bash"]
item["snippets"] = spec.Snippets
}
if spec.SourceID != "" {
item["sourceId"] = spec.SourceID
}
return item
}
func (spec codeHelperSectionSpec) toMap() map[string]any {
return map[string]any{
"id": spec.ID,
"title": spec.Title,
"itemIds": spec.ItemIDs,
}
}
func (spec codeHelperSourceSpec) toMap() map[string]any {
return map[string]any{
"id": spec.ID,
"label": spec.Label,
"description": spec.Description,
"searchable": spec.Searchable,
"externalUrlTemplate": spec.ExternalURLTemplate,
}
}
func defaultCodeHelperItems() []codeHelperItemSpec {
return []codeHelperItemSpec{
{
ID: "payload-data",
Title: "PAYLOAD_DATA",
Kind: "env_var",
Contexts: []string{utils.WebhookEntity},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"request", "body", "payload", "json", "webhook"},
Summary: "Request body data for webhook runs.",
Content: "RapidForge stores the raw request body in PAYLOAD_DATA so scripts can parse or forward it.",
Snippets: helperSnippets("bash", `echo "$PAYLOAD_DATA"`, "lua", `local payload = os.getenv("PAYLOAD_DATA")`, "mruby", `payload = env("PAYLOAD_DATA")`),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: "form-vars",
Title: "FORM_{NAME}",
Kind: "env_var",
Contexts: []string{utils.WebhookEntity},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"form", "request", "fields", "post"},
Summary: "Parsed form fields from webhook requests.",
Content: "Each submitted form field becomes an environment variable such as FORM_COMMENT or FORM_EMAIL.",
Snippets: helperSnippets("bash", `echo "$FORM_COMMENT"`, "lua", `local comment = os.getenv("FORM_COMMENT")`, "mruby", `comment = env("FORM_COMMENT")`),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: "url-param-vars",
Title: "URL_PARAM_{NAME}",
Kind: "env_var",
Contexts: []string{utils.WebhookEntity},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"query", "params", "url", "get"},
Summary: "Query parameters for GET-style webhook requests.",
Content: "RapidForge exposes query parameters as URL_PARAM_* values so you can branch on request input.",
Snippets: helperSnippets("bash", `echo "$URL_PARAM_ID"`, "lua", `local id = os.getenv("URL_PARAM_ID")`, "mruby", `id = env("URL_PARAM_ID")`),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: "header-vars",
Title: "HEADER_{HEADER_NAME}",
Kind: "env_var",
Contexts: []string{utils.WebhookEntity},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"headers", "request", "user agent", "auth"},
Summary: "Incoming webhook headers as environment variables.",
Content: "Headers are normalized into HEADER_* variables such as HEADER_USER_AGENT and HEADER_CONTENT_TYPE.",
Snippets: helperSnippets("bash", `echo "$HEADER_USER_AGENT"`, "lua", `local userAgent = os.getenv("HEADER_USER_AGENT")`, "mruby", `user_agent = env("HEADER_USER_AGENT")`),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: "credential-vars",
Title: "CRED_{CREDENTIAL_NAME}",
Kind: "env_var",
Contexts: []string{"all"},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"credentials", "secret", "token", "oauth"},
Summary: "Saved credentials exposed as environment variables.",
Content: "Stored credentials are injected as CRED_* variables, for example CRED_GITHUB_OAUTH_TOKEN.",
Snippets: helperSnippets("bash", `echo "$CRED_GITHUB_OAUTH_TOKEN"`, "lua", `local token = os.getenv("CRED_GITHUB_OAUTH_TOKEN")`, "mruby", `token = env("CRED_GITHUB_OAUTH_TOKEN")`),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: "kv-helpers",
Title: "KV Helpers (Bash)",
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"bash"},
Keywords: []string{"kv", "key value", "storage", "sqlite"},
Summary: "Read and write small persistent values from Bash scripts.",
Content: "Use the built-in kv_get, kv_set, kv_del, and kv_list helpers for small shared state across webhook and periodic-task runs.",
Snippets: helperSnippets(
"bash", `kv_get KEY
kv_set KEY VALUE
kv_del KEY
kv_list`,
),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: "lua-kv-module",
Title: `Lua "kv" Module`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"lua"},
Keywords: []string{"lua", "kv", "require", "storage", "keys"},
Summary: `Built-in Lua module for key-value storage.`,
Content: `RapidForge ships luarunner/libs/kv.lua, so Lua scripts can require("kv") and call kv.get, kv.set, kv.del, and kv.list.`,
Snippets: helperSnippets(
"lua", `local kv = require("kv")
local value, err = kv.get("KEY")
if value then
print(value)
end
kv.set("KEY", "VALUE")
kv.del("KEY")
local keys = kv.list()`,
),
SourceID: "rapidforge-internal",
QuickRef: true,
},
{
ID: `lua-http-module`,
Title: `Lua "http" Module`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"lua"},
Keywords: []string{"lua", "http", "require", "curl", "get", "post", "put", "delete"},
Summary: `Built-in Lua HTTP helper backed by curl.`,
Content: `RapidForge ships luarunner/libs/http.lua with http.get, http.post, http.put, and http.delete. Each call returns response body and HTTP status code.`,
Snippets: helperSnippets(
"lua", `local http = require("http")
local body, status = http.get("https://example.com", {
["Accept"] = "application/json"
})
local postBody, postStatus = http.post(
"https://example.com/api",
os.getenv("PAYLOAD_DATA") or "{}",
{ ["Content-Type"] = "application/json" }
)`,
),
SourceID: "lua-reference",
QuickRef: true,
},
{
ID: `lua-json-module`,
Title: `Lua "json" Module`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"lua"},
Keywords: []string{"lua", "json", "require", "encode", "decode"},
Summary: `Built-in Lua JSON encode/decode helper.`,
Content: `RapidForge ships luarunner/libs/json.lua, so Lua scripts can require("json") and use json.encode(...) and json.decode(...).`,
Snippets: helperSnippets(
"lua", `local json = require("json")
local payload = os.getenv("PAYLOAD_DATA") or "{}"
local data = json.decode(payload)
local encoded = json.encode({
ok = true,
id = data and data.id
})`,
),
SourceID: "lua-reference",
QuickRef: true,
},
{
ID: `mruby-env-helper`,
Title: `mRuby env helper`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"mruby"},
Keywords: []string{"mruby", "env", "environment", "payload", "headers"},
Summary: `Read RapidForge variables in mRuby with env("NAME").`,
Content: `RapidForge injects all runtime variables into the mRuby bootstrap layer and exposes them through env("NAME") and RAPIDFORGE_ENV.`,
Snippets: helperSnippets(
"mruby", `payload = env("PAYLOAD_DATA")
user_agent = env("HEADER_USER_AGENT")
puts RAPIDFORGE_ENV.keys.sort.inspect`,
),
SourceID: "mruby-reference",
QuickRef: true,
},
{
ID: `mruby-http-helper`,
Title: `mRuby HTTP helpers`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"mruby"},
Keywords: []string{"mruby", "http", "curl", "json", "get", "post", "put", "patch", "delete"},
Summary: `Use http_get/http_post/http_put/http_patch/http_delete in mRuby scripts.`,
Content: `RapidForge boots mRuby with top-level HTTP helpers backed by the bundled mruby-curl runtime. Each helper returns [body, status].`,
Snippets: helperSnippets(
"mruby", `body, status = http_get("https://example.com", {
"Accept" => "application/json"
})
payload = JSON.generate({ "ok" => true })
post_body, post_status = http_post(
"https://example.com/api",
payload,
{ "Content-Type" => "application/json" }
)`,
),
SourceID: "mruby-reference",
QuickRef: true,
},
{
ID: `mruby-kv-helper`,
Title: `mRuby KV helpers`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"mruby"},
Keywords: []string{"mruby", "kv", "storage", "sqlite", "keys"},
Summary: `Use kv_get/kv_set/kv_del/kv_list in mRuby scripts.`,
Content: `RapidForge exposes KV helpers as top-level mRuby functions so scripts can read and persist small values without shelling out.`,
Snippets: helperSnippets(
"mruby", `kv_set("job:last_id", "123")
value = kv_get("job:last_id")
keys = kv_list
deleted = kv_del("job:last_id")`,
),
SourceID: "mruby-reference",
QuickRef: true,
},
{
ID: `mruby-json-helper`,
Title: `mRuby JSON helpers`,
Kind: "helper_method",
Contexts: []string{"all"},
Languages: []string{"mruby"},
Keywords: []string{"mruby", "json", "parse", "generate", "payload"},
Summary: `Use JSON.parse(...) and JSON.generate(...) in mRuby scripts.`,
Content: `The bundled mRuby runtime includes JSON support, so scripts can parse payloads and generate response bodies without extra setup.`,
Snippets: helperSnippets(
"mruby", `payload = JSON.parse(env("PAYLOAD_DATA") || "{}")
encoded = JSON.generate({
"ok" => true,
"id" => payload["id"]
})
puts encoded`,
),
SourceID: "mruby-reference",
QuickRef: true,
},
{
ID: `mruby-enumerable-patterns`,
Title: `mRuby Enumerable patterns`,
Kind: "snippet",
Contexts: []string{"all"},
Languages: []string{"mruby"},
Keywords: []string{"mruby", "enumerable", "array", "map", "select", "find"},
Summary: `Common collection patterns for Arrays and Enumerable.`,
Content: `Use Ruby's collection helpers like each, map, select, and find to transform webhook payloads and task data cleanly.`,
Snippets: helperSnippets(
"mruby", `items = JSON.parse(env("PAYLOAD_DATA") || "[]")
names = items.map do |item|
item["name"]
end
selected = items.select do |item|
item["active"] == true
end
match = items.find do |item|
item["id"] == "target"
end`,
),
SourceID: "mruby-reference",
},
{
ID: "bash-http",
Title: "HTTP Request in Bash",
Kind: "snippet",
Contexts: []string{"all"},
Languages: []string{"bash"},
Keywords: []string{"bash", "curl", "http", "request"},
Summary: "Send an HTTP request from a Bash script.",
Content: "Use curl for outgoing calls from Bash-based webhook and periodic-task scripts.",
Snippets: helperSnippets(
"bash", `curl -sS -X POST "https://example.com/api" \
-H "Content-Type: application/json" \
-d "$PAYLOAD_DATA"`,
),
SourceID: "bash-reference",
},
{
ID: "lua-http",
Title: "HTTP Request in Lua",
Kind: "snippet",
Contexts: []string{"all"},
Languages: []string{"lua"},
Keywords: []string{"lua", "http", "request"},
Summary: "Use the bundled http Lua library for outgoing requests.",
Content: "RapidForge includes an http helper in Lua scripts for making outbound calls without shelling out.",
Snippets: helperSnippets(
"lua", `local http = require("http")
local response, err = http.request("POST", "https://example.com/api", {
headers = { ["Content-Type"] = "application/json" },
body = os.getenv("PAYLOAD_DATA") or "{}"
})`,
),
SourceID: "lua-reference",
},
{
ID: "on-fail-webhook",
Title: "Webhook On-Fail Vars",
Kind: "env_var",
Contexts: []string{utils.WebhookEntity},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"on fail", "failure", "stderr", "webhook"},
Summary: "Variables available to webhook on-fail scripts.",
Content: "Webhook on-fail scripts can inspect FAILURE_EXIT_CODE, FAILURE_OUTPUT, FAILURE_ERROR, WEBHOOK_ID, and WEBHOOK_PATH.",
Snippets: helperSnippets("bash", `echo "$FAILURE_ERROR"`, "lua", `local errorText = os.getenv("FAILURE_ERROR")`, "mruby", `error_text = env("FAILURE_ERROR")`),
SourceID: "rapidforge-internal",
},
{
ID: "on-fail-periodic",
Title: "Periodic Task On-Fail Vars",
Kind: "env_var",
Contexts: []string{utils.PeriodicTaskEntity},
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"on fail", "failure", "stderr", "task"},
Summary: "Variables available to periodic-task on-fail scripts.",
Content: "Periodic-task on-fail scripts can inspect FAILURE_EXIT_CODE, FAILURE_OUTPUT, FAILURE_ERROR, and TASK_ID.",
Snippets: helperSnippets("bash", `echo "$FAILURE_EXIT_CODE"`, "lua", `local exitCode = os.getenv("FAILURE_EXIT_CODE")`, "mruby", `exit_code = env("FAILURE_EXIT_CODE")`),
SourceID: "rapidforge-internal",
},
}
}
func defaultCodeHelperSections() []codeHelperSectionSpec {
return []codeHelperSectionSpec{
{ID: "request-data", Title: "Request data", ItemIDs: []string{"payload-data", "form-vars", "url-param-vars", "header-vars", "credential-vars"}},
{ID: "helpers", Title: "Helpers", ItemIDs: []string{"kv-helpers", "on-fail-webhook", "on-fail-periodic"}},
{ID: "lua-modules", Title: "Lua modules", ItemIDs: []string{"lua-kv-module", "lua-http-module", "lua-json-module"}},
{ID: "mruby-helpers", Title: "mRuby helpers", ItemIDs: []string{"mruby-env-helper", "mruby-http-helper", "mruby-kv-helper", "mruby-json-helper", "mruby-enumerable-patterns"}},
{ID: "bash-recipes", Title: "Bash recipes", ItemIDs: []string{"bash-http"}},
}
}
func defaultCodeHelperSources() []codeHelperSourceSpec {
return []codeHelperSourceSpec{
{ID: "rapidforge-internal", Label: "RapidForge Docs", Description: "RapidForge installation and configuration docs.", ExternalURLTemplate: "https://rapidforge.io/install-config/"},
{ID: "bash-reference", Label: "Bash Reference", Description: "External Bash manual and shell examples.", ExternalURLTemplate: "https://www.gnu.org/software/bash/manual/bash.html"},
{ID: "lua-reference", Label: "Lua Reference", Description: "External Lua reference manual.", ExternalURLTemplate: "https://www.lua.org/manual/5.4/"},
{ID: "mruby-reference", Label: "mRuby Reference", Description: "Official mRuby guides and API docs.", ExternalURLTemplate: "https://mruby.org/docs/"},
}
}
func ignoredCodeHelperVariables() map[string]bool {
return map[string]bool{
"PAYLOAD_DATA": true,
"FORM_{NAME}": true,
"URL_PARAM_{NAME}": true,
"HEADER_{HEADER_NAME}": true,
"CRED_{CREDENTIAL_NAME}": true,
"FAILURE_EXIT_CODE": true,
"FAILURE_OUTPUT": true,
"FAILURE_ERROR": true,
"WEBHOOK_ID": true,
"WEBHOOK_PATH": true,
"TASK_ID": true,
}
}
func buildCodeHelperVariableItem(variable string) codeHelperItemSpec {
contexts := []string{"all"}
if strings.HasPrefix(variable, "FORM_") || strings.HasPrefix(variable, "URL_PARAM_") || strings.HasPrefix(variable, "HEADER_") {
contexts = []string{utils.WebhookEntity}
}
summary := "Custom environment variable available to this script."
if strings.HasPrefix(variable, "CRED_") {
summary = "Credential value injected into the script environment."
}
return codeHelperItemSpec{
ID: fmt.Sprintf("env-%s", strings.ToLower(strings.ReplaceAll(variable, "_", "-"))),
Title: variable,
Kind: "env_var",
Contexts: contexts,
Languages: []string{"bash", "lua", "mruby"},
Keywords: []string{"env", "custom", strings.ToLower(variable)},
Summary: summary,
Content: "RapidForge will inject this variable when the script executes.",
Snippets: helperSnippets(
"bash", fmt.Sprintf(`echo "$%s"`, variable),
"lua", fmt.Sprintf(`local value = os.getenv("%s")`, variable),
"mruby", fmt.Sprintf(`value = env("%s")`, variable),
),
}
}
func buildCodeHelperData(context string, variables []string) map[string]any {
items := make([]map[string]any, 0, len(defaultCodeHelperItems())+len(variables))
for _, item := range defaultCodeHelperItems() {
items = append(items, item.toMap())
}
ignoredVars := ignoredCodeHelperVariables()
for _, variable := range variables {
if ignoredVars[variable] {
continue
}
items = append(items, buildCodeHelperVariableItem(variable).toMap())
}
sections := make([]map[string]any, 0, len(defaultCodeHelperSections()))
for _, section := range defaultCodeHelperSections() {
sections = append(sections, section.toMap())
}
sources := make([]map[string]any, 0, len(defaultCodeHelperSources()))
for _, source := range defaultCodeHelperSources() {
sources = append(sources, source.toMap())
}
return map[string]any{
"context": context,
"sections": sections,
"items": items,
"sources": sources,
}
}