From 268359f105422b79771fa5532961a922c0ef67ff Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sun, 3 Nov 2024 13:34:16 -0500 Subject: [PATCH] refactor: consolidated and simplified minijinja env/template processing --- src/cmd/fetchpost.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/cmd/fetchpost.rs b/src/cmd/fetchpost.rs index cef2411c8..fea3d7cdf 100644 --- a/src/cmd/fetchpost.rs +++ b/src/cmd/fetchpost.rs @@ -654,18 +654,6 @@ pub fn run(argv: &[&str]) -> CliResult<()> { report_wtr.write_byte_record(&report_headers)?; } - let mut template_content = String::new(); - let mut build_payload = false; - let payload_env_option = if let Some(template_file) = args.flag_payload_tpl { - template_content = fs::read_to_string(template_file)?; - let mut env = Environment::new(); - env.add_template("template", &template_content)?; - build_payload = true; - Some(env) - } else { - None - }; - // amortize memory allocations // why optimize for mem & speed, when we're just doing single-threaded, throttled URL fetches? // we still optimize since fetch is backed by a memoized cache (in memory or Redis, when --redis @@ -712,10 +700,18 @@ pub fn run(argv: &[&str]) -> CliResult<()> { .collect(); let debug_flag = log_enabled!(Debug); + + let mut template_content = String::new(); + let build_payload: bool; let mut rendered_json: Value; - let payload_env = if build_payload { - payload_env_option.unwrap() + let payload_env = if let Some(template_file) = args.flag_payload_tpl { + template_content = fs::read_to_string(template_file)?; + let mut env = Environment::new(); + env.add_template("template", &template_content)?; + build_payload = true; + env } else { + build_payload = false; Environment::empty() };