Skip to content

refactor: migrate command handlers to modular directory structure#57

Merged
josecelano merged 4 commits into
mainfrom
copilot/refactor-command-handlers-structure
Oct 27, 2025
Merged

refactor: migrate command handlers to modular directory structure#57
josecelano merged 4 commits into
mainfrom
copilot/refactor-command-handlers-structure

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 27, 2025

Refactors three single-file command handlers (provision, configure, destroy) into modular directory structures matching the existing create handler pattern. Reduces file sizes and improves maintainability through separation of concerns.

Changes

File splits:

  • provision.rs (752 lines) → provision/{mod.rs, handler.rs, errors.rs, tests/}
  • configure.rs (370 lines) → configure/{mod.rs, handler.rs, errors.rs, tests/}
  • destroy.rs (631 lines) → destroy/{mod.rs, handler.rs, tests/}

Structure per handler:

{command}/
├── mod.rs          # Module documentation & public API re-exports
├── handler.rs      # Command handler implementation
├── errors.rs       # Error types with Traceable trait
└── tests/
    ├── mod.rs      # Test module entry
    ├── builders.rs # Test builders and fixtures
    └── integration.rs # Integration tests

Visibility changes:

  • Made struct fields pub(crate) for test access
  • Made helper methods pub(crate) where tests require them (e.g., build_failure_context, should_destroy_infrastructure, cleanup_state_files)

All business logic preserved unchanged. Import paths updated throughout codebase.

Example

Before:

// Single 752-line file mixing concerns
src/application/command_handlers/provision.rs

After:

// Focused modules
src/application/command_handlers/provision/
├── handler.rs      // ~370 lines - business logic only
├── errors.rs       // ~80 lines - error types
└── tests/          // ~280 lines - organized test infrastructure
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor Command Handlers to Modular Directory Structure</issue_title>
<issue_description>## Overview

Refactor the existing single-file command handlers (provision, configure, destroy) to follow the new modular directory structure pattern established in CreateCommandHandler. This improves code organization, maintainability, and scalability by separating concerns into dedicated files.

Currently, three command handlers use a single-file pattern:

  • provision.rs (752 lines) - Too large, mixing handler logic, errors, and tests
  • destroy.rs (631 lines) - Getting unwieldy
  • configure.rs (370 lines) - Manageable but would benefit from separation

The CreateCommandHandler demonstrates a better structure with separate files for handler logic, errors, and tests.

Specification

See detailed specification: docs/issues/refactor-command-handlers-to-modular-structure.md

(Link will be updated after file rename with issue number)

🏗️ Architecture Requirements

DDD Layer: Application Layer
Module Path: src/application/command_handlers/{provision|configure|destroy}/
Pattern: Command Handler with modular organization

Target Structure

Each command handler will follow this pattern:

{command}/
├── mod.rs          # Module documentation & public API
├── handler.rs      # Command handler implementation
├── errors.rs       # Error types with .help()
└── tests/
    ├── mod.rs      # Test module entry
    ├── builders.rs # Test builders and fixtures
    └── integration.rs # Integration tests

Architectural Constraints

  • No changes to command handler business logic (refactoring only)
  • Error handling follows project conventions
  • All existing tests must continue to pass
  • Breaking changes to import paths are acceptable (library not yet publicly used)

Implementation Plan

Important: Migrate and commit each command handler independently. Run pre-commit checks after completing each one before moving to the next.

Subtask 1: Migrate ProvisionCommandHandler (3-4 hours)

  • Create provision/ directory with modular structure
  • Split provision.rs into handler.rs, errors.rs, tests/
  • Update all imports across codebase
  • Verify tests pass and run pre-commit checks
  • Commit independently before moving to next handler

Subtask 2: Migrate ConfigureCommandHandler (2-3 hours)

  • Create configure/ directory with modular structure
  • Split configure.rs into handler.rs, errors.rs, tests/
  • Update all imports across codebase
  • Verify tests pass and run pre-commit checks
  • Commit independently before moving to next handler

Subtask 3: Migrate DestroyCommandHandler (2-3 hours)

  • Create destroy/ directory with modular structure
  • Split destroy.rs into handler.rs, errors.rs, tests/
  • Update all imports across codebase
  • Verify tests pass and run pre-commit checks
  • Commit independently

Subtask 4: Final Verification (1 hour)

  • Run full test suite
  • Run E2E tests
  • Verify consistent structure across all handlers
  • Update any relevant documentation
  • Final pre-commit verification

Acceptance Criteria

Quality Checks:

  • Pre-commit checks pass: ./scripts/pre-commit.sh

Refactoring-Specific Criteria:

  • All three command handlers use modular directory structure
  • Directory structure matches create/ pattern exactly
  • All existing tests continue to pass
  • No changes to business logic (pure refactoring)
  • File sizes reduced to manageable levels (< 300 lines per file)
  • Clear separation of concerns (handler, errors, tests)
  • All imports updated correctly throughout entire codebase

Commit Strategy:

  • Each command handler refactoring committed independently
  • Pre-commit checks pass before each commit
  • Clear, descriptive commit messages following conventional commits format

Related Documentation

Benefits

  • Maintainability: Smaller, focused files (200-300 lines vs 752 lines)
  • Scalability: Easy to add new features without bloating files
  • Consistency: All command handlers follow same structure
  • Testing: Clear test organization and better discoverability
    </issue_description>

Comments on the Issue (you ar...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 27, 2025 20:26
Split provision.rs (752 lines) into modular directory structure:
- provision/mod.rs - Module documentation and public API
- provision/handler.rs - Command handler implementation
- provision/errors.rs - Error types with Traceable impl
- provision/tests/ - Test organization (builders, integration)

All existing tests pass. No changes to business logic.

Co-authored-by: josecelano <58816+josecelano@users.noreply.github.com>
Split configure.rs (370 lines) into modular directory structure:
- configure/mod.rs - Module documentation and public API
- configure/handler.rs - Command handler implementation
- configure/errors.rs - Error types with Traceable impl
- configure/tests/ - Test organization (builders, integration)

All existing tests pass. No changes to business logic.

Co-authored-by: josecelano <58816+josecelano@users.noreply.github.com>
Split destroy.rs (631 lines) into modular directory structure:
- destroy/mod.rs - Module documentation and public API
- destroy/handler.rs - Command handler implementation
- destroy/errors.rs - Error types with Traceable impl
- destroy/tests/ - Test organization (builders, integration)

All existing tests pass. No changes to business logic.

Co-authored-by: josecelano <58816+josecelano@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor command handlers to modular directory structure refactor: migrate command handlers to modular directory structure Oct 27, 2025
Copilot AI requested a review from josecelano October 27, 2025 20:47
@josecelano josecelano marked this pull request as ready for review October 27, 2025 20:54
Copy link
Copy Markdown
Member

@josecelano josecelano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 74cffc0

@josecelano josecelano merged commit 704be06 into main Oct 27, 2025
43 checks passed
@josecelano josecelano deleted the copilot/refactor-command-handlers-structure branch April 15, 2026 16:38
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.

Refactor Command Handlers to Modular Directory Structure

2 participants