1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::process::{
    Command,
    exit, 
};

pub fn run_command(command_name: &str, command_args: &[&str], error_message: &str, directory: Option<&str>) {
    let mut command_handle = Command::new(command_name)
        .args(command_args)
        .current_dir(directory.unwrap_or("."))
        .spawn()
        .expect(error_message);
    command_handle.wait().expect(error_message);
}

pub fn run_command_capture_output(command_name: &str, command_args: &[&str], error_message: &str, directory: Option<&str>) -> String {
    let command_output = Command::new(command_name)
        .args(command_args)
        .current_dir(directory.unwrap_or("."))
        .output()
        .expect(error_message);
    String::from_utf8_lossy(&command_output.stdout).to_string()
}

pub fn run_command_silent(command_name: &str, command_args: &[&str], error_message: &str, directory: Option<&str>) {
    Command::new(command_name)
        .args(command_args)
        .current_dir(directory.unwrap_or("."))
        .output()
        .expect(error_message);
}

pub fn exit_gracefully() {
    exit(0);
}