Make template renders inherit perms of template

This commit is contained in:
2022-06-09 19:45:12 +02:00
parent ece7f1c92a
commit 263be3ebfc
2 changed files with 195 additions and 157 deletions

View File

@ -10,7 +10,8 @@ use std::io::ErrorKind;
use std::path::PathBuf;
use std::process::Command;
use std::str::from_utf8;
use tokio::fs::{copy, create_dir, read_dir, read_to_string, write};
use tokio::fs::{copy, create_dir, read_dir, read_to_string, File};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::join;
pub const TEMPLATE_EXTENSION: &str = "tpl";
@ -115,10 +116,22 @@ async fn file(cfg: &Config, env: &Env, relative: PathBuf) -> Result<(), Error> {
if template_path.extension() == Some(OsStr::new(TEMPLATE_EXTENSION)) {
// perform templating
let file_str = read_to_string(&template_path)
let mut template_file = File::open(&template_path)
.await
.with_location(&template_path)?;
let mut file_str = String::new();
template_file
.read_to_string(&mut file_str)
.await
.with_location(&template_path)?;
let permissions = template_file
.metadata()
.await
.with_location(&template_path)?
.permissions();
let mut rendered = Vec::<u8>::new();
parse_template(&file_str)
.with_location(&template_path)?
@ -129,8 +142,19 @@ async fn file(cfg: &Config, env: &Env, relative: PathBuf) -> Result<(), Error> {
// remove template file extension
new_path.set_extension("");
let mut rendered_file = File::create(&new_path).await.with_location(&new_path)?;
// write the rendered file
write(&new_path, &rendered).await.with_location(&new_path)?;
rendered_file
.write_all(rendered.as_bytes())
.await
.with_location(&new_path)?;
// make sure the permissions match the original
rendered_file
.set_permissions(permissions)
.await
.with_location(&new_path)?;
} else {
// else just copy the file
debug!("copying {:?}", template_path);