Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

The codebase had ~38 lines of duplicate docker inspect logic across three functions in internal/config/env_validation.go, each validating container IDs and executing docker commands with different format templates.

Changes

New helper function runDockerInspect():

  • Validates container ID (prevents injection)
  • Executes docker inspect with format template parameter
  • Returns trimmed output with consistent error handling

Refactored functions now delegate to helper:

  • checkPortMapping(): 20 → 11 lines
  • checkStdinInteractive(): 14 → 8 lines
  • checkLogDirMounted(): 15 → 9 lines
// Before: repeated validation + exec logic in each function
func checkStdinInteractive(containerID string) bool {
    if err := validateContainerID(containerID); err != nil {
        return false
    }
    cmd := exec.Command("docker", "inspect", "--format", "{{.Config.OpenStdin}}", containerID)
    output, err := cmd.Output()
    if err != nil {
        return false
    }
    return strings.TrimSpace(string(output)) == "true"
}

// After: helper centralizes the pattern
func checkStdinInteractive(containerID string) bool {
    output, err := runDockerInspect(containerID, "{{.Config.OpenStdin}}")
    if err != nil {
        return false
    }
    return output == "true"
}

Security note: runDockerInspect() is internal-only with hardcoded format templates. Container ID validation prevents injection; format templates are never user-supplied.

Test coverage

Added 4 test cases covering container ID validation, error handling, and invalid input rejection.

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Duplicate Code Analysis Report</issue_title>
<issue_description># 🔍 Duplicate Code Analysis Report

Analysis of commit a53fac8

Summary

This automated analysis identified 3 significant duplication patterns across the Go codebase that impact code maintainability and increase the risk of inconsistent updates. The patterns span logger initialization, Docker command execution, and error handling.

Detected Patterns

This analysis found 3 significant duplication patterns:

  1. Logger Initialization Pattern - Severity: High - See sub-issue [duplicate-code] Duplicate Code Pattern: Logger Initialization #250
  2. Docker Inspect Command Pattern - Severity: Medium - See sub-issue [duplicate-code] Duplicate Code Pattern: Docker Inspect Commands #251
  3. Close Method Implementation - Severity: Medium - See sub-issue [duplicate-code] Duplicate Code Pattern: Close Method Implementation #252

Overall Impact

  • Total Duplicated Lines: ~120 lines of nearly identical code
  • Affected Files: 6 files in internal/logger/ and internal/config/
  • Maintainability Risk: High - changes to initialization or cleanup logic must be replicated across multiple files
  • Refactoring Priority: High for Pattern 1, Medium for Patterns 2 and 3

Next Steps

  1. Review individual pattern sub-issues for detailed analysis
  2. Prioritize refactoring based on severity and impact
  3. Create implementation plan for highest priority patterns

Analysis Metadata

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 16, 2026 01:17
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate code patterns in Go codebase Refactor duplicate docker inspect code with helper function Jan 16, 2026
Copilot AI requested a review from lpcox January 16, 2026 01:23
@lpcox lpcox marked this pull request as ready for review January 16, 2026 01:30
@lpcox lpcox merged commit 60b162c into main Jan 16, 2026
3 checks passed
@lpcox lpcox deleted the copilot/fix-duplicate-code-issues branch January 16, 2026 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Analysis Report

2 participants