first try.

This commit is contained in:
licsber 2024-09-19 15:28:32 +08:00
parent bdc42bfa90
commit 37c18e7002

View File

@ -1,3 +1,50 @@
fn main() {
println!("Hello, world!");
use std::env;
use std::io::{self, Write};
use std::process::Command;
fn retry(cmd: &String, cmd_args: &[String]) {
loop {
let output = Command::new(cmd)
.args(cmd_args)
.output()
.map_err(|e| {
eprintln!("Failed to execute command: {}", e);
io::stderr().flush().unwrap();
});
match output {
Ok(output) => {
if output.status.success() {
io::stdout().write_all(&output.stdout).unwrap();
break;
} else {
eprintln!("Command failed with status: {}", output.status);
}
}
Err(_) => {
println!("Error, wait 1s for retry.")
}
}
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} <cmd> [args...]", args[0]);
std::process::exit(1);
}
let cmd = &args[1];
let cmd_args = if args.len() > 2 {
&args[2..]
} else {
&[]
};
retry(cmd, cmd_args);
}