tests_fuzz/utils/
config.rs1use std::path::PathBuf;
16
17use common_telemetry::tracing::info;
18use serde::Serialize;
19use snafu::ResultExt;
20use tinytemplate::TinyTemplate;
21use tokio::fs::File;
22use tokio::io::AsyncWriteExt;
23
24use crate::error;
25use crate::error::Result;
26
27pub fn get_conf_path() -> PathBuf {
29 let mut root_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
30 root_path.push("conf");
31 root_path
32}
33
34pub fn render_config_file<C: Serialize>(template_path: &str, context: &C) -> String {
36 let mut tt = TinyTemplate::new();
37 let template = std::fs::read_to_string(template_path).unwrap();
38 tt.add_template(template_path, &template).unwrap();
39 tt.render(template_path, context).unwrap()
40}
41
42pub async fn write_config_file<C: Serialize>(
44 template_path: &str,
45 context: &C,
46 output_path: &str,
47) -> Result<()> {
48 info!("template_path: {template_path}, output_path: {output_path}");
49 let content = render_config_file(template_path, context);
50 let mut config_file = File::create(output_path)
51 .await
52 .context(error::CreateFileSnafu { path: output_path })?;
53 config_file
54 .write_all(content.as_bytes())
55 .await
56 .context(error::WriteFileSnafu { path: output_path })?;
57 Ok(())
58}