Skip to content

Commit

Permalink
fix(template): improve error handling for files and folders
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Oct 21, 2023
1 parent 10a8a90 commit 7d09aa5
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/util/template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use color_eyre::{eyre::Result, Report};
use color_eyre::eyre::ContextCompat;
use color_eyre::eyre::WrapErr;

use colorsys::Hsl;
use regex::Regex;
Expand All @@ -8,6 +10,7 @@ use std::str;

use std::fs::read_to_string;
use std::fs::OpenOptions;
use std::fs::create_dir_all;
use std::io::Write;
use std::path::PathBuf;

Expand Down Expand Up @@ -105,8 +108,9 @@ impl Template {
continue;
}


let mut data = read_to_string(&input_path_absolute)?;

replace_matches(
&regexvec,
&mut data,
Expand All @@ -115,11 +119,24 @@ impl Template {
default_scheme,
);

debug!("Trying to write the {} template to {}", name, output_path_absolute.display());

if !output_path_absolute.exists() {
error!("The <b><red>{}</> folder doesnt exist, trying to create...", &output_path_absolute.display());
let parent_folder = &output_path_absolute.parent().wrap_err("Could not get the parent of the output path.")?;
println!("{}", parent_folder.display());
create_dir_all(&parent_folder).wrap_err(format!("Failed to create the {} folders.", &output_path_absolute.display()));
}

let mut output_file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&output_path_absolute)?;
.create(true)
.truncate(true)
.write(true)
.open(&output_path_absolute)?;

if output_file.metadata()?.permissions().readonly() {
error!("The <b><red>{}</> file is Read-Only", &output_path_absolute.display());
}

output_file.write_all(data.as_bytes())?;
success!(
Expand Down

0 comments on commit 7d09aa5

Please sign in to comment.