pub struct Command { /* private fields */ }Expand description
The type to spawn commands.
Implementations§
Source§impl Command
impl Command
Sourcepub fn env<K, V>(self, key: K, value: V) -> Self
pub fn env<K, V>(self, key: K, value: V) -> Self
Inserts or updates an explicit environment variable mapping.
Sourcepub fn envs<I, K, V>(self, envs: I) -> Self
pub fn envs<I, K, V>(self, envs: I) -> Self
Adds or updates multiple environment variable mappings.
Sourcepub fn current_dir<P: AsRef<Path>>(self, current_dir: P) -> Self
pub fn current_dir<P: AsRef<Path>>(self, current_dir: P) -> Self
Sets the working directory for the child process.
Sourcepub fn set_raw_out(self, raw_out: bool) -> Self
pub fn set_raw_out(self, raw_out: bool) -> Self
Configures the reader to output bytes from the child process exactly as received
Sourcepub fn set_process_group(self, process_group: bool) -> Self
pub fn set_process_group(self, process_group: bool) -> Self
Configures the command to spawn in a new process group (POSIX) or job object (Windows).
When enabled, killing the child process will also kill all processes in the group, which is useful for programs that spawn child processes (e.g. PyInstaller wrappers).
Sourcepub fn spawn(self) -> Result<(Receiver<CommandEvent>, CommandChild), Error>
pub fn spawn(self) -> Result<(Receiver<CommandEvent>, CommandChild), Error>
Spawns the command.
§Examples
use tauri_plugin_shell::{process::CommandEvent, ShellExt};
tauri::Builder::default()
.setup(|app| {
let handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
let (mut rx, mut child) = handle
.shell()
.command("cargo")
.args(["tauri", "dev"])
.spawn()
.expect("Failed to spawn cargo");
let mut i = 0;
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
println!("got: {}", String::from_utf8(line).unwrap());
i += 1;
if i == 4 {
child.write("message from Rust\n".as_bytes()).unwrap();
i = 0;
}
}
}
});
Ok(())
});Depending on the command you spawn, it might output in a specific encoding, to parse the output lines in this case:
use tauri_plugin_shell::{process::{CommandEvent, Encoding}, ShellExt};
tauri::Builder::default()
.setup(|app| {
let handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
let (mut rx, mut child) = handle
.shell()
.command("some-program")
.arg("some-arg")
.spawn()
.expect("Failed to spawn some-program");
let encoding = Encoding::for_label(b"windows-1252").unwrap();
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
let (decoded, _, _) = encoding.decode(&line);
println!("got: {decoded}");
}
}
});
Ok(())
});Sourcepub async fn status(self) -> Result<ExitStatus, Error>
pub async fn status(self) -> Result<ExitStatus, Error>
Executes a command as a child process, waiting for it to finish and collecting its exit status. Stdin, stdout and stderr are ignored.
§Examples
use tauri_plugin_shell::ShellExt;
tauri::Builder::default()
.setup(|app| {
let status = tauri::async_runtime::block_on(async move { app.shell().command("which").args(["ls"]).status().await.unwrap() });
println!("`which` finished with status: {:?}", status.code());
Ok(())
});Sourcepub async fn output(self) -> Result<Output, Error>
pub async fn output(self) -> Result<Output, Error>
Executes the command as a child process, waiting for it to finish and collecting all of its output. Stdin is ignored.
§Examples
use tauri_plugin_shell::ShellExt;
tauri::Builder::default()
.setup(|app| {
let output = tauri::async_runtime::block_on(async move { app.shell().command("echo").args(["TAURI"]).output().await.unwrap() });
assert!(output.status.success());
assert_eq!(String::from_utf8(output.stdout).unwrap(), "TAURI");
Ok(())
});