Skip to content

Refactor add_command.go into focused modules (1,218 → 610 lines)#12286

Merged
pelikhan merged 6 commits intomainfrom
copilot/refactor-add-command-module
Jan 28, 2026
Merged

Refactor add_command.go into focused modules (1,218 → 610 lines)#12286
pelikhan merged 6 commits intomainfrom
copilot/refactor-add-command-module

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

add_command.go grew to 1,218 lines with mixed responsibilities (compilation, resolution, repository ops, PR workflow). Split into focused modules for maintainability.

Changes

New modules extracted:

  • add_workflow_compilation.go (127 lines) - Compilation orchestration and title/source helpers
  • add_workflow_resolution.go (256 lines) - Workflow resolution, wildcard expansion, dispatch checking
  • add_workflow_repository.go (163 lines) - Repo listing and interactive selection
  • add_workflow_pr.go (118 lines) - PR creation workflow with branch management

Updated:

  • add_command.go (610 lines) - Command registration, orchestration, core operations

Structure

Before:
add_command.go (1,218 lines)
├── Command setup
├── Workflow resolution
├── Repository operations  
├── PR workflow
├── Compilation helpers
└── Core operations

After:
add_command.go (610 lines)
├── Command setup
├── Orchestration (AddWorkflows, AddResolvedWorkflows)
└── Core operations (addWorkflowsNormal, addWorkflowWithTracking)

add_workflow_compilation.go (127 lines)
add_workflow_resolution.go (256 lines)  
add_workflow_repository.go (163 lines)
add_workflow_pr.go (118 lines)

Notes

  • Public API unchanged (AddWorkflows, ResolveWorkflows, NewAddCommand)
  • Existing test coverage leveraged (no new tests needed)
  • Core operations (addWorkflowsNormal, addWorkflowWithTracking) remain in main file due to tight coupling
Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor add_command.go (1,218 lines) into focused modules</issue_title>
<issue_description>### Overview

The file pkg/cli/add_command.go has grown to 1,218 lines, making it difficult to maintain and test. This task involves refactoring it into smaller, focused files with improved test coverage.

Current State

  • File: pkg/cli/add_command.go
  • Size: 1,218 lines
  • Test Coverage: 206 test lines (17% ratio) - primarily structural tests, missing core logic coverage
  • Complexity: High - contains 17 functions handling command setup, workflow resolution, repository operations, PR creation, and file operations
  • Primary Issue: The addWorkflowWithTracking() function alone is 262 lines with high cyclomatic complexity
Full File Analysis

Detailed Breakdown

Functional Areas:

  1. Command Definition & Configuration (Lines 61-203)

    • NewAddCommand() - 142 lines
    • Flag registration (11+ flags)
    • Help text and usage examples
    • Interactive mode decision logic
  2. Workflow Resolution (Lines 208-337)

    • ResolveWorkflows() - 130 lines
    • Workflow spec parsing and validation
    • Repository installation coordination
    • Wildcard expansion
    • Metadata extraction from frontmatter
  3. Workflow Addition Logic (Lines 338-407)

    • AddWorkflows() - orchestrator function
    • AddResolvedWorkflows() - 49 lines
    • Routing between normal and PR flows
  4. Repository Listing & Interactive Selection (Lines 408-557)

    • handleRepoOnlySpec() - 67 lines
    • showInteractiveWorkflowSelection() - 32 lines
    • displayAvailableWorkflows() - 48 lines
    • Table rendering and user interaction
  5. Normal Workflow Addition (Lines 558-658)

    • addWorkflowsNormal() - 101 lines
    • File creation and compilation
    • Git operations and staging
    • Push handling with confirmation
  6. PR Creation Flow (Lines 661-765)

    • addWorkflowsWithPR() - 105 lines
    • Branch creation (with random suffix)
    • PR creation via GitHub API
    • Branch cleanup and restoration
  7. Single Workflow Addition (Lines 766-1027) ⚠️ COMPLEXITY HOTSPOT

    • addWorkflowWithTracking() - 262 lines
    • File copying and path resolution
    • Version management and source tracking
    • Content manipulation (append, title updates)
    • Compilation orchestration
    • Multiple error handling paths
    • Git staging integration
  8. Utility Functions (Lines 1028-1218)

    • updateWorkflowTitle() - 12 lines
    • compileWorkflow() and 3 variants - 115 lines total
    • addSourceToWorkflow() - 8 lines
    • expandWildcardWorkflows() - 36 lines
    • checkWorkflowHasDispatch() - 46 lines

Complexity Hotspots:

  • ⚠️ addWorkflowWithTracking() (262 lines) - handles file operations, version tracking, source metadata, compilation
  • ⚠️ NewAddCommand() (142 lines) - massive flag configuration with complex help text
  • ⚠️ ResolveWorkflows() (130 lines) - orchestrates multiple subsystems (parsing, installation, expansion)
  • ⚠️ compileWorkflowWithTrackingAndRefresh() - complex compilation flow with file tracking

Coupling Issues:

The file has heavy coupling with:

  • workflow package (compilation)
  • parser package (spec parsing)
  • console package (UI rendering)
  • Git operations (inline throughout)
  • File I/O operations scattered across functions

Duplication Patterns:

  • Multiple compileWorkflow*() function variants with similar logic
  • Repeated git staging patterns in multiple functions
  • Similar error handling and verbose flag checking
  • Repeated file existence checks and path operations

Refactoring Strategy

Proposed File Splits

Split the file into 6 focused modules organized by responsibility:

1. add_command.go (Command Definition)

  • Functions: NewAddCommand(), AddWorkflows(), AddResolvedWorkflows()
  • Responsibility: Command registration, flag configuration, high-level orchestration
  • Estimated LOC: ~250 lines
  • Rationale: Keep command definition and flag setup together, delegate implementation to specialized modules

2. add_workflow_resolution.go (Workflow Resolution)

  • Functions: ResolveWorkflows(), expandWildcardWorkflows(), checkWorkflowHasDispatch()
  • Types: ResolvedWorkflow, ResolvedWorkflows
  • Responsibility: Workflow spec parsing, repository installation, wildcard expansion, metadata extraction
  • Estimated LOC: ~200 lines
  • Rationale: Isolated workflow resolution logic with clear input/output contracts

3. add_workflow_repository.go (Repository Operations)

  • Functions: handleRepoOnlySpec(), showInteractiveWorkflowSelection(), displayAvailableWorkflows()
  • Responsibility: Repository listing, interactive workflow selection, workflow discovery
  • Estimated LOC: ~150 lines
    ...

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits January 28, 2026 15:08
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor add_command.go into focused modules Refactor add_command.go into focused modules (1,218 → 610 lines) Jan 28, 2026
Copilot AI requested a review from pelikhan January 28, 2026 15:21
@pelikhan pelikhan marked this pull request as ready for review January 28, 2026 15:26
@pelikhan pelikhan merged commit e56a7b5 into main Jan 28, 2026
123 of 124 checks passed
@pelikhan pelikhan deleted the copilot/refactor-add-command-module branch January 28, 2026 15:43
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.

[file-diet] Refactor add_command.go (1,218 lines) into focused modules

2 participants