extractRequiredKeys parses source code content and returns fully-qualified config keys (namespace:key). It tracks which Config object uses which namespace via simple line-by-line analysis.
(content, defaultNS string)
| 1348 | // config keys (namespace:key). It tracks which Config object uses which |
| 1349 | // namespace via simple line-by-line analysis. |
| 1350 | func extractRequiredKeys(content, defaultNS string) []string { |
| 1351 | lines := strings.Split(content, "\n") |
| 1352 | |
| 1353 | // Map variable names to their config namespace. |
| 1354 | // e.g. "config" -> "voting-app", "aws_config" -> "aws" |
| 1355 | varNS := map[string]string{} |
| 1356 | |
| 1357 | // Patterns to detect config variable assignments: |
| 1358 | // Python: config = pulumi.Config() or config = pulumi.Config("ns") |
| 1359 | // TS: const config = new pulumi.Config() or new pulumi.Config("ns") |
| 1360 | // Go: cfg := config.New(ctx, "ns") |
| 1361 | reAssignDefault := regexp.MustCompile(`(\w+)\s*[:=]\s*(?:new\s+)?(?:pulumi\.)?Config\(\s*\)`) |
| 1362 | reAssignNS := regexp.MustCompile(`(\w+)\s*[:=]\s*(?:new\s+)?(?:pulumi\.)?Config\(\s*["']([^"']+)["']\s*\)`) |
| 1363 | // Go style: cfg, err := config.New(ctx, "ns") or cfg := config.New(ctx, "ns") |
| 1364 | reGoAssign := regexp.MustCompile(`(\w+)(?:\s*,\s*\w+)?\s*[:=]\s*(?:\w+\.)?(?:New|Try)\w*\(\s*\w+\s*,\s*["']([^"']+)["']\s*\)`) |
| 1365 | |
| 1366 | var keys []string |
| 1367 | |
| 1368 | for _, line := range lines { |
| 1369 | trimmed := strings.TrimSpace(line) |
| 1370 | |
| 1371 | // Detect config variable assignments |
| 1372 | if m := reAssignNS.FindStringSubmatch(trimmed); m != nil { |
| 1373 | varNS[m[1]] = m[2] |
| 1374 | } else if m := reAssignDefault.FindStringSubmatch(trimmed); m != nil { |
| 1375 | varNS[m[1]] = defaultNS |
| 1376 | } else if m := reGoAssign.FindStringSubmatch(trimmed); m != nil { |
| 1377 | varNS[m[1]] = m[2] |
| 1378 | } |
| 1379 | |
| 1380 | // Detect require calls |
| 1381 | // Match patterns like: config.require("key"), config.require_secret("key"), etc. |
| 1382 | reCall := regexp.MustCompile(`(\w+)\.(require|require_secret|requireSecret|Require|RequireSecret)\(\s*["']([^"']+)["']`) |
| 1383 | if m := reCall.FindStringSubmatch(trimmed); m != nil { |
| 1384 | varName := m[1] |
| 1385 | configKey := m[3] |
| 1386 | |
| 1387 | ns := defaultNS |
| 1388 | if mappedNS, ok := varNS[varName]; ok { |
| 1389 | ns = mappedNS |
| 1390 | } |
| 1391 | |
| 1392 | fullKey := ns + ":" + configKey |
| 1393 | keys = append(keys, fullKey) |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | return keys |
| 1398 | } |
| 1399 | |
| 1400 | // pricingRelevantSuffixes are config key suffixes that affect cost estimation. |
| 1401 | // Keys matching these should NOT be auto-filled with dummy values. |
no outgoing calls
no test coverage detected