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
- Preserve Behavior: Ensure all existing functionality works identically
- 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)
- Add Tests First: Write tests for each new file before refactoring
- Incremental Changes: Split one module at a time, starting with cleanest boundaries
- Run Tests Frequently: Verify
make test-unit passes after each split
- Update Imports: Ensure all import paths are correct
- Document Changes: Add package-level comments explaining module responsibilities
Suggested Implementation Order
-
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
-
Phase 2: Extract isolated features
- Create
update_extension_check.go
- Create
update_git.go
- Add tests and verify
-
Phase 3: Extract GitHub Actions logic
- Create
update_actions.go with complete Actions update system
- Move types and all related functions
- Add comprehensive tests
-
Phase 4: Extract merge logic
- Create
update_merge.go
- Add merge-specific tests
-
Phase 5: Split workflow logic
- Create
update_workflows.go with core workflow operations
- Keep orchestrator in main file
- Add workflow discovery and update tests
-
Phase 6: Minimize main file
- Keep only command setup in
update_command.go
- Verify all imports and exports work
Acceptance Criteria
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
The file
pkg/cli/update_command.gohas 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
pkg/cli/update_command.goCodebase Health Context
Current file size landscape:
pkg/cli/update_command.go(1,331 lines)pkg/parser/frontmatter.go(1,294 lines)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)NewUpdateCommand(), flag definitions, command configurationNewUpdateCommand()2.
update_workflows.go(Workflow Update Logic)UpdateWorkflows()findWorkflowsWithSource()updateWorkflow()resolveLatestRef()resolveLatestRelease()hasLocalModifications()UpdateWorkflows(),UpdateWorkflowsWithExtensionCheck()3.
update_merge.go(Merge Operations)MergeWorkflowContent()normalizeWhitespace()MergeWorkflowContent()4.
update_actions.go(GitHub Actions Updates)UpdateActions()getLatestActionRelease()getLatestActionReleaseViaGit()getActionSHAForTag()marshalActionsLockSorted()actionsLockEntry,actionsLockFileUpdateActions()5.
update_git.go(Git Operations)hasGitChanges()runGitCommand()createUpdatePR()6.
update_extension_check.go(Extension Version Check)checkExtensionUpdate()Shared Types
Keep shared types in a common location:
update_types.go:workflowWithSource,updateFailureUtility 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.go2.
update_workflows_test.go3.
update_merge_test.go4.
update_actions_test.go5.
update_git_test.goImplementation Guidelines
NewUpdateCommand()must remain exportedUpdateWorkflows()must remain exportedUpdateActions()must remain exportedMergeWorkflowContent()must remain exported (used by other packages)make test-unitpasses after each splitSuggested Implementation Order
Phase 1: Extract utility functions (lowest risk)
update_types.gowith shared typesupdate_display.gowith display functionsPhase 2: Extract isolated features
update_extension_check.goupdate_git.goPhase 3: Extract GitHub Actions logic
update_actions.gowith complete Actions update systemPhase 4: Extract merge logic
update_merge.goPhase 5: Split workflow logic
update_workflows.gowith core workflow operationsPhase 6: Minimize main file
update_command.goAcceptance Criteria
make test-unitandmake test)make lint)make build)make agent-finishcompletes successfullyAdditional Context
AGENTS.mdandskills/developer.skill.mdcreate_*.gopattern)pkg/cli/*_test.gopkg/workflow/safe_outputs.gofamily for examples of well-split codePriority: Medium
Effort: Large (estimated 4-6 hours)
Expected Impact: Improved maintainability, easier testing, reduced complexity, better code organization
Labels: refactoring, code-health, technical-debt