bare-script 0.1.1

The type-safe scripting authority for Rust. A framework for building robust shell commands and automation with 'Parse, don't validate' philosophy.
Documentation
//! Environment variables and working directory example.
//!
//! Run with: cargo run --example env_cwd

use bare_script::sync::CommandBuilder;
use thiserror as _;
use tokio as _;

fn main() -> Result<(), bare_script::ScriptError> {
    #[cfg(windows)]
    let output = {
        CommandBuilder::new("cmd")
            .args(["/C", "echo %MY_VAR%"])
            .env("MY_VAR", "custom_value")
            .capture_output()
            .execute()
    };

    #[cfg(not(windows))]
    let output = {
        CommandBuilder::new("sh")
            .args(["-c", "echo $MY_VAR"])
            .env("MY_VAR", "custom_value")
            .capture_output()
            .execute()
    };

    let output = output?;

    println!("With env MY_VAR=custom_value");
    println!("Output: {}", output.stdout_str());

    // Test current directory
    let cwd = std::env::current_dir().unwrap();
    #[cfg(windows)]
    let output = {
        CommandBuilder::new("cmd")
            .args(["/C", "cd"])
            .current_dir(&cwd)
            .capture_output()
            .execute()
    };

    #[cfg(not(windows))]
    let output = {
        CommandBuilder::new("pwd")
            .current_dir(&cwd)
            .capture_output()
            .execute()
    };

    if let Ok(output) = output {
        println!("Current dir: {}", output.stdout_str().trim());
    }

    Ok(())
}