-
Notifications
You must be signed in to change notification settings - Fork 36
Description
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 exportedUpdateWorkflows()must remain exportedUpdateActions()must remain exportedMergeWorkflowContent()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-unitpasses 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.gowith shared types - Create
update_display.gowith display functions - Add tests and verify
- Create
-
Phase 2: Extract isolated features
- Create
update_extension_check.go - Create
update_git.go - Add tests and verify
- Create
-
Phase 3: Extract GitHub Actions logic
- Create
update_actions.gowith complete Actions update system - Move types and all related functions
- Add comprehensive tests
- Create
-
Phase 4: Extract merge logic
- Create
update_merge.go - Add merge-specific tests
- Create
-
Phase 5: Split workflow logic
- Create
update_workflows.gowith core workflow operations - Keep orchestrator in main file
- Add workflow discovery and update tests
- Create
-
Phase 6: Minimize main file
- Keep only command setup in
update_command.go - Verify all imports and exports work
- Keep only command setup in
Acceptance Criteria
- Original file is split into 8 focused files
- Each new file is under 500 lines
- All tests pass (
make test-unitandmake 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-finishcompletes successfully - Update command works identically to before refactoring
Additional Context
- Repository Guidelines: Follow patterns in
AGENTS.mdandskills/developer.skill.md - Code Organization: Prefer many small files grouped by functionality (see
create_*.gopattern) - Testing: Match existing test patterns in
pkg/cli/*_test.go - Similar Patterns: See
pkg/workflow/safe_outputs.gofamily 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