You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tourmaline/src/utils.rs

57 lines
1.3 KiB
Rust

use std::path::Path;
use std::{env, path::PathBuf};
use tokio::fs;
use crate::error::AppResult;
use crate::task::base_task::ALL_BASE_TASKS;
const DEFAULT_CONFIG_DIR: &str = "/etc";
lazy_static::lazy_static! {
pub static ref CFG_PATH: PathBuf = env::var("TRM_CFG_PATH").map(PathBuf::from).unwrap_or_else(|_| PathBuf::from(DEFAULT_CONFIG_DIR).join("tourmaline"));
}
pub async fn generate_script_files<P: AsRef<Path>>(output: P) -> AppResult<()> {
let output = output.as_ref();
2 years ago
for task in &*ALL_BASE_TASKS {
let key_data = task.key_data();
let name = key_data.task_name;
let script_dir = output.join(name);
if !script_dir.exists() {
fs::create_dir_all(&script_dir).await?;
}
let up_path = output.join("up.nu");
let down_path = output.join("down.nu");
fs::write(
&up_path,
format!(
r#"# Applies all system changes of `{name}`
def main [cfg] {{
echo "Executing up task `{name}` with config" $cfg
}}
"#
),
)
.await?;
fs::write(
&down_path,
format!(
r#"# Reverts all system changes of `{name}`
def main [cfg] {{
echo "Executing up task `{name}` with config" $cfg
}}
"#
)
.trim(),
)
.await?;
}
Ok(())
}