1- //! Create Subcommand Handler
1+ //! Environment Creation Subcommand
22//!
3- //! This module handles the create subcommand execution at the presentation layer,
4- //! including configuration file loading, argument processing, user interaction,
5- //! and command execution.
3+ //! This module handles the environment creation subcommand for creating
4+ //! deployment environments from configuration files.
65
76use std:: path:: Path ;
87use std:: sync:: Arc ;
@@ -11,99 +10,11 @@ use std::time::Duration;
1110use crate :: application:: command_handlers:: create:: CreateCommandHandler ;
1211use crate :: domain:: config:: EnvironmentCreationConfig ;
1312use crate :: infrastructure:: persistence:: repository_factory:: RepositoryFactory ;
14- use crate :: presentation:: cli:: commands:: CreateAction ;
1513use crate :: presentation:: user_output:: { UserOutput , VerbosityLevel } ;
1614use crate :: shared:: { Clock , SystemClock } ;
1715
18- use super :: config_loader:: ConfigLoader ;
19- use super :: errors:: CreateSubcommandError ;
20-
21- /// Handle the create command with its subcommands
22- ///
23- /// This function routes between different create subcommands (environment or template).
24- ///
25- /// # Arguments
26- ///
27- /// * `action` - The create action to perform (environment creation or template generation)
28- /// * `working_dir` - Root directory for environment data storage
29- ///
30- /// # Returns
31- ///
32- /// Returns `Ok(())` on success, or a `CreateSubcommandError` on failure.
33- ///
34- /// # Errors
35- ///
36- /// Returns an error if the subcommand execution fails.
37- #[ allow( clippy:: result_large_err) ] // Error contains detailed context for user guidance
38- pub fn handle_create_command (
39- action : CreateAction ,
40- working_dir : & Path ,
41- ) -> Result < ( ) , CreateSubcommandError > {
42- match action {
43- CreateAction :: Environment { env_file } => {
44- handle_environment_creation ( & env_file, working_dir)
45- }
46- CreateAction :: Template { output_path } => {
47- let template_path = output_path. unwrap_or_else ( CreateAction :: default_template_path) ;
48- handle_template_generation ( & template_path)
49- }
50- }
51- }
52-
53- /// Handle template generation
54- ///
55- /// This function generates a configuration template file with placeholder values
56- /// that users can edit to create their own environment configurations.
57- ///
58- /// # Arguments
59- ///
60- /// * `output_path` - Path where the template file should be created
61- ///
62- /// # Returns
63- ///
64- /// Returns `Ok(())` on success, or a `CreateSubcommandError` on failure.
65- ///
66- /// # Errors
67- ///
68- /// Returns an error if template file creation fails.
69- #[ allow( clippy:: result_large_err) ] // Error contains detailed context for user guidance
70- fn handle_template_generation ( output_path : & Path ) -> Result < ( ) , CreateSubcommandError > {
71- // Create user output for progress messages
72- let mut output = UserOutput :: new ( VerbosityLevel :: Normal ) ;
73-
74- output. progress ( "Generating configuration template..." ) ;
75-
76- // Call existing domain method - template generation implemented in PR #48
77- // This is async, so we need to use tokio runtime
78- tokio:: runtime:: Runtime :: new ( )
79- . expect ( "Failed to create tokio runtime" )
80- . block_on ( async {
81- EnvironmentCreationConfig :: generate_template_file ( output_path)
82- . await
83- . map_err ( CreateSubcommandError :: TemplateGenerationFailed )
84- } ) ?;
85-
86- output. success ( & format ! (
87- "Configuration template generated: {}" ,
88- output_path. display( )
89- ) ) ;
90- println ! ( ) ;
91- println ! ( "Next steps:" ) ;
92- println ! ( "1. Edit the template file and replace placeholder values:" ) ;
93- println ! ( " - REPLACE_WITH_ENVIRONMENT_NAME: Choose a unique environment name (e.g., 'dev', 'staging')" ) ;
94- println ! ( " - REPLACE_WITH_SSH_PRIVATE_KEY_PATH: Path to your SSH private key" ) ;
95- println ! ( " - REPLACE_WITH_SSH_PUBLIC_KEY_PATH: Path to your SSH public key" ) ;
96- println ! ( "2. Review default values:" ) ;
97- println ! ( " - username: 'torrust' (can be changed if needed)" ) ;
98- println ! ( " - port: 22 (standard SSH port)" ) ;
99- println ! ( "3. Create the environment:" ) ;
100- println ! (
101- " torrust-tracker-deployer create environment --env-file {}" ,
102- output_path. display( )
103- ) ;
104-
105- Ok ( ( ) )
106- }
16+ use super :: super :: config_loader:: ConfigLoader ;
17+ use super :: super :: errors:: CreateSubcommandError ;
10718
10819/// Handle environment creation from configuration file
10920///
@@ -135,7 +46,7 @@ fn handle_template_generation(output_path: &Path) -> Result<(), CreateSubcommand
13546/// loaded, parsed, validated, or if the create command execution fails.
13647/// All errors include detailed context and actionable troubleshooting guidance.
13748#[ allow( clippy:: result_large_err) ] // Error contains detailed context for user guidance
138- fn handle_environment_creation (
49+ pub fn handle_environment_creation (
13950 env_file : & Path ,
14051 working_dir : & Path ,
14152) -> Result < ( ) , CreateSubcommandError > {
@@ -369,3 +280,4 @@ mod tests {
369280 ) ;
370281 }
371282}
283+
0 commit comments