diff --git a/cuddle/src/cli/subcommands/init.rs b/cuddle/src/cli/subcommands/init.rs index aacc9ec..74610d1 100644 --- a/cuddle/src/cli/subcommands/init.rs +++ b/cuddle/src/cli/subcommands/init.rs @@ -3,6 +3,7 @@ use std::fs::{create_dir_all, read, read_dir}; use std::io::Write; use std::path::PathBuf; +use anyhow::Context; use clap::{ArgMatches, Command}; use walkdir::WalkDir; @@ -178,8 +179,16 @@ pub fn execute_init(exe_submatch: &ArgMatches, _cli: CuddleCli) -> anyhow::Resul } tracing::info!("writing to: {}", new_path.display()); - let new_content = - replace_with_variables(&std::fs::read_to_string(entry_path)?, &template)?; + + let old_content = match std::fs::read_to_string(entry_path) { + Ok(e) => e, + Err(e) => { + tracing::debug!("found invalid file possibly with invalid utf8: {}", e); + std::fs::copy(entry_path, new_path).context("failed to write file")?; + continue; + } + }; + let new_content = replace_with_variables(&old_content, &template)?; std::fs::write(new_path, new_content.as_bytes())?; }