Skip to main content

torrust_tracker_deployer_lib/presentation/cli/controllers/create/
router.rs

1//! Create Command Router
2//!
3//! This module handles the create command execution at the presentation layer,
4//! routing between different subcommands (environment creation or template generation).
5
6use std::path::Path;
7
8use crate::presentation::cli::dispatch::ExecutionContext;
9use crate::presentation::cli::input::cli::commands::CreateAction;
10
11use super::errors::CreateCommandError;
12
13/// Route the create command to its appropriate subcommand
14///
15/// This function routes between different create subcommands (environment, template, schema, or cli-schema).
16///
17/// # Arguments
18///
19/// * `action` - The create action to perform (environment creation, template generation, schema generation, or CLI schema generation)
20/// * `working_dir` - Root directory for environment data storage
21/// * `context` - Execution context providing access to application services
22///
23/// # Returns
24///
25/// Returns `Ok(())` on success, or a `CreateCommandError` on failure.
26///
27/// # Errors
28///
29/// Returns an error if the subcommand execution fails.
30#[allow(clippy::result_large_err)] // Error contains detailed context for user guidance
31pub async fn route_command(
32    action: CreateAction,
33    working_dir: &Path,
34    context: &ExecutionContext,
35) -> Result<(), CreateCommandError> {
36    match action {
37        CreateAction::Environment { env_file } => {
38            let output_format = context.output_format();
39            context
40                .container()
41                .create_environment_controller()
42                .execute(&env_file, working_dir, output_format)
43                .await
44                .map(|_| ()) // Convert Environment<Created> to ()
45                .map_err(CreateCommandError::Environment)
46        }
47        CreateAction::Template {
48            output_path,
49            provider,
50        } => {
51            let template_path = output_path.unwrap_or_else(CreateAction::default_template_path);
52            context
53                .container()
54                .create_template_controller()
55                .execute(&template_path, provider)
56                .await
57                .map_err(CreateCommandError::Template)
58        }
59        CreateAction::Schema { output_path } => context
60            .container()
61            .create_schema_controller()
62            .execute(output_path.as_ref())
63            .map_err(CreateCommandError::Schema),
64    }
65}