Skip to content

Commit 5e3c148

Browse files
committed
fix: display undeclared variables when rendering templates
1 parent 7e31e00 commit 5e3c148

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/dots.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,19 @@ impl Dot {
145145
// Single file : inject vars and write to .dots/
146146
if source.is_file() {
147147
fs::create_dir_all(&target.parent().unwrap())?;
148-
if let Ok(content) = vars.to_dot(source) {
149-
let permissions = fs::metadata(source)?.permissions();
150-
let mut dot_copy = File::create(&target)?;
151-
dot_copy.write_all(content.as_bytes())?;
152-
dot_copy.set_permissions(permissions)?;
153-
} else {
154-
// Something went wrong parsing or reading the source path,
155-
// We just copy the file in place
156-
fs::copy(source, target)?;
148+
match vars.to_dot(source) {
149+
Ok(content) => {
150+
let permissions = fs::metadata(source)?.permissions();
151+
let mut dot_copy = File::create(&target)?;
152+
dot_copy.write_all(content.as_bytes())?;
153+
dot_copy.set_permissions(permissions)?;
154+
}
155+
Err(err) => {
156+
// Something went wrong parsing or reading the source path,
157+
// We just copy the file in place
158+
fs::copy(source, target)?;
159+
eprintln!("{err:?}")
160+
}
157161
}
158162
} else if source.is_dir() {
159163
fs::create_dir_all(target)?;

src/templating.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::gpg::{Gpg, GPG_PREFIX};
2-
use anyhow::{anyhow, Context, Result};
2+
use anyhow::{anyhow, Result};
33
use colored::Colorize;
44
use std::collections::HashMap;
55
use std::fs::File;
66
use std::io::prelude::*;
77
use std::io::BufReader;
88
use std::path::{Path, PathBuf};
9+
use tera::Tera;
910

1011
#[derive(Clone, Debug, Default)]
1112
pub(crate) struct Variables {
@@ -82,8 +83,11 @@ impl Variables {
8283
context.insert(k.to_owned(), v);
8384
});
8485

85-
tera::Tera::one_off(&contents, &context, false)
86-
.context("Failed to apply templating to file:")
86+
let mut tera = Tera::default();
87+
let filename = path.as_os_str().to_str().expect("Non UTF8 filename");
88+
89+
tera.add_raw_template(filename, &contents)?;
90+
tera.render(filename, &context).map_err(Into::into)
8791
}
8892

8993
pub(crate) fn resolve_ref(&mut self) {

0 commit comments

Comments
 (0)