summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop/src-tauri/src/cli.rs
diff options
context:
space:
mode:
authorBrendan Allan <[email protected]>2026-01-12 17:58:13 +0800
committerGitHub <[email protected]>2026-01-12 17:58:13 +0800
commitebbb4dd88a55a69ea1722eb0622b91d697acf507 (patch)
treef58ecab8b64f0a2abc4ce725b0073ab6e42e935d /packages/desktop/src-tauri/src/cli.rs
parent087473be6ec4ad21220752de2391e8b882f7058f (diff)
downloadopencode-ebbb4dd88a55a69ea1722eb0622b91d697acf507.tar.gz
opencode-ebbb4dd88a55a69ea1722eb0622b91d697acf507.zip
fix(desktop): improve server detection & connection logic (#7962)
Diffstat (limited to 'packages/desktop/src-tauri/src/cli.rs')
-rw-r--r--packages/desktop/src-tauri/src/cli.rs56
1 files changed, 55 insertions, 1 deletions
diff --git a/packages/desktop/src-tauri/src/cli.rs b/packages/desktop/src-tauri/src/cli.rs
index 8b76d1a7f..87ecf4997 100644
--- a/packages/desktop/src-tauri/src/cli.rs
+++ b/packages/desktop/src-tauri/src/cli.rs
@@ -1,8 +1,30 @@
-use tauri::Manager;
+use tauri::{path::BaseDirectory, AppHandle, Manager};
+use tauri_plugin_shell::{process::Command, ShellExt};
const CLI_INSTALL_DIR: &str = ".opencode/bin";
const CLI_BINARY_NAME: &str = "opencode";
+#[derive(serde::Deserialize)]
+pub struct ServerConfig {
+ pub hostname: Option<String>,
+ pub port: Option<u32>,
+}
+
+#[derive(serde::Deserialize)]
+pub struct Config {
+ pub server: Option<ServerConfig>,
+}
+
+pub async fn get_config(app: &AppHandle) -> Option<Config> {
+ create_command(app, "debug config")
+ .output()
+ .await
+ .inspect_err(|e| eprintln!("Failed to read OC config: {e}"))
+ .ok()
+ .and_then(|out| String::from_utf8(out.stdout.to_vec()).ok())
+ .and_then(|s| serde_json::from_str::<Config>(&s).ok())
+}
+
fn get_cli_install_path() -> Option<std::path::PathBuf> {
std::env::var("HOME").ok().map(|home| {
std::path::PathBuf::from(home)
@@ -117,3 +139,35 @@ pub fn sync_cli(app: tauri::AppHandle) -> Result<(), String> {
Ok(())
}
+
+fn get_user_shell() -> String {
+ std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string())
+}
+
+pub fn create_command(app: &tauri::AppHandle, args: &str) -> Command {
+ let state_dir = app
+ .path()
+ .resolve("", BaseDirectory::AppLocalData)
+ .expect("Failed to resolve app local data dir");
+
+ #[cfg(target_os = "windows")]
+ return app
+ .shell()
+ .sidecar("opencode-cli")
+ .unwrap()
+ .env("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY", "true")
+ .env("OPENCODE_CLIENT", "desktop")
+ .env("XDG_STATE_HOME", &state_dir);
+
+ #[cfg(not(target_os = "windows"))]
+ return {
+ let sidecar = get_sidecar_path(app);
+ let shell = get_user_shell();
+ app.shell()
+ .command(&shell)
+ .env("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY", "true")
+ .env("OPENCODE_CLIENT", "desktop")
+ .env("XDG_STATE_HOME", &state_dir)
+ .args(["-il", "-c", &format!("\"{}\" {}", sidecar.display(), args)])
+ };
+}