Part of duplicate code analysis: #4376
Summary
The --guards-mode flag validation block (3 lines, identical error message) is copy-pasted verbatim in both internal/cmd/root.go and internal/cmd/proxy.go. While minor in isolation, it is a clear exact-copy pattern.
Duplication Details
Pattern: ParseEnforcementMode CLI validation
- Severity: Low
- Occurrences: 2 (identical code, same error string)
- Locations:
internal/cmd/root.go (line 194)
internal/cmd/proxy.go (line 144)
Code Sample (identical in both files):
if _, err := difc.ParseEnforcementMode(difcMode); err != nil {
return fmt.Errorf("invalid --guards-mode flag: %w", err)
}
The same pattern also appears as a comment/reference in internal/cmd/flags_difc.go (getDefaultDIFCMode), showing this validation logic is spread across three locations in the cmd package.
Impact Analysis
- Maintainability: If the error message or validation logic changes (e.g., to include valid values in the error), it must be updated in two places
- Bug Risk: Low — the current duplication is stable
- Code Bloat: Minimal, but the pattern could spread as more server modes are added
Refactoring Recommendations
- Extract
validateDIFCModeFlag helper in the cmd package
- Location:
internal/cmd/flags_difc.go (near getDefaultDIFCMode)
- Signature:
func validateDIFCModeFlag(mode string) error
- Implementation:
func validateDIFCModeFlag(mode string) error {
if _, err := difc.ParseEnforcementMode(mode); err != nil {
return fmt.Errorf("invalid --guards-mode flag: %w", err)
}
return nil
}
- Both
root.go and proxy.go call validateDIFCModeFlag(difcMode) instead
- Estimated effort: 30 minutes
- Benefits: single place to update the error message or add valid-values hint
Implementation Checklist
Parent Issue
See parent analysis report: #4376
Related to #4376
Generated by Duplicate Code Detector · ● 4.8M · ◷
Part of duplicate code analysis: #4376
Summary
The
--guards-modeflag validation block (3 lines, identical error message) is copy-pasted verbatim in bothinternal/cmd/root.goandinternal/cmd/proxy.go. While minor in isolation, it is a clear exact-copy pattern.Duplication Details
Pattern:
ParseEnforcementModeCLI validationinternal/cmd/root.go(line 194)internal/cmd/proxy.go(line 144)Code Sample (identical in both files):
The same pattern also appears as a comment/reference in
internal/cmd/flags_difc.go(getDefaultDIFCMode), showing this validation logic is spread across three locations in thecmdpackage.Impact Analysis
Refactoring Recommendations
validateDIFCModeFlaghelper in thecmdpackageinternal/cmd/flags_difc.go(neargetDefaultDIFCMode)func validateDIFCModeFlag(mode string) errorroot.goandproxy.gocallvalidateDIFCModeFlag(difcMode)insteadImplementation Checklist
validateDIFCModeFlaghelper tointernal/cmd/flags_difc.goroot.goandproxy.gomake lintandmake testto verify no regressionsParent Issue
See parent analysis report: #4376
Related to #4376