added documentation

This commit is contained in:
2026-03-16 17:55:10 +01:00
parent 7aa12f1a31
commit e72fdd9fcb
6 changed files with 119 additions and 39 deletions

View File

@@ -20,23 +20,32 @@ use crossterm::{
};
use ratatui::{backend::CrosstermBackend, Terminal};
/// Helper to automatically determine the output file path based on common naming conventions.
fn determine_output_path(input: &Path) -> PathBuf {
let file_name = input.file_name().unwrap_or_default().to_string_lossy();
// .env.example -> .env
if file_name == ".env.example" {
return input.with_file_name(".env");
}
// docker-compose.yml -> docker-compose.override.yml
if file_name == "docker-compose.yml" {
return input.with_file_name("docker-compose.override.yml");
}
if file_name == "docker-compose.yaml" {
return input.with_file_name("docker-compose.override.yaml");
}
// config.example.json -> config.json
if file_name.ends_with(".example.json") {
return input.with_file_name(file_name.replace(".example.json", ".json"));
}
if file_name.ends_with(".template.json") {
return input.with_file_name(file_name.replace(".template.json", ".json"));
}
// Fallback: append .out to the extension
input.with_extension(format!(
"{}.out",
input.extension().unwrap_or_default().to_string_lossy()
@@ -44,6 +53,7 @@ fn determine_output_path(input: &Path) -> PathBuf {
}
fn main() -> Result<(), Box<dyn Error>> {
// Parse CLI arguments
let args = cli::parse();
let input_path = args.input;
@@ -52,13 +62,16 @@ fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}
// Detect format and select appropriate handler
let format_type = detect_format(&input_path, args.format);
let handler = get_handler(format_type);
// Determine where to save the result
let output_path = args
.output
.unwrap_or_else(|| determine_output_path(&input_path));
// Initial parsing of the template file
let mut vars = handler.parse(&input_path).unwrap_or_else(|err| {
println!("Error parsing input file: {}", err);
vec![]
@@ -72,22 +85,27 @@ fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}
// Merge values from an existing output file if it exists
if let Err(e) = handler.merge(&output_path, &mut vars) {
println!("Warning: Could not merge existing output file: {}", e);
}
// Load user configuration and initialize application state
let config = load_config();
let mut app = App::new(vars);
// Initialize terminal into raw mode and enter alternate screen
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// Instantiate the runner and start the application loop
let mut runner = AppRunner::new(&mut terminal, &mut app, &config, &output_path, handler.as_ref());
let res = runner.run();
// Clean up terminal state on exit
disable_raw_mode()?;
execute!(
terminal.backend_mut(),