-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.rs
More file actions
78 lines (68 loc) · 2.78 KB
/
Copy pathtemplate.rs
File metadata and controls
78 lines (68 loc) · 2.78 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
//! Template Generation Subcommand
//!
//! This module handles the template generation subcommand for creating
//! configuration file templates with placeholder values.
use std::path::Path;
use std::sync::{Arc, Mutex};
use crate::application::command_handlers::create::config::EnvironmentCreationConfig;
use crate::presentation::user_output::UserOutput;
use super::super::errors::CreateSubcommandError;
/// Handle template generation
///
/// This function generates a configuration template file with placeholder values
/// that users can edit to create their own environment configurations.
///
/// # Arguments
///
/// * `output_path` - Path where the template file should be created
/// * `user_output` - Shared user output service for consistent output formatting
///
/// # Returns
///
/// Returns `Ok(())` on success, or a `CreateSubcommandError` on failure.
///
/// # Errors
///
/// Returns an error if template file creation fails.
///
/// # Panics
///
/// Panics if the tokio runtime cannot be created. This is a critical system
/// failure that prevents any async operations from running.
#[allow(clippy::result_large_err)] // Error contains detailed context for user guidance
pub fn handle_template_generation(
output_path: &Path,
user_output: &Arc<Mutex<UserOutput>>,
) -> Result<(), CreateSubcommandError> {
// Lock user output for progress messages
let mut output = user_output
.lock()
.map_err(|_| CreateSubcommandError::UserOutputLockFailed)?;
output.progress("Generating configuration template...");
// Call existing domain method - template generation implemented in PR #48
// This is async, so we need to use tokio runtime
tokio::runtime::Runtime::new()
.expect("Failed to create tokio runtime")
.block_on(async {
EnvironmentCreationConfig::generate_template_file(output_path)
.await
.map_err(|source| CreateSubcommandError::TemplateGenerationFailed { source })
})?;
output.success(&format!(
"Configuration template generated: {}",
output_path.display()
));
output.blank_line();
output.steps(
"Next steps:",
&[
"Edit the template file and replace placeholder values:\n - REPLACE_WITH_ENVIRONMENT_NAME: Choose a unique environment name (e.g., 'dev', 'staging')\n - REPLACE_WITH_SSH_PRIVATE_KEY_PATH: Path to your SSH private key\n - REPLACE_WITH_SSH_PUBLIC_KEY_PATH: Path to your SSH public key",
"Review default values:\n - username: 'torrust' (can be changed if needed)\n - port: 22 (standard SSH port)",
&format!(
"Create the environment:\n torrust-tracker-deployer create environment --env-file {}",
output_path.display()
),
],
);
Ok(())
}