use std::process::Command;
use itertools::Itertools;
use redgold_schema::{error_info, ErrorInfoContext, RgResult, SafeOption};
use redgold_schema::errors::EnhanceErrorInfo;
pub fn run_cmd(cmd: impl Into<String>, args: Vec<impl Into<String>>) -> (String, String) {
let mut echo_hello = Command::new(cmd.into());
for arg in args {
echo_hello.arg(arg.into());
}
let hello_1 = echo_hello.output().expect("Ouput from command failure");
let string1 = String::from_utf8(hello_1.stdout).expect("String decode failure");
let string2 = String::from_utf8(hello_1.stderr).expect("String decode failure");
(string1, string2)
}
pub fn run_cmd_safe(cmd: impl Into<String>, args: Vec<impl Into<String>>) -> RgResult<(String, String)> {
let program = cmd.into();
let mut command = Command::new(program.clone());
for arg in args {
command.arg(arg.into());
}
let cmd_output = command.output().error_info("Ouput from command failure ")
.add(program.clone())?;
let stdout = String::from_utf8(cmd_output.stdout).error_info("stdout String decode failure ")
.add(program.clone())?;
let stderr = String::from_utf8(cmd_output.stderr).error_info("stderr String decode failure ")
.add(program.clone())?;
Ok((stdout, stderr))
}
pub async fn run_cmd_safe_async(cmd: impl Into<String>, args: Vec<impl Into<String>>) -> RgResult<(String, String)> {
let program = cmd.into();
let mut command = tokio::process::Command::new(program.clone());
for arg in args {
command.arg(arg.into());
}
let cmd_output = command.output().await.error_info("Ouput from command failure ")
.add(program.clone())?;
let stdout = String::from_utf8(cmd_output.stdout).error_info("stdout String decode failure ")
.add(program.clone())?;
let stderr = String::from_utf8(cmd_output.stderr).error_info("stderr String decode failure ")
.add(program.clone())?;
Ok((stdout, stderr))
}
pub fn run_bash(cmd: impl Into<String>) -> RgResult<(String, String)> {
run_cmd_safe("bash", vec!["-c", &cmd.into()])
}
pub fn run_powershell(cmd: impl Into<String>) -> RgResult<(String, String)> {
run_cmd_safe("powershell", vec!["-Command", &cmd.into()])
}
pub async fn run_bash_async(cmd: impl Into<String>) -> RgResult<(String, String)> {
run_cmd_safe_async("bash", vec!["-c", &cmd.into()]).await
}
pub async fn run_powershell_async(cmd: impl Into<String>) -> RgResult<(String, String)> {
run_cmd_safe_async("powershell", vec!["-Command", &cmd.into()]).await
}
pub fn available_bytes(path: impl Into<String>, is_windows: bool) -> RgResult<i64> {
if is_windows {
return Err(error_info("available_bytes Not implemented for Windows"));
}
let out = run_bash(format!("df {}", path.into()))?.0;
let mut split = out.split("\n");
let head = split.next().ok_msg("Missing stdout from df disk space command")?;
let string = head.split_whitespace().dropping(1).next().ok_msg("Missing second column from df disk space command")?
.split("-").next().ok_msg("Missing first part of second column from df disk space command")?.to_lowercase();
let blocks = string.as_str();
let multiplier: i64 = match blocks {
"1k" => {1024}
"512" => {512}
_ => {
return Err(error_info(format!("Unknown block size: {}", blocks)))
}
};
let second = split.next().ok_msg("Missing second line from df disk space command")?;
let available_bytes = second.split_whitespace().dropping(3).next()
.ok_msg("Missing fourth column from df disk space command")?
.parse::<i64>().error_info("Failed to parse available bytes")? * multiplier;
Ok(available_bytes)
}
#[test]
fn test_disk() {
let b = available_bytes("~", false).unwrap();
let gb = b / (1024 * 1024 * 1024);
println!("Available bytes in gb: {}", gb);
}