Skip to content

[duplicate-code] Duplicate Code Pattern: --guards-mode Flag Validation in CLI Commands #4378

@github-actions

Description

@github-actions

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

  1. 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

  • Review duplication findings
  • Add validateDIFCModeFlag helper to internal/cmd/flags_difc.go
  • Replace duplicated 3-line block in root.go and proxy.go
  • Run make lint and make test to verify no regressions

Parent Issue

See parent analysis report: #4376
Related to #4376

Generated by Duplicate Code Detector · ● 4.8M ·

  • expires on Apr 30, 2026, 6:17 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions