Commit 5d5726e
committed
Merge #78: Extract shared command dependencies into CommandContext
7af25fa feat: [#67] extract shared command dependencies into CommandContext (copilot-swe-agent[bot])
e2d2e48 Initial plan (copilot-swe-agent[bot])
Pull request description:
Both create and destroy command handlers manually instantiated identical dependencies (repository, clock, user output) with duplicate 8+ line initialization blocks.
## Changes
**New `CommandContext` struct** (`src/presentation/commands/context.rs`)
- Encapsulates `EnvironmentRepository`, `Clock`, and `UserOutput`
- `new(working_dir)` - production constructor with default configuration
- `new_for_testing(repo, clock, output)` - test constructor for dependency injection
- `report_error(output, error)` - utility for consistent error reporting
- 8 unit tests
**Updated command handlers**
- `destroy/handler.rs` - Replaced 17 lines of initialization with `CommandContext::new()`
- `create/subcommands/environment.rs` - Replaced 13 lines of initialization with `CommandContext::new()`
## Before/After
```rust
// Before: Duplicated in every handler
let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
let repository = repository_factory.create(working_dir.to_path_buf());
let clock = Arc::new(SystemClock);
let mut output = UserOutput::new(DEFAULT_VERBOSITY);
// After: Single line
let mut ctx = CommandContext::new(working_dir.to_path_buf());
// Access dependencies
let handler = DestroyCommandHandler::new(ctx.repository().clone(), ctx.clock().clone());
ctx.output().progress("Processing...");
```
## Testing
- All 1010 unit tests pass (54 command tests, 8 new for context)
- No behavioral changes to existing handlers
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Extract Shared Command Dependencies and Output (CommandContext)</issue_title>
> <issue_description>**Parent Issue**: #63
> **Type**: 🎯 Quick Win
> **Impact**: 🟢🟢🟢 High
> **Effort**: 🔵🔵 Medium
> **Priority**: P0
> **Depends On**: #65 (Proposal 2)
>
> ## Problem
>
> Both command handlers manually create the same dependencies with duplicate code:
>
> ```rust
> // In create handler
> let repository_factory = RepositoryFactory::new(Duration::from_secs(30));
> let repository = repository_factory.create(working_dir.to_path_buf());
> let clock: Arc<dyn Clock> = Arc::new(SystemClock);
> let mut output = UserOutput::new(VerbosityLevel::Normal);
>
> // In destroy handler (exact duplicate)
> let repository_factory = RepositoryFactory::new(Duration::from_secs(30));
> let repository = repository_factory.create(working_dir.to_path_buf());
> let clock = Arc::new(crate::shared::SystemClock);
> let mut output = UserOutput::new(VerbosityLevel::Normal);
> ```
>
> ## Proposed Solution
>
> Create a `CommandContext` struct in `src/presentation/commands/context.rs` that includes **all** shared dependencies including `UserOutput`:
>
> ```rust
> pub struct CommandContext {
> repository: Arc<dyn EnvironmentRepository>,
> clock: Arc<dyn Clock>,
> output: UserOutput,
> }
>
> impl CommandContext {
> pub fn new(working_dir: PathBuf) -> Self {
> let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
> let repository = repository_factory.create(working_dir);
> let clock = Arc::new(SystemClock);
> let output = UserOutput::new(DEFAULT_VERBOSITY);
>
> Self { repository, clock, output }
> }
>
> pub fn repository(&self) -> &Arc<dyn EnvironmentRepository> { &self.repository }
> pub fn clock(&self) -> &Arc<dyn Clock> { &self.clock }
> pub fn output(&mut self) -> &mut UserOutput { &mut self.output }
> }
>
> pub fn report_error(output: &mut UserOutput, error: &dyn std::error::Error) {
> output.error(&error.to_string());
> }
> ```
>
> ## Benefits
>
> - ✅ Eliminates 8+ lines of duplicate code per command handler
> - ✅ Makes testing easier with `new_for_testing()` constructor
> - ✅ Future commands automatically get consistent dependency setup
> - ✅ Single place to modify if dependency initialization changes
> - ✅ No need for separate output helper module
> - ✅ All output goes through the same instance, ensuring consistency
>
> ## Implementation Checklist
>
> - [ ] Create `src/presentation/commands/context.rs`
> - [ ] Define `CommandContext` struct with repository, clock, and output
> - [ ] Implement `new()` constructor using constants
> - [ ] Add accessor methods: `repository()`, `clock()`, `output()`
> - [ ] Add `new_for_testing()` for test support
> - [ ] Add `report_error()` utility function
> - [ ] Add comprehensive documentation with examples
> - [ ] Update `src/presentation/commands/mod.rs` to include module
> - [ ] Update create handler to use `CommandContext`
> - [ ] Update destroy handler to use `CommandContext`
> - [ ] Remove duplicate initialization code from handlers
> - [ ] Update tests to verify same behavior
> - [ ] Run all tests
> - [ ] Run linter and fix issues
>
> ## Acceptance Criteria
>
> - [ ] `CommandContext` struct exists with all dependencies
> - [ ] Both create and destroy handlers use `CommandContext`
> - [ ] No duplicate dependency initialization code remains
> - [ ] `report_error()` utility is available and used
> - [ ] Tests verify context provides correct dependencies
> - [ ] All tests pass: `cargo test presentation::commands`
> - [ ] Pre-commit checks pass: `./scripts/pre-commit.sh`
> - [ ] Code follows project conventions
>
> ## Related Documentation
>
> - [Refactor Plan](https://github.com/torrust/torrust-tracker-deployer/blob/main/docs/refactors/plans/presentation-commands-cleanup.md)</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
- Fixes #66
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
ACKs for top commit:
josecelano:
ACK 7af25fa
Tree-SHA512: 90070cf4f66baccfd762bc82cf30b8dd450b885b2b2f6ee6bd813d336af7a9aa6914b831aa7f88d59b1913476167b9096d695f1a5206069507ac44f6b1370b10File tree
4 files changed
+389
-47
lines changed- src/presentation/commands
- create/subcommands
- destroy
4 files changed
+389
-47
lines changed| 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 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 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 | + | |
0 commit comments