use openrunner_rs::{run, ScriptOptions};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Real-world OpenRunner examples\n");
println!("Example 1: CI/CD Pipeline Simulation");
simulate_ci_pipeline().await?;
println!("\nExample 2: Log Analysis");
analyze_logs().await?;
println!("\nExample 3: System Monitoring");
monitor_system().await?;
println!("\nExample 4: Backup Operations");
backup_operations().await?;
println!("\nExample 5: Development Environment Setup");
setup_dev_environment().await?;
println!("\nReal-world examples completed!");
Ok(())
}
async fn simulate_ci_pipeline() -> Result<(), Box<dyn std::error::Error>> {
let pipeline_steps = vec![
("Checkout Code", "echo 'Checking out source code...' && sleep 1"),
("Install Dependencies", "echo 'Installing dependencies...' && sleep 2"),
("Run Tests", "echo 'Running test suite...' && sleep 3"),
("Build Application", "echo 'Building application...' && sleep 2"),
("Deploy", "echo 'Deploying to staging...' && sleep 1"),
];
let start_time = std::time::Instant::now();
for (step_name, script) in pipeline_steps {
println!(" Running: {}", step_name);
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.timeout(Duration::from_secs(10));
match run(script, options).await {
Ok(result) => {
if result.exit_code == 0 {
println!(" SUCCESS: {} completed", step_name);
} else {
println!(" FAILED: {} failed with exit code {}", step_name, result.exit_code);
return Err(format!("Pipeline failed at step: {}", step_name).into());
}
}
Err(e) => {
println!(" ERROR: {} failed: {}", step_name, e);
return Err(e.into());
}
}
}
println!(" Total pipeline time: {:?}", start_time.elapsed());
Ok(())
}
async fn analyze_logs() -> Result<(), Box<dyn std::error::Error>> {
let log_analysis_script = r#"
# Create sample log data
cat << 'EOF' > /tmp/sample.log
2024-01-15 10:00:01 INFO Application started
2024-01-15 10:00:02 DEBUG Loading configuration
2024-01-15 10:00:03 INFO Server listening on port 8080
2024-01-15 10:00:05 WARN Connection timeout for user 123
2024-01-15 10:00:10 ERROR Database connection failed
2024-01-15 10:00:15 INFO Request processed successfully
2024-01-15 10:00:20 ERROR Authentication failed for user abc
EOF
echo "=== Log Analysis Report ==="
echo "Total lines: $(wc -l < /tmp/sample.log)"
echo "Error count: $(grep -c ERROR /tmp/sample.log)"
echo "Warning count: $(grep -c WARN /tmp/sample.log)"
echo "Info count: $(grep -c INFO /tmp/sample.log)"
echo ""
echo "=== Recent Errors ==="
grep ERROR /tmp/sample.log | tail -5
rm -f /tmp/sample.log
"#;
let options = ScriptOptions::new().openscript_path("/bin/sh");
let result = run(log_analysis_script, options).await?;
println!(" Log Analysis Results:");
for line in result.stdout.lines() {
println!(" {}", line);
}
Ok(())
}
async fn monitor_system() -> Result<(), Box<dyn std::error::Error>> {
println!(" Monitoring system resources...");
let monitoring_scripts = vec![
("CPU Usage", "top -l 1 | grep 'CPU usage' || echo 'CPU info not available'"),
("Memory Usage", "vm_stat | head -5 || free -h | head -2 || echo 'Memory info not available'"),
("Disk Usage", "df -h / | tail -1 || echo 'Disk info not available'"),
("Load Average", "uptime || echo 'Load average not available'"),
];
for (metric, script) in monitoring_scripts {
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.timeout(Duration::from_secs(5));
match run(script, options).await {
Ok(result) => {
println!(" {}: {}", metric, result.stdout.trim());
}
Err(e) => {
println!(" FAILED: Failed to get {}: {}", metric, e);
}
}
}
Ok(())
}
async fn backup_operations() -> Result<(), Box<dyn std::error::Error>> {
let backup_script = r#"
# Simulate backup operations
echo "Starting backup process..."
# Create sample data
mkdir -p /tmp/app_data
echo "Important data 1" > /tmp/app_data/file1.txt
echo "Important data 2" > /tmp/app_data/file2.txt
# Create backup
backup_dir="/tmp/backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$backup_dir"
echo "Backing up application data..."
cp -r /tmp/app_data/* "$backup_dir/" 2>/dev/null || true
# Verify backup
file_count=$(find "$backup_dir" -type f | wc -l)
echo "Backup completed: $file_count files backed up"
echo "Backup location: $backup_dir"
# List backup contents
echo "Backup contents:"
ls -la "$backup_dir"
# Cleanup
rm -rf /tmp/app_data "$backup_dir"
echo "Cleanup completed"
"#;
let options = ScriptOptions::new().openscript_path("/bin/sh");
let result = run(backup_script, options).await?;
println!(" Backup Results:");
for line in result.stdout.lines() {
println!(" {}", line);
}
Ok(())
}
async fn setup_dev_environment() -> Result<(), Box<dyn std::error::Error>> {
println!(" Setting up development environment...");
let setup_tasks = vec![
("Check Git", "git --version"),
("Check Rust", "rustc --version || echo 'Rust not installed'"),
("Check Node.js", "node --version || echo 'Node.js not installed'"),
("Check Docker", "docker --version || echo 'Docker not installed'"),
("Create Project Structure", r#"
mkdir -p /tmp/dev_project/{src,tests,docs}
echo 'Project structure created'
ls -la /tmp/dev_project/
"#),
];
for (task_name, script) in setup_tasks {
println!(" {}", task_name);
let options = ScriptOptions::new()
.openscript_path("/bin/sh")
.timeout(Duration::from_secs(10));
match run(script, options).await {
Ok(result) => {
if !result.stdout.trim().is_empty() {
println!(" {}", result.stdout.trim());
}
}
Err(e) => println!(" ERROR: {}: {}", task_name, e),
}
}
let cleanup_options = ScriptOptions::new().openscript_path("/bin/sh");
let _ = run("rm -rf /tmp/dev_project", cleanup_options).await;
Ok(())
}