diff options
Diffstat (limited to 'src/bash.rs')
-rw-r--r-- | src/bash.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/bash.rs b/src/bash.rs new file mode 100644 index 0000000..059909f --- /dev/null +++ b/src/bash.rs @@ -0,0 +1,41 @@ +use std::{env, collections::HashMap, fs::read_to_string, process::{exit, Command}}; + +pub fn load_config() -> HashMap<String, String> { + + let config_path = env::var("CONFIG_PATH").unwrap_or_else(|_| String::from("config")); + let config = match read_to_string(&config_path) { + Ok(data) => data, + Err(err) => { + eprintln!("cannot load '{config_path}': {err}"); + exit(1); + }, + }; + + let mut map = HashMap::new(); + let lines = config.split("\n").into_iter(); + + for line in lines { + let mut parts = line.trim().split(" "); + let Some(route) = parts.next() else { continue }; + let Some(script) = parts.next() else { continue }; + + println!("adding entry {route} => {script}"); + map.insert(route.to_owned(), script.to_owned()); + } + + map +} + +pub fn handle_script(script: &str, body: Option<&String>) -> Result<String, String> { + let mut command = Command::new(script); + if let Some(body) = body { + command.args([body]); + } + + let output = match command.output() { + Ok(o) => o, + Err(err) => return Err(format!("{err}")), + }; + + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) +} |