use openrunner_rs::{run, run_file, ScriptOptions};
use std::io::Write;
use tempfile::NamedTempFile;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🚀 Running basic OpenRunner examples...\n");
println!("📝 Example 1: Simple script execution");
let options = ScriptOptions::new().openscript_path("/bin/sh");
let result = run(r#"echo "Hello from OpenRunner!""#, options).await?;
println!(" Exit code: {}", result.exit_code);
println!(" Output: {}", result.stdout.trim());
println!(" Duration: {:?}\n", result.duration);
println!("📝 Example 2: Script with arguments");
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.args(vec!["World".to_string(), "from Rust".to_string()]);
let result = run(r#"echo "Hello, $1 $2!""#, options).await?;
println!(" Output: {}", result.stdout.trim());
println!("\n📝 Example 3: Environment variables");
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.env("GREETING", "Hi there")
.env("NAME", "Developer");
let result = run(r#"echo "$GREETING, $NAME!""#, options).await?;
println!(" Output: {}", result.stdout.trim());
println!("\n📝 Example 4: Working directory");
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.working_directory("/tmp");
let result = run(r#"pwd && echo "Working in: $(pwd)""#, options).await?;
println!(" Output: {}", result.stdout.trim());
println!("\n📝 Example 5: File execution");
let mut temp_file = NamedTempFile::new()?;
writeln!(temp_file, "#!/bin/sh")?;
writeln!(temp_file, "echo 'This script is from a file!'")?;
writeln!(temp_file, "echo 'Current time:' $(date)")?;
let path = temp_file.path().to_path_buf();
let options = ScriptOptions::new().openscript_path("/bin/sh");
let result = run_file(&path, options).await?;
println!(" Output: {}", result.stdout.trim());
println!("\n📝 Example 6: Error handling");
let options = ScriptOptions::new().openscript_path("/bin/sh");
let result = run(r#"echo "This will succeed" && exit 42"#, options).await?;
println!(" Exit code: {}", result.exit_code);
println!(" Stdout: {}", result.stdout.trim());
if result.exit_code != 0 {
println!(" ⚠️ Script failed with non-zero exit code");
}
println!("\n✅ Basic examples completed successfully!");
Ok(())
}