feat(runtime): run local shell commands from UI
test / workspace (push) Successful in 12m36s

This commit is contained in:
Tom You
2026-07-09 00:17:45 -05:00
parent fc17c44c38
commit a215d91f40
6 changed files with 185 additions and 1 deletions
+1
View File
@@ -1,3 +1,4 @@
pub mod config_store;
pub mod local_terminal;
pub mod pty;
pub mod wallet_vault;
+118
View File
@@ -0,0 +1,118 @@
use serde::Serialize;
use std::path::PathBuf;
use std::process::Command;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocalCommand {
pub command: String,
pub cwd: Option<PathBuf>,
}
impl LocalCommand {
pub fn new(command: impl Into<String>) -> Self {
Self {
command: command.into(),
cwd: None,
}
}
pub fn with_cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
self.cwd = Some(cwd.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LocalCommandOutput {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LocalTerminalError {
InvalidCommand(String),
Io(String),
}
#[derive(Debug, Default, Clone)]
pub struct LocalTerminal;
impl LocalTerminal {
pub fn run(&self, command: LocalCommand) -> Result<LocalCommandOutput, LocalTerminalError> {
if command.command.trim().is_empty() {
return Err(LocalTerminalError::InvalidCommand(
"command is required".to_string(),
));
}
let mut child = Command::new(default_shell());
child.arg("-lc").arg(&command.command);
if let Some(cwd) = command.cwd {
child.current_dir(cwd);
}
let output = child
.output()
.map_err(|err| LocalTerminalError::Io(err.to_string()))?;
Ok(LocalCommandOutput {
exit_code: output.status.code().unwrap_or(-1),
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
})
}
}
fn default_shell() -> &'static str {
if cfg!(windows) {
"cmd"
} else {
"/bin/sh"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_terminal_runs_shell_command_and_captures_stdout() {
let output = LocalTerminal::default()
.run(LocalCommand::new("printf 'rabby-local-terminal'"))
.unwrap();
assert_eq!(output.exit_code, 0);
assert_eq!(output.stdout, "rabby-local-terminal");
assert!(output.stderr.is_empty());
}
#[test]
fn local_terminal_reports_non_zero_exit() {
let output = LocalTerminal::default()
.run(LocalCommand::new("printf error >&2; exit 7"))
.unwrap();
assert_eq!(output.exit_code, 7);
assert_eq!(output.stderr, "error");
}
#[test]
fn local_terminal_rejects_blank_commands() {
let err = LocalTerminal::default()
.run(LocalCommand::new(" "))
.unwrap_err();
assert_eq!(
err,
LocalTerminalError::InvalidCommand("command is required".to_string())
);
}
#[test]
fn local_terminal_runs_from_working_directory() {
let dir = std::env::temp_dir();
let output = LocalTerminal::default()
.run(LocalCommand::new("pwd").with_cwd(&dir))
.unwrap();
assert_eq!(output.exit_code, 0);
assert_eq!(output.stdout.trim(), dir.display().to_string());
}
}
+21 -1
View File
@@ -1,6 +1,7 @@
use rabby_core::{
demo_dashboard, wallet_feature_summary, WalletDashboard, WalletFeatureStatus, Workspace,
};
use rabby_runtime::local_terminal::{LocalCommand, LocalCommandOutput, LocalTerminal};
use rabby_runtime::wallet_vault::{EncryptedWalletVault, WalletVaultStatus};
use serde::Serialize;
use std::path::PathBuf;
@@ -58,6 +59,17 @@ fn load_wallet_vault(path: String, passphrase: String) -> Result<WalletDashboard
EncryptedWalletVault::load_dashboard(path, &passphrase).map_err(|err| err.to_string())
}
#[tauri::command]
fn run_local_command(command: String, cwd: Option<String>) -> Result<LocalCommandOutput, String> {
let mut local_command = LocalCommand::new(command);
if let Some(cwd) = cwd {
local_command = local_command.with_cwd(cwd);
}
LocalTerminal::default()
.run(local_command)
.map_err(|err| format!("{err:?}"))
}
fn resolve_vault_path(path: Option<String>) -> PathBuf {
path.map(PathBuf::from)
.unwrap_or_else(|| std::env::temp_dir().join("rabby-demo-wallet-vault.json"))
@@ -70,7 +82,8 @@ fn main() {
wallet_mvp_summary,
wallet_dashboard,
save_demo_wallet_vault,
load_wallet_vault
load_wallet_vault,
run_local_command
])
.run(tauri::generate_context!())
.expect("failed to run Rabby Tauri application");
@@ -94,4 +107,11 @@ mod tests {
let path = resolve_vault_path(Some("/tmp/custom-rabby-vault.json".to_string()));
assert_eq!(path, PathBuf::from("/tmp/custom-rabby-vault.json"));
}
#[test]
fn local_command_tauri_bridge_captures_output() {
let output = run_local_command("printf tauri-local".to_string(), None).unwrap();
assert_eq!(output.exit_code, 0);
assert_eq!(output.stdout, "tauri-local");
}
}
+25
View File
@@ -136,6 +136,30 @@ function wireVaultActions() {
if (button) button.addEventListener('click', saveDemoVault);
}
async function runLocalTerminalCommand() {
const command = document.getElementById('terminal-command').value;
const output = document.getElementById('terminal-output');
output.textContent = 'Running…';
try {
const tauri = window.__TAURI__?.core;
if (!tauri) throw new Error('Tauri bridge unavailable in browser preview');
const result = await tauri.invoke('run_local_command', { command, cwd: null });
output.textContent = [
`$ ${command}`,
`exit ${result.exit_code}`,
result.stdout ? `stdout:\n${result.stdout}` : '',
result.stderr ? `stderr:\n${result.stderr}` : ''
].filter(Boolean).join('\n');
} catch (error) {
output.textContent = String(error);
}
}
function wireTerminalActions() {
const button = document.getElementById('run-terminal');
if (button) button.addEventListener('click', runLocalTerminalCommand);
}
async function boot() {
const [dashboard, features] = await Promise.all([
invokeOrFallback('wallet_dashboard', fallbackDashboard),
@@ -144,6 +168,7 @@ async function boot() {
renderDashboard(dashboard);
renderFeatures(features);
wireVaultActions();
wireTerminalActions();
}
boot();
+14
View File
@@ -132,6 +132,20 @@
</article>
</section>
<section class="panel terminal-panel">
<div class="panel-heading">
<div>
<p class="eyebrow">Local terminal</p>
<h3>Run a local shell command</h3>
</div>
<button id="run-terminal" class="primary" type="button">Run</button>
</div>
<div class="terminal-form">
<input id="terminal-command" value="printf 'hello from Rabby local terminal'" />
<pre id="terminal-output">Local command runtime is wired through Tauri.</pre>
</div>
</section>
<section class="panel feature-panel">
<div class="panel-heading">
<div>
+6
View File
@@ -306,3 +306,9 @@ h3 { margin-bottom: 0; font-size: 18px; }
.hero-grid,
.two-column { grid-template-columns: 1fr; }
}
.terminal-panel { margin-bottom: 16px; }
.terminal-form { margin-top: 18px; display: grid; gap: 12px; }
.terminal-form input { width: 100%; border: 1px solid #dfe6f6; border-radius: 14px; padding: 12px 14px; color: #253052; background: #f8faff; font: 14px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace; }
.terminal-form pre { min-height: 120px; margin: 0; padding: 16px; border-radius: 18px; color: #b9fbcf; background: #12182b; white-space: pre-wrap; overflow: auto; font: 13px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }