1use anyhow::anyhow;
2use tokio::process;
3
4pub async fn hostname() -> anyhow::Result<String> {
5 let bytes = cmd("hostname", &[]).await?;
7 let str = String::from_utf8(bytes)?;
8 let str = str.trim();
9 Ok(str.to_string())
10}
11
12pub async fn cmd(exe: &str, args: &[&str]) -> anyhow::Result<Vec<u8>> {
13 let out = process::Command::new(exe).args(args).output().await?;
14 if out.status.success() {
15 Ok(out.stdout)
16 } else {
17 Err(anyhow!(
18 "Failed to execute command: exe={exe:?} args={args:?} err={:?}",
19 String::from_utf8_lossy(&out.stderr[..])
20 ))
21 }
22}