|
1 | | -use tauri::Manager; |
| 1 | +use tauri::{path::BaseDirectory, AppHandle, Manager}; |
| 2 | +use tauri_plugin_shell::{process::Command, ShellExt}; |
2 | 3 |
|
3 | 4 | const CLI_INSTALL_DIR: &str = ".opencode/bin"; |
4 | 5 | const CLI_BINARY_NAME: &str = "opencode"; |
5 | 6 |
|
| 7 | +#[derive(serde::Deserialize)] |
| 8 | +pub struct ServerConfig { |
| 9 | + pub hostname: Option<String>, |
| 10 | + pub port: Option<u32>, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(serde::Deserialize)] |
| 14 | +pub struct Config { |
| 15 | + pub server: Option<ServerConfig>, |
| 16 | +} |
| 17 | + |
| 18 | +pub async fn get_config(app: &AppHandle) -> Option<Config> { |
| 19 | + create_command(app, "debug config") |
| 20 | + .output() |
| 21 | + .await |
| 22 | + .inspect_err(|e| eprintln!("Failed to read OC config: {e}")) |
| 23 | + .ok() |
| 24 | + .and_then(|out| String::from_utf8(out.stdout.to_vec()).ok()) |
| 25 | + .and_then(|s| serde_json::from_str::<Config>(&s).ok()) |
| 26 | +} |
| 27 | + |
6 | 28 | fn get_cli_install_path() -> Option<std::path::PathBuf> { |
7 | 29 | std::env::var("HOME").ok().map(|home| { |
8 | 30 | std::path::PathBuf::from(home) |
@@ -117,3 +139,35 @@ pub fn sync_cli(app: tauri::AppHandle) -> Result<(), String> { |
117 | 139 |
|
118 | 140 | Ok(()) |
119 | 141 | } |
| 142 | + |
| 143 | +fn get_user_shell() -> String { |
| 144 | + std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()) |
| 145 | +} |
| 146 | + |
| 147 | +pub fn create_command(app: &tauri::AppHandle, args: &str) -> Command { |
| 148 | + let state_dir = app |
| 149 | + .path() |
| 150 | + .resolve("", BaseDirectory::AppLocalData) |
| 151 | + .expect("Failed to resolve app local data dir"); |
| 152 | + |
| 153 | + #[cfg(target_os = "windows")] |
| 154 | + return app |
| 155 | + .shell() |
| 156 | + .sidecar("opencode-cli") |
| 157 | + .unwrap() |
| 158 | + .env("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY", "true") |
| 159 | + .env("OPENCODE_CLIENT", "desktop") |
| 160 | + .env("XDG_STATE_HOME", &state_dir); |
| 161 | + |
| 162 | + #[cfg(not(target_os = "windows"))] |
| 163 | + return { |
| 164 | + let sidecar = get_sidecar_path(app); |
| 165 | + let shell = get_user_shell(); |
| 166 | + app.shell() |
| 167 | + .command(&shell) |
| 168 | + .env("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY", "true") |
| 169 | + .env("OPENCODE_CLIENT", "desktop") |
| 170 | + .env("XDG_STATE_HOME", &state_dir) |
| 171 | + .args(["-il", "-c", &format!("\"{}\" {}", sidecar.display(), args)]) |
| 172 | + }; |
| 173 | +} |
0 commit comments