44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
use std::{fs::create_dir_all, io::Read, process::{ChildStdout, ExitStatus}};
|
|
|
|
use rand::{distributions::Alphanumeric, Rng, thread_rng};
|
|
|
|
pub fn assure_dir_exists() -> Result<(), std::io::Error> {
|
|
create_dir_all("/tmp/sandy-tmp")?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn create_path() -> String {
|
|
let name = thread_rng()
|
|
.sample_iter(&Alphanumeric)
|
|
.take(64)
|
|
.map(char::from)
|
|
.collect::<String>();
|
|
format!("/tmp/sandy-tmp/{name}")
|
|
}
|
|
|
|
pub fn exit_code_msg(code: ExitStatus) -> String {
|
|
let status = code.code();
|
|
if let Some(status) = status {
|
|
if status != 0 {
|
|
format!("<span style='color:red;font-weight:bolder'>Command exited with code {status}</span>")
|
|
} else {
|
|
"Command exited with code 0".into()
|
|
}
|
|
} else {
|
|
"Command exited with unknown code".into()
|
|
}
|
|
}
|
|
|
|
pub fn get_stdout(stdout: Option<ChildStdout>) -> Result<String, String> {
|
|
let ret = {
|
|
if let Some(mut sout) = stdout {
|
|
let mut buf = vec![];
|
|
sout.read_to_end(&mut buf).map_err(|x| x.to_string())?;
|
|
String::from_utf8(buf).map_err(|x| x.to_string())?
|
|
} else {
|
|
"Couldn't get stdout from the process".into()
|
|
}
|
|
};
|
|
|
|
Ok(ret)
|
|
} |