Part of duplicate code analysis: #3207
Summary
In internal/server/guard_init.go, the same environment-variable lookup for the WASM guards root directory is performed twice in separate functions. Both functions independently call strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar)) and handle the empty-string case, leading to 5–8 lines of duplicated initialization code.
Duplication Details
Pattern: Repeated strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar)) lookup
// findServerWASMGuardFile (lines 157–161)
guardsRootDir := strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))
if guardsRootDir == "" {
logGuardInit.Printf("Skipping WASM guard discovery: %s is not set", wasmGuardsDirEnvVar)
return "", false, nil
}
// logWASMGuardsDirConfiguration (lines 191–198)
guardsRootDir := strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))
if guardsRootDir == "" {
log.Printf("[DIFC] %s is not set", wasmGuardsDirEnvVar)
return
}
log.Printf("[DIFC] %s=%s", wasmGuardsDirEnvVar, guardsRootDir)
Impact Analysis
- Maintainability: Any change to how the env var is read (e.g. adding validation, switching to
envutil.GetEnvString) must be applied in two places.
- Bug Risk: Low — the two functions serve different purposes (discovery vs logging), but if one site adds extra trimming or validation the other may be missed.
- Code Bloat: Minor (~8 lines), but the constant
wasmGuardsDirEnvVar is already centralised; only the lookup idiom is duplicated.
Refactoring Recommendations
-
Extract a small private helper getWASMGuardsRootDir() string that performs the trimmed env-var read:
// In guard_init.go
func getWASMGuardsRootDir() string {
return strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))
}
-
Update both call sites to use getWASMGuardsRootDir(), reducing each to a single-expression variable assignment.
-
Estimated effort: 15 minutes.
Implementation Checklist
Parent Issue
See parent analysis report: #3207
Related to #3207
Generated by Duplicate Code Detector · ◷
Part of duplicate code analysis: #3207
Summary
In
internal/server/guard_init.go, the same environment-variable lookup for the WASM guards root directory is performed twice in separate functions. Both functions independently callstrings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))and handle the empty-string case, leading to 5–8 lines of duplicated initialization code.Duplication Details
Pattern: Repeated
strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))lookupSeverity: Low
Occurrences: 2 independent reads of the same env var in the same file
Locations:
internal/server/guard_init.golines 157–161 (findServerWASMGuardFile)internal/server/guard_init.golines 191–198 (logWASMGuardsDirConfiguration)Code Sample:
Impact Analysis
envutil.GetEnvString) must be applied in two places.wasmGuardsDirEnvVaris already centralised; only the lookup idiom is duplicated.Refactoring Recommendations
Extract a small private helper
getWASMGuardsRootDir() stringthat performs the trimmed env-var read:Update both call sites to use
getWASMGuardsRootDir(), reducing each to a single-expression variable assignment.Estimated effort: 15 minutes.
Implementation Checklist
getWASMGuardsRootDir()helper inguard_init.gofindServerWASMGuardFileto call the helperlogWASMGuardsDirConfigurationto call the helpermake testto confirm no behaviour changeParent Issue
See parent analysis report: #3207
Related to #3207