Skip to content

[file-diet] Refactor Large Go File: pkg/cli/update_command.go #6892

@agentic-workflows-dev

Description

@agentic-workflows-dev

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

Current State

  • File: pkg/cli/update_command.go
  • Size: 1,331 lines
  • Test Coverage: 900 lines of tests (test-to-source ratio: 0.68)
  • Complexity: High - combines multiple concerns including command setup, Git operations, workflow updates, merge logic, and GitHub Actions updates

Codebase Health Context

Current file size landscape:

  • 21 files exceed the 800-line healthy threshold
  • Largest file: pkg/cli/update_command.go (1,331 lines)
  • Second largest: pkg/parser/frontmatter.go (1,294 lines)
  • Third largest: pkg/workflow/copilot_engine.go (1,168 lines)

Refactoring Strategy

Based on semantic analysis, the file has clear functional boundaries that can be split into focused modules:

Proposed File Splits

1. update_command.go (Command Setup)

  • Functions: NewUpdateCommand(), flag definitions, command configuration
  • Responsibility: Cobra command setup and CLI interface
  • Estimated LOC: ~150 lines
  • Exports: NewUpdateCommand()

2. update_workflows.go (Workflow Update Logic)

  • Functions:
    • UpdateWorkflows()
    • findWorkflowsWithSource()
    • updateWorkflow()
    • resolveLatestRef()
    • resolveLatestRelease()
    • hasLocalModifications()
  • Responsibility: Core workflow discovery and update operations
  • Estimated LOC: ~500 lines
  • Exports: UpdateWorkflows(), UpdateWorkflowsWithExtensionCheck()

3. update_merge.go (Merge Operations)

  • Functions:
    • MergeWorkflowContent()
    • normalizeWhitespace()
  • Responsibility: 3-way merge logic for preserving local changes
  • Estimated LOC: ~200 lines
  • Exports: MergeWorkflowContent()

4. update_actions.go (GitHub Actions Updates)

  • Functions:
    • UpdateActions()
    • getLatestActionRelease()
    • getLatestActionReleaseViaGit()
    • getActionSHAForTag()
    • marshalActionsLockSorted()
  • Types: actionsLockEntry, actionsLockFile
  • Responsibility: GitHub Actions version management and actions-lock.json updates
  • Estimated LOC: ~350 lines
  • Exports: UpdateActions()

5. update_git.go (Git Operations)

  • Functions:
    • hasGitChanges()
    • runGitCommand()
    • createUpdatePR()
  • Responsibility: Git operations and PR creation
  • Estimated LOC: ~150 lines
  • Exports: None (package-private helpers)

6. update_extension_check.go (Extension Version Check)

  • Functions: checkExtensionUpdate()
  • Responsibility: Check for gh-aw CLI updates
  • Estimated LOC: ~80 lines
  • Exports: None (called from orchestrator)

Shared Types

Keep shared types in a common location:

  • update_types.go: workflowWithSource, updateFailure

Utility Functions

  • update_display.go: showUpdateSummary() (formatting and display)

Test Coverage Plan

Expand tests for each new file to achieve >80% coverage:

1. update_command_test.go

  • Test cases: Command flag parsing, argument validation
  • Target coverage: >80%

2. update_workflows_test.go

  • Test cases:
    • Workflow discovery with filters
    • Ref resolution (tags, branches, commits)
    • Local modification detection
    • Update behavior (force, merge modes)
  • Target coverage: >80%

3. update_merge_test.go

  • Test cases:
    • 3-way merge with no conflicts
    • Merge with conflicts
    • Whitespace normalization
    • Source field updates
  • Target coverage: >80%

4. update_actions_test.go

  • Test cases:
    • actions-lock.json parsing
    • Version resolution (major/minor)
    • SHA fetching for tags
    • Sorted JSON marshaling
  • Target coverage: >80%

5. update_git_test.go

  • Test cases:
    • Git change detection
    • PR creation with proper metadata
    • Error handling for git commands
  • Target coverage: >80%

Implementation Guidelines

  1. Preserve Behavior: Ensure all existing functionality works identically
  2. Maintain Exports: Keep public API unchanged:
    • NewUpdateCommand() must remain exported
    • UpdateWorkflows() must remain exported
    • UpdateActions() must remain exported
    • MergeWorkflowContent() must remain exported (used by other packages)
  3. Add Tests First: Write tests for each new file before refactoring
  4. Incremental Changes: Split one module at a time, starting with cleanest boundaries
  5. Run Tests Frequently: Verify make test-unit passes after each split
  6. Update Imports: Ensure all import paths are correct
  7. Document Changes: Add package-level comments explaining module responsibilities

Suggested Implementation Order

  1. Phase 1: Extract utility functions (lowest risk)

    • Create update_types.go with shared types
    • Create update_display.go with display functions
    • Add tests and verify
  2. Phase 2: Extract isolated features

    • Create update_extension_check.go
    • Create update_git.go
    • Add tests and verify
  3. Phase 3: Extract GitHub Actions logic

    • Create update_actions.go with complete Actions update system
    • Move types and all related functions
    • Add comprehensive tests
  4. Phase 4: Extract merge logic

    • Create update_merge.go
    • Add merge-specific tests
  5. Phase 5: Split workflow logic

    • Create update_workflows.go with core workflow operations
    • Keep orchestrator in main file
    • Add workflow discovery and update tests
  6. Phase 6: Minimize main file

    • Keep only command setup in update_command.go
    • Verify all imports and exports work

Acceptance Criteria

  • Original file is split into 8 focused files
  • Each new file is under 500 lines
  • All tests pass (make test-unit and make test)
  • Test coverage is ≥80% for new files
  • No breaking changes to public API
  • Code passes linting (make lint)
  • Build succeeds (make build)
  • make agent-finish completes successfully
  • Update command works identically to before refactoring

Additional Context

  • Repository Guidelines: Follow patterns in AGENTS.md and skills/developer.skill.md
  • Code Organization: Prefer many small files grouped by functionality (see create_*.go pattern)
  • Testing: Match existing test patterns in pkg/cli/*_test.go
  • Similar Patterns: See pkg/workflow/safe_outputs.go family for examples of well-split code

Priority: Medium
Effort: Large (estimated 4-6 hours)
Expected Impact: Improved maintainability, easier testing, reduced complexity, better code organization
Labels: refactoring, code-health, technical-debt

AI generated by Daily File Diet

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions