-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.rs
More file actions
315 lines (279 loc) · 11.5 KB
/
Copy pathhandler.rs
File metadata and controls
315 lines (279 loc) · 11.5 KB
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
//! Destroy Command Handler
//!
//! This module handles the destroy command execution at the presentation layer,
//! including environment validation, repository initialization, and user interaction.
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use crate::application::command_handlers::DestroyCommandHandler;
use crate::domain::environment::name::EnvironmentName;
use crate::domain::environment::state::Destroyed;
use crate::domain::Environment;
use crate::presentation::commands::context::CommandContext;
use crate::presentation::commands::factory::CommandHandlerFactory;
use crate::presentation::progress::ProgressReporter;
use crate::presentation::user_output::UserOutput;
use super::errors::DestroySubcommandError;
// ============================================================================
// CONSTANTS
// ============================================================================
/// Number of main steps in the destroy workflow
const DESTROY_WORKFLOW_STEPS: usize = 3;
// ============================================================================
// PRESENTATION LAYER CONTROLLER
// ============================================================================
/// Presentation layer controller for destroy command workflow
///
/// Coordinates user interaction, progress reporting, and input validation
/// before delegating to the application layer `DestroyCommandHandler`.
///
/// # Responsibilities
///
/// - Validate user input (environment name format)
/// - Show progress updates to the user
/// - Format success/error messages for display
/// - Delegate business logic to application layer
///
/// # Architecture
///
/// This controller sits in the presentation layer and handles all user-facing
/// concerns. It delegates actual business logic to the application layer's
/// `DestroyCommandHandler`, maintaining clear separation of concerns.
pub struct DestroyCommandController {
factory: CommandHandlerFactory,
ctx: CommandContext,
progress: ProgressReporter,
}
impl DestroyCommandController {
/// Create a new destroy command controller
///
/// # Arguments
///
/// * `working_dir` - Root directory for environment data storage
/// * `user_output` - Shared user output service for consistent formatting
pub fn new(working_dir: PathBuf, user_output: Arc<Mutex<UserOutput>>) -> Self {
let factory = CommandHandlerFactory::new();
let ctx = factory.create_context(working_dir, user_output.clone());
let progress = ProgressReporter::new(user_output, DESTROY_WORKFLOW_STEPS);
Self {
factory,
ctx,
progress,
}
}
/// Execute the complete destroy workflow
///
/// Orchestrates all steps of the destroy command:
/// 1. Validate environment name
/// 2. Initialize dependencies
/// 3. Tear down infrastructure
/// 4. Complete with success message
///
/// # Arguments
///
/// * `environment_name` - The name of the environment to destroy
///
/// # Errors
///
/// Returns an error if:
/// - Environment name is invalid (format validation fails)
/// - Environment cannot be loaded from repository
/// - Infrastructure teardown fails
/// - Progress reporting encounters a poisoned mutex
///
/// # Returns
///
/// Returns `Ok(())` on success, or a `DestroySubcommandError` if any step fails.
#[allow(clippy::result_large_err)]
pub fn execute(&mut self, environment_name: &str) -> Result<(), DestroySubcommandError> {
let env_name = self.validate_environment_name(environment_name)?;
let handler = self.initialize_dependencies()?;
let _destroyed = self.tear_down_infrastructure(&handler, &env_name)?;
self.complete_workflow(environment_name)?;
Ok(())
}
/// Validate the environment name format
///
/// Shows progress to user and validates that the environment name
/// meets domain requirements (1-63 chars, alphanumeric + hyphens).
#[allow(clippy::result_large_err)]
fn validate_environment_name(
&mut self,
name: &str,
) -> Result<EnvironmentName, DestroySubcommandError> {
self.progress.start_step("Validating environment")?;
let env_name = EnvironmentName::new(name.to_string()).map_err(|source| {
DestroySubcommandError::InvalidEnvironmentName {
name: name.to_string(),
source,
}
})?;
self.progress
.complete_step(Some(&format!("Environment name validated: {name}")))?;
Ok(env_name)
}
/// Initialize application layer dependencies
///
/// Creates the application layer command handler with all required
/// dependencies (repository, clock, etc.).
#[allow(clippy::result_large_err)]
fn initialize_dependencies(&mut self) -> Result<DestroyCommandHandler, DestroySubcommandError> {
self.progress.start_step("Initializing dependencies")?;
let handler = self.factory.create_destroy_handler(&self.ctx);
self.progress.complete_step(None)?;
Ok(handler)
}
/// Execute infrastructure teardown via application layer
///
/// Delegates to the application layer `DestroyCommandHandler` to
/// orchestrate the actual infrastructure destruction workflow.
#[allow(clippy::result_large_err)]
fn tear_down_infrastructure(
&mut self,
handler: &DestroyCommandHandler,
env_name: &EnvironmentName,
) -> Result<Environment<Destroyed>, DestroySubcommandError> {
self.progress.start_step("Tearing down infrastructure")?;
let destroyed = handler.execute(env_name).map_err(|source| {
DestroySubcommandError::DestroyOperationFailed {
name: env_name.to_string(),
source,
}
})?;
self.progress
.complete_step(Some("Infrastructure torn down"))?;
Ok(destroyed)
}
/// Complete the workflow with success message
///
/// Shows final success message to the user with workflow summary.
#[allow(clippy::result_large_err)]
fn complete_workflow(&mut self, name: &str) -> Result<(), DestroySubcommandError> {
self.progress
.complete(&format!("Environment '{name}' destroyed successfully"))?;
Ok(())
}
}
// ============================================================================
// PUBLIC ENTRY POINT
// ============================================================================
/// Handle the destroy command
///
/// This is a thin wrapper over `DestroyCommandController` that serves as
/// the public entry point for the destroy command.
///
/// # Arguments
///
/// * `environment_name` - The name of the environment to destroy
/// * `working_dir` - Root directory for environment data storage
/// * `user_output` - Shared user output service for consistent output formatting
///
/// # Errors
///
/// Returns an error if:
/// - Environment name is invalid (format validation fails)
/// - Environment cannot be loaded from repository
/// - Infrastructure teardown fails
/// - Progress reporting encounters a poisoned mutex
///
/// All errors include detailed context and actionable troubleshooting guidance.
///
/// # Returns
///
/// Returns `Ok(())` on success, or a `DestroySubcommandError` on failure.
///
/// # Example
///
/// ```rust
/// use std::path::Path;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::destroy;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// if let Err(e) = destroy::handle_destroy_command("test-env", Path::new("."), &user_output) {
/// eprintln!("Destroy failed: {e}");
/// eprintln!("Help: {}", e.help());
/// }
/// ```
#[allow(clippy::result_large_err)] // Error contains detailed context for user guidance
pub fn handle_destroy_command(
environment_name: &str,
working_dir: &std::path::Path,
user_output: &Arc<Mutex<UserOutput>>,
) -> Result<(), DestroySubcommandError> {
DestroyCommandController::new(working_dir.to_path_buf(), user_output.clone())
.execute(environment_name)
}
// ============================================================================
// TESTS
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
use crate::presentation::user_output::test_support::TestUserOutput;
use crate::presentation::user_output::VerbosityLevel;
use std::fs;
use tempfile::TempDir;
#[test]
fn it_should_return_error_for_invalid_environment_name() {
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path();
let user_output = TestUserOutput::wrapped(VerbosityLevel::Normal);
// Test with invalid environment name (contains underscore)
let result = handle_destroy_command("invalid_name", working_dir, &user_output);
assert!(result.is_err());
match result.unwrap_err() {
DestroySubcommandError::InvalidEnvironmentName { name, .. } => {
assert_eq!(name, "invalid_name");
}
other => panic!("Expected InvalidEnvironmentName, got: {other:?}"),
}
}
#[test]
fn it_should_return_error_for_empty_environment_name() {
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path();
let user_output = TestUserOutput::wrapped(VerbosityLevel::Normal);
let result = handle_destroy_command("", working_dir, &user_output);
assert!(result.is_err());
match result.unwrap_err() {
DestroySubcommandError::InvalidEnvironmentName { name, .. } => {
assert_eq!(name, "");
}
other => panic!("Expected InvalidEnvironmentName, got: {other:?}"),
}
}
#[test]
fn it_should_return_error_for_nonexistent_environment() {
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path();
let user_output = TestUserOutput::wrapped(VerbosityLevel::Normal);
// Try to destroy an environment that doesn't exist
let result = handle_destroy_command("nonexistent-env", working_dir, &user_output);
assert!(result.is_err());
// Should get DestroyOperationFailed because environment doesn't exist
match result.unwrap_err() {
DestroySubcommandError::DestroyOperationFailed { name, .. } => {
assert_eq!(name, "nonexistent-env");
}
other => panic!("Expected DestroyOperationFailed, got: {other:?}"),
}
}
#[test]
fn it_should_accept_valid_environment_name() {
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path();
let user_output = TestUserOutput::wrapped(VerbosityLevel::Normal);
// Create a mock environment directory to test validation
let env_dir = working_dir.join("test-env");
fs::create_dir_all(&env_dir).unwrap();
// Valid environment name should pass validation, but will fail
// at destroy operation since we don't have a real environment setup
let result = handle_destroy_command("test-env", working_dir, &user_output);
// Should fail at operation, not at name validation
if let Err(DestroySubcommandError::InvalidEnvironmentName { .. }) = result {
panic!("Should not fail at name validation for 'test-env'");
}
// Expected - valid name but operation fails or other errors acceptable in test context
}
}