libkelp/lib/util/
scripts.rs

1use crate::lib::structs::script::Script;
2use crate::lib::util::exec::get_root_exec_program;
3use anyhow::Context;
4use kelpdot_macros::{debug_print, red};
5use std::process::Command;
6pub fn run_script(root: String, script: Script) -> anyhow::Result<()> {
7    if let Some(run) = script.elevated {
8        if run {
9            debug_print!("Getting elevator for script {}", script);
10            let elevator = get_root_exec_program()?;
11            Command::new(elevator.clone()) // Use SH because some systems symlinks it to bash / zsh / ash
12                .arg("sh")
13                .arg(&format!("{}/{}", root, script.path))
14                .arg(root)
15                .status()
16                .with_context(|| {
17                    red!(
18                        "Failed to execute script {} with elevator {}",
19                        script,
20                        elevator
21                    )
22                })?;
23        } else {
24            run_script(
25                root,
26                Script {
27                    path: script.path,
28                    elevated: None,
29                },
30            )?;
31        }
32    } else {
33        Command::new("sh") // Use SH because some systems symlinks it to bash / zsh / ash
34            .arg(&format!("{}/{}", root, script.path))
35            .arg(root)
36            .status()
37            .with_context(|| red!("Failed to run script {}", script))?;
38    }
39    Ok(())
40}