From 37c18e7002700c73726553925b22a9f071d7450c Mon Sep 17 00:00:00 2001 From: licsber Date: Thu, 19 Sep 2024 15:28:32 +0800 Subject: [PATCH] first try. --- src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index e7a11a9..f04c289 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = env::args().collect(); + + if args.len() < 2 { + eprintln!("Usage: {} [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); +} +