Part of duplicate code analysis: #1305
Summary
internal/cmd/flags_logging.go and internal/cmd/flags_difc.go each contain getDefault* functions that are structurally identical 1–3 line wrappers around envutil.GetEnv*. There are 4 such functions. While individually small, the pattern is purely mechanical and signals that a table-driven approach (or direct inline calls) would remove the repetition.
Duplication Details
Pattern: getDefault* env-var wrapper functions in cmd flags files
- Severity: Low
- Occurrences: 4
- Locations:
internal/cmd/flags_logging.go lines 34–47 (3 functions)
internal/cmd/flags_difc.go lines 29–31 (1 function)
All four functions follow this template:
func getDefaultX() T {
return envutil.GetEnvY("MCP_GATEWAY_X", defaultX)
}
Concrete examples:
// flags_logging.go
func getDefaultLogDir() string {
return envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", defaultLogDir)
}
func getDefaultPayloadDir() string {
return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", defaultPayloadDir)
}
func getDefaultPayloadSizeThreshold() int {
return envutil.GetEnvInt("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD", defaultPayloadSizeThreshold)
}
// flags_difc.go
func getDefaultEnableDIFC() bool {
return envutil.GetEnvBool("MCP_GATEWAY_ENABLE_DIFC", defaultEnableDIFC)
}
Impact Analysis
- Maintainability: Adding a new flag with an env-var default requires adding another boilerplate function; the pattern will grow as features are added
- Bug Risk: Low — functions are too simple to diverge incorrectly
- Code Bloat: ~12 lines of boilerplate for 4 trivial wrappers
Refactoring Recommendations
-
Inline the calls directly — since each function is called exactly once (at cmd.Flags().*Var(...) registration), remove the helper and call envutil.GetEnv* inline:
cmd.Flags().StringVar(&logDir, "log-dir",
envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", defaultLogDir),
"Directory for log files ...")
This removes 4 functions without losing readability.
-
Or centralise defaults in a struct if the number of env-configurable flags grows significantly:
type flagDefaults struct {
LogDir string
PayloadDir string
PayloadSizeThreshold int
EnableDIFC bool
}
func loadFlagDefaults() flagDefaults { ... }
Implementation Checklist
Parent Issue
See parent analysis report: #1305
Related to #1305
Generated by Duplicate Code Detector
Part of duplicate code analysis: #1305
Summary
internal/cmd/flags_logging.goandinternal/cmd/flags_difc.goeach containgetDefault*functions that are structurally identical 1–3 line wrappers aroundenvutil.GetEnv*. There are 4 such functions. While individually small, the pattern is purely mechanical and signals that a table-driven approach (or direct inline calls) would remove the repetition.Duplication Details
Pattern:
getDefault*env-var wrapper functions in cmd flags filesinternal/cmd/flags_logging.golines 34–47 (3 functions)internal/cmd/flags_difc.golines 29–31 (1 function)All four functions follow this template:
Concrete examples:
Impact Analysis
Refactoring Recommendations
Inline the calls directly — since each function is called exactly once (at
cmd.Flags().*Var(...)registration), remove the helper and callenvutil.GetEnv*inline:This removes 4 functions without losing readability.
Or centralise defaults in a struct if the number of env-configurable flags grows significantly:
Implementation Checklist
getDefault*wrapper functionsenvutil.GetEnv*calls at flag registration sitesParent Issue
See parent analysis report: #1305
Related to #1305