Commit 704be06
committed
Merge #57: refactor: migrate command handlers to modular directory structure
74cffc0 refactor: [#56] migrate DestroyCommandHandler to modular structure (copilot-swe-agent[bot])
ece9119 refactor: [#56] migrate ConfigureCommandHandler to modular structure (copilot-swe-agent[bot])
845df78 refactor: [#56] migrate ProvisionCommandHandler to modular structure (copilot-swe-agent[bot])
309a5d4 Initial plan (copilot-swe-agent[bot])
Pull request description:
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:
```rust
// Single 752-line file mixing concerns
src/application/command_handlers/provision.rs
```
After:
```rust
// 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
```
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
----
*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](https://github.com/torrust/torrust-tracker-deployer/blob/main/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:
```text
{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
- [Module Organization Guide](https://github.com/torrust/torrust-tracker-deployer/blob/main/docs/contributing/module-organization.md)
- [Error Handling Guide](https://github.com/torrust/torrust-tracker-deployer/blob/main/docs/contributing/error-handling.md)
- [Testing Conventions](https://github.com/torrust/torrust-tracker-deployer/blob/main/docs/contributing/testing.md)
- Reference Implementation: `src/application/command_handlers/create/`
## 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...
</details>
- Fixes #56
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 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](https://gh.io/copilot-coding-agent-tips) in the docs.
ACKs for top commit:
josecelano:
ACK 74cffc0
Tree-SHA512: 487b98cd00b8bfdd30e8d1cbe8fa8b32d79ddefb6098cd5f660797841f97a8b192f2292b7d9423db876affeafd4fe2f0865751fe181887f2dc6c375211bf8b5319 files changed
Lines changed: 1406 additions & 1169 deletions
File tree
- src/application/command_handlers
- configure
- tests
- destroy
- tests
- provision
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
Lines changed: 8 additions & 152 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | | - | |
| 5 | + | |
4 | 6 | | |
| 7 | + | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
| |||
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
13 | | - | |
14 | 16 | | |
15 | 17 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | 18 | | |
54 | 19 | | |
55 | 20 | | |
| |||
69 | 34 | | |
70 | 35 | | |
71 | 36 | | |
72 | | - | |
73 | | - | |
74 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
75 | 40 | | |
76 | 41 | | |
77 | 42 | | |
| |||
194 | 159 | | |
195 | 160 | | |
196 | 161 | | |
197 | | - | |
198 | 162 | | |
199 | 163 | | |
200 | 164 | | |
| |||
214 | 178 | | |
215 | 179 | | |
216 | 180 | | |
217 | | - | |
| 181 | + | |
218 | 182 | | |
219 | 183 | | |
220 | 184 | | |
| |||
260 | 224 | | |
261 | 225 | | |
262 | 226 | | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
Lines changed: 74 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
0 commit comments