44//! deployment environments from configuration files.
55
66use std:: path:: Path ;
7- use std:: sync:: Arc ;
87
98use crate :: application:: command_handlers:: create:: config:: EnvironmentCreationConfig ;
10- use crate :: application:: command_handlers:: create:: CreateCommandHandler ;
119use crate :: domain:: Environment ;
12- use crate :: presentation:: commands:: context:: { report_error, CommandContext } ;
10+ use crate :: presentation:: commands:: context:: report_error;
11+ use crate :: presentation:: commands:: factory:: CommandHandlerFactory ;
1312use crate :: presentation:: user_output:: UserOutput ;
1413
1514use super :: super :: config_loader:: ConfigLoader ;
@@ -47,17 +46,15 @@ pub fn handle_environment_creation(
4746 env_file : & Path ,
4847 working_dir : & Path ,
4948) -> Result < ( ) , CreateSubcommandError > {
50- let mut ctx = CommandContext :: new ( working_dir. to_path_buf ( ) ) ;
49+ let factory = CommandHandlerFactory :: new ( ) ;
50+ let mut ctx = factory. create_context ( working_dir. to_path_buf ( ) ) ;
5151
5252 // Step 1: Load configuration
5353 let config = load_configuration ( ctx. output ( ) , env_file) ?;
5454
55- // Step 2: Execute command (split borrow to avoid conflict)
56- let environment = {
57- let repository = ctx. repository ( ) . clone ( ) ;
58- let clock = ctx. clock ( ) . clone ( ) ;
59- execute_create_command ( ctx. output ( ) , config, repository, clock) ?
60- } ;
55+ // Step 2: Execute command (create handler before borrowing output)
56+ let command_handler = factory. create_create_handler ( & ctx) ;
57+ let environment = execute_create_command ( ctx. output ( ) , command_handler, config) ?;
6158
6259 // Step 3: Display results
6360 display_creation_results ( ctx. output ( ) , & environment) ;
@@ -105,16 +102,14 @@ fn load_configuration(
105102/// Execute the create command with the given configuration
106103///
107104/// This step handles:
108- /// - Creating the command handler with dependencies
109- /// - Executing the create command
105+ /// - Executing the create command with the given handler
110106/// - Handling command execution errors
111107///
112108/// # Arguments
113109///
114110/// * `output` - User output for progress messages
111+ /// * `command_handler` - Pre-created command handler
115112/// * `config` - Validated environment creation configuration
116- /// * `repository` - Repository for environment persistence
117- /// * `clock` - Clock for timing operations
118113///
119114/// # Returns
120115///
@@ -125,17 +120,14 @@ fn load_configuration(
125120/// Returns an error if command execution fails (e.g., environment already exists).
126121fn execute_create_command (
127122 output : & mut UserOutput ,
123+ command_handler : crate :: application:: command_handlers:: CreateCommandHandler ,
128124 config : EnvironmentCreationConfig ,
129- repository : Arc < dyn crate :: domain:: environment:: repository:: EnvironmentRepository > ,
130- clock : Arc < dyn crate :: shared:: Clock > ,
131125) -> Result < Environment , CreateSubcommandError > {
132126 output. progress ( & format ! (
133127 "Creating environment '{}'..." ,
134128 config. environment. name
135129 ) ) ;
136130
137- let command_handler = CreateCommandHandler :: new ( repository, clock) ;
138-
139131 output. progress ( "Validating configuration and creating environment..." ) ;
140132
141133 #[ allow( clippy:: manual_inspect) ]
@@ -449,10 +441,10 @@ mod tests {
449441 let loader = ConfigLoader ;
450442 let config = loader. load_from_file ( & config_path) . unwrap ( ) ;
451443
452- let ctx = CommandContext :: new ( temp_dir . path ( ) . to_path_buf ( ) ) ;
453- let repository = ctx . repository ( ) . clone ( ) ;
454- let clock = ctx . clock ( ) . clone ( ) ;
455- let result = execute_create_command ( & mut output, config , repository , clock ) ;
444+ let factory = CommandHandlerFactory :: new ( ) ;
445+ let ctx = factory . create_context ( temp_dir . path ( ) . to_path_buf ( ) ) ;
446+ let command_handler = factory . create_create_handler ( & ctx ) ;
447+ let result = execute_create_command ( & mut output, command_handler , config ) ;
456448
457449 assert ! ( result. is_ok( ) , "Should execute command successfully" ) ;
458450 let environment = result. unwrap ( ) ;
@@ -485,21 +477,17 @@ mod tests {
485477 let loader = ConfigLoader ;
486478 let config = loader. load_from_file ( & config_path) . unwrap ( ) ;
487479
488- let ctx = CommandContext :: new ( temp_dir. path ( ) . to_path_buf ( ) ) ;
489- let repository = ctx. repository ( ) . clone ( ) ;
490- let clock = ctx. clock ( ) . clone ( ) ;
480+ let factory = CommandHandlerFactory :: new ( ) ;
481+ let ctx = factory. create_context ( temp_dir. path ( ) . to_path_buf ( ) ) ;
491482
492483 // Create environment first time
493- let result1 = execute_create_command (
494- & mut output,
495- config. clone ( ) ,
496- repository. clone ( ) ,
497- clock. clone ( ) ,
498- ) ;
484+ let command_handler1 = factory. create_create_handler ( & ctx) ;
485+ let result1 = execute_create_command ( & mut output, command_handler1, config. clone ( ) ) ;
499486 assert ! ( result1. is_ok( ) , "First execution should succeed" ) ;
500487
501488 // Try to create same environment again
502- let result2 = execute_create_command ( & mut output, config, repository, clock) ;
489+ let command_handler2 = factory. create_create_handler ( & ctx) ;
490+ let result2 = execute_create_command ( & mut output, command_handler2, config) ;
503491 assert ! ( result2. is_err( ) , "Second execution should fail" ) ;
504492
505493 match result2. unwrap_err ( ) {
@@ -539,14 +527,13 @@ mod tests {
539527 fs:: write ( & config_path, config_json) . unwrap ( ) ;
540528
541529 // Create environment
542- let ctx = CommandContext :: new ( temp_dir. path ( ) . to_path_buf ( ) ) ;
530+ let factory = CommandHandlerFactory :: new ( ) ;
531+ let ctx = factory. create_context ( temp_dir. path ( ) . to_path_buf ( ) ) ;
543532 let loader = ConfigLoader ;
544533 let config = loader. load_from_file ( & config_path) . unwrap ( ) ;
545534 let mut quiet_output = UserOutput :: new ( VerbosityLevel :: Quiet ) ;
546- let repository = ctx. repository ( ) . clone ( ) ;
547- let clock = ctx. clock ( ) . clone ( ) ;
548- let environment =
549- execute_create_command ( & mut quiet_output, config, repository, clock) . unwrap ( ) ;
535+ let command_handler = factory. create_create_handler ( & ctx) ;
536+ let environment = execute_create_command ( & mut quiet_output, command_handler, config) . unwrap ( ) ;
550537
551538 // Test display function with custom output
552539 let stderr_buf = Vec :: new ( ) ;
0 commit comments