Part of duplicate code analysis: #3827
Summary
Across three internal/cmd/flags_*.go files, 13 trivially similar getDefault* functions exist, each being a 3-line wrapper that calls envutil.GetEnvString, envutil.GetEnvInt, or envutil.GetEnvBool with a hardcoded env-var name and a default value. Nine of the thirteen are structurally identical single-return wrappers.
Duplication Details
Pattern: getDefault* env-var wrapper functions
Note: getDefaultDIFCMode is a justified exception — it includes validation logic beyond a simple env lookup.
Impact Analysis
- Maintainability: Low risk day-to-day, but adding new flags continues to grow the file with boilerplate. The docstring and function naming convention is inconsistently applied.
- Bug Risk: Low — each function is independent and correct.
- Code Bloat: ~30 lines of highly repetitive 3-line functions.
Refactoring Recommendations
-
Inline the calls directly in the init() / RegisterFlag block
Since these functions are only called once (as default-value arguments in flag registration), they can be inlined:
// Before
cmd.Flags().StringVar(&payloadDir, "payload-dir", getDefaultPayloadDir(), "...")
// After (inline)
cmd.Flags().StringVar(&payloadDir, "payload-dir", envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", config.DefaultPayloadDir), "...")
This removes the need for the 3-line helper entirely. The env-var name is documented in AGENTS.md and README.md, so discoverability is not a concern.
-
Alternative: centralise defaults in a flagDefaults struct
If you prefer named defaults for testability, define them once:
var flagDefaults = struct {
logDir func() string
payloadDir func() string
}{
logDir: func() string { return envutil.GetEnvString("MCP_GATEWAY_LOG_DIR", config.DefaultLogDir) },
payloadDir: func() string { return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", config.DefaultPayloadDir) },
}
This keeps the logic in one place and makes testing easier.
Implementation Checklist
Parent Issue
See parent analysis report: #3827
Related to #3827
Generated by Duplicate Code Detector · ● 1.7M · ◷
Part of duplicate code analysis: #3827
Summary
Across three
internal/cmd/flags_*.gofiles, 13 trivially similargetDefault*functions exist, each being a 3-line wrapper that callsenvutil.GetEnvString,envutil.GetEnvInt, orenvutil.GetEnvBoolwith a hardcoded env-var name and a default value. Nine of the thirteen are structurally identical single-return wrappers.Duplication Details
Pattern:
getDefault*env-var wrapper functionsSeverity: Low
Occurrences: 13 functions across 3 files
Locations:
internal/cmd/flags_logging.go(lines 35–56):getDefaultLogDir,getDefaultPayloadDir,getDefaultPayloadPathPrefix,getDefaultPayloadSizeThresholdinternal/cmd/flags_difc.go(lines 58–92):getDefaultAllowOnlyScopePublic,getDefaultDIFCSinkServerIDs,getDefaultGuardPolicyJSON,getDefaultAllowOnlyOwner,getDefaultAllowOnlyRepo,getDefaultAllowOnlyMinIntegrityinternal/cmd/flags_tracing.go(lines 31–40):getDefaultOTLPEndpoint,getDefaultOTLPServiceNameCode Sample (9 of 13 functions look like this):
Note:
getDefaultDIFCModeis a justified exception — it includes validation logic beyond a simple env lookup.Impact Analysis
Refactoring Recommendations
Inline the calls directly in the
init()/RegisterFlagblockSince these functions are only called once (as default-value arguments in flag registration), they can be inlined:
This removes the need for the 3-line helper entirely. The env-var name is documented in
AGENTS.mdandREADME.md, so discoverability is not a concern.Alternative: centralise defaults in a
flagDefaultsstructIf you prefer named defaults for testability, define them once:
This keeps the logic in one place and makes testing easier.
Implementation Checklist
getDefault*wrappers, replacing with inlineenvutil.Get*callsgetDefaultDIFCModeas-is (it contains validation logic)make lintandmake testto confirm no regressionsParent Issue
See parent analysis report: #3827
Related to #3827