pub struct Bash { /* private fields */ }Expand description
Main entry point for Bashkit.
Provides a virtual bash interpreter with an in-memory virtual filesystem.
Implementations§
Source§impl Bash
impl Bash
Sourcepub fn builder() -> BashBuilder
pub fn builder() -> BashBuilder
Create a new BashBuilder for customized configuration.
Sourcepub async fn exec(&mut self, script: &str) -> Result<ExecResult>
pub async fn exec(&mut self, script: &str) -> Result<ExecResult>
Execute a bash script and return the result.
This method first validates that the script does not exceed the maximum input size, then parses the script with a timeout, AST depth limit, and fuel limit, then executes the resulting AST.
Sourcepub async fn exec_streaming(
&mut self,
script: &str,
output_callback: OutputCallback,
) -> Result<ExecResult>
pub async fn exec_streaming( &mut self, script: &str, output_callback: OutputCallback, ) -> Result<ExecResult>
Execute a bash script with streaming output.
Like exec, but calls output_callback with incremental
(stdout_chunk, stderr_chunk) pairs as output is produced. Callbacks fire
after each loop iteration, command list element, and top-level command.
The full result is still returned in ExecResult for callers that need it.
§Example
use bashkit::Bash;
use std::sync::{Arc, Mutex};
let chunks: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
let chunks_cb = chunks.clone();
let mut bash = Bash::new();
let result = bash.exec_streaming(
"for i in 1 2 3; do echo $i; done",
Box::new(move |stdout, _stderr| {
chunks_cb.lock().unwrap().push(stdout.to_string());
}),
).await?;
assert_eq!(result.stdout, "1\n2\n3\n");
assert_eq!(*chunks.lock().unwrap(), vec!["1\n", "2\n", "3\n"]);Sourcepub fn fs(&self) -> Arc<dyn FileSystem>
pub fn fs(&self) -> Arc<dyn FileSystem>
Get a clone of the underlying filesystem.
Provides direct access to the virtual filesystem for:
- Pre-populating files before script execution
- Reading binary file outputs after execution
- Injecting test data or configuration
§Example
use bashkit::Bash;
use std::path::Path;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut bash = Bash::new();
let fs = bash.fs();
// Pre-populate config file
fs.mkdir(Path::new("/config"), false).await?;
fs.write_file(Path::new("/config/app.txt"), b"debug=true\n").await?;
// Bash script can read pre-populated files
let result = bash.exec("cat /config/app.txt").await?;
assert_eq!(result.stdout, "debug=true\n");
// Bash creates output, read it directly
bash.exec("echo 'done' > /output.txt").await?;
let output = fs.read_file(Path::new("/output.txt")).await?;
assert_eq!(output, b"done\n");
Ok(())
}Sourcepub fn shell_state(&self) -> ShellState
pub fn shell_state(&self) -> ShellState
Capture the current shell state (variables, env, cwd, options).
Returns a serializable snapshot of the interpreter state. Combine with
InMemoryFs::snapshot() for full session persistence.
§Example
use bashkit::Bash;
let mut bash = Bash::new();
bash.exec("x=42").await?;
let state = bash.shell_state();
bash.exec("x=99").await?;
bash.restore_shell_state(&state);
let result = bash.exec("echo $x").await?;
assert_eq!(result.stdout, "42\n");Sourcepub fn restore_shell_state(&mut self, state: &ShellState)
pub fn restore_shell_state(&mut self, state: &ShellState)
Restore shell state from a previous snapshot.
Restores variables, env, cwd, arrays, aliases, traps, and options. Does not restore functions or builtins — those remain as-is.