use openrunner::{run, spawn, ScriptOptions};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Running advanced scripts...");
println!("\n--- Running with 1-second timeout (should fail)...");
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.timeout(Duration::from_secs(1));
let result = run("sleep 3 && echo 'Done'", options).await?;
assert!(result.timed_out);
println!("Script timed out as expected.");
println!("\n--- Spawning a background process...");
let options = ScriptOptions::new().openscript_path("/bin/sh");
let child = spawn(
"echo 'Background process started'; sleep 2; echo 'Background process finished'",
options,
)
.await?;
println!("Spawned process with ID: {}", child.id().unwrap());
tokio::time::sleep(Duration::from_millis(500)).await;
println!("Doing other work while process runs...");
let output = child.wait_with_output().await?;
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("Background process finished"));
println!("Spawned process finished. Output:\n{}", stdout);
println!("\n--- Running with custom environment...");
let temp_dir = tempfile::tempdir()?;
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.working_directory(temp_dir.path())
.env("MY_VAR", "custom_value");
let result = run("echo $MY_VAR > test.txt", options).await?;
assert_eq!(result.exit_code, 0);
let file_content = std::fs::read_to_string(temp_dir.path().join("test.txt"))?;
assert!(file_content.contains("custom_value"));
println!("Custom environment script ran successfully.");
println!("\nAdvanced examples completed successfully.");
Ok(())
}