Browse Source

Add the rev_labels template formatter

pull/49/head
Rodolphe Bréard 4 years ago
parent
commit
2fa21bb7b7
  1. 61
      acmed/src/template.rs
  2. 10
      man/en/acmed.toml.5

61
acmed/src/template.rs

@ -1,12 +1,73 @@
use acme_common::error::Error; use acme_common::error::Error;
use serde::Serialize; use serde::Serialize;
use serde_json::Value;
use tinytemplate::TinyTemplate; use tinytemplate::TinyTemplate;
macro_rules! default_format {
($value: ident, $output: ident) => {{
$output.push_str(&$value.to_string());
Ok(())
}};
}
fn formatter_rev_labels(value: &Value, output: &mut String) -> tinytemplate::error::Result<()> {
match value {
Value::Null => Ok(()),
Value::Bool(v) => default_format!(v, output),
Value::Number(v) => default_format!(v, output),
Value::String(v) => {
let s = v.rsplit(".").collect::<Vec<&str>>().join(".");
output.push_str(&s);
Ok(())
}
_ => Ok(()),
}
}
pub fn render_template<T>(template: &str, data: &T) -> Result<String, Error> pub fn render_template<T>(template: &str, data: &T) -> Result<String, Error>
where where
T: Serialize, T: Serialize,
{ {
let mut reg = TinyTemplate::new(); let mut reg = TinyTemplate::new();
reg.add_formatter("rev_labels", formatter_rev_labels);
reg.add_template("reg", template)?; reg.add_template("reg", template)?;
Ok(reg.render("reg", data)?) Ok(reg.render("reg", data)?)
} }
#[cfg(test)]
mod tests {
use super::render_template;
use serde::Serialize;
#[derive(Serialize)]
struct TplTest {
foo: String,
bar: u64,
}
#[test]
fn test_basic_template() {
let c = TplTest {
foo: String::from("test"),
bar: 42,
};
let tpl = "This is { foo } { bar -} !";
let rendered = render_template(tpl, &c);
assert!(rendered.is_ok());
let rendered = rendered.unwrap();
assert_eq!(rendered, "This is test 42!");
}
#[test]
fn test_formatter_rev_labels() {
let c = TplTest {
foo: String::from("mx1.example.org"),
bar: 42,
};
let tpl = "{ foo } - { foo | rev_labels }";
let rendered = render_template(tpl, &c);
assert!(rendered.is_ok());
let rendered = rendered.unwrap();
assert_eq!(rendered, "mx1.example.org - org.example.mx1");
}
}

10
man/en/acmed.toml.5

@ -669,6 +669,16 @@ For example,
and and
.Dq 40s20h4h2s .Dq 40s20h4h2s
both represents a period of one day and forty-two seconds. both represents a period of one day and forty-two seconds.
.Sh TEMPLATE FORMATTERS
In addition the the formatters provided by default by TinyTemplate, ACMEd provides the following formatters:
.Bl -tag
.It Pa rev_labels
Reverts the labels of a domain name (eg:
.Dq mx1.example.org
becomes
.Dq org.example.mx1
).
.El
.Sh FILES .Sh FILES
.Bl -tag .Bl -tag
.It Pa /var/lib/acmed/accounts .It Pa /var/lib/acmed/accounts

Loading…
Cancel
Save