use env_logger::Builder;
use std::env;
use std::ffi::OsStr;
use std::io::Read;
use std::path::Path;
use std::process::{self, Command};
fn prog() -> Option<String> {
env::args()
.next()
.as_ref()
.map(Path::new)
.and_then(Path::file_name)
.and_then(OsStr::to_str)
.map(String::from)
}
fn main() {
Builder::new().filter_level(log::LevelFilter::Debug).init();
eprintln!("Running {:#?}", prog().unwrap());
let mut cmd = Command::new("cargo");
cmd.args(["run", "--", "-cvv", "demo/config.rs"]);
let mut child = cmd
.stdout(process::Stdio::piped())
.stderr(process::Stdio::piped())
.spawn()
.expect("failed to spawn command");
let mut stdout = child.stdout.take().expect("failed to get stdout");
let mut stderr = child.stderr.take().expect("failed to get stderr");
let mut stdout_output = String::new();
stdout
.read_to_string(&mut stdout_output)
.expect("failed to read stdout");
println!("Captured stdout:\n{}", stdout_output);
let mut stderr_output = String::new();
stderr
.read_to_string(&mut stderr_output)
.expect("failed to read stderr");
println!("Captured stderr:\n{}", stderr_output);
let status = child.wait().expect("failed to wait for child");
if status.success()
&& !stderr_output.contains("error:")
&& !stderr_output.contains("Build failed")
{
println!("Command executed successfully");
} else {
eprintln!("Build failed!");
if let Some(code) = status.code() {
eprintln!("Exit code: {}", code);
}
panic!("Build failed when it should have succeeded");
}
}