1use std::path::{Path, PathBuf};
9use std::process::{Child, Command};
10
11use thiserror::Error;
12
13pub const ADMIN_SIDECAR_NAME: &str = "kvconfig-admin";
15
16#[derive(Debug, Clone)]
18pub struct AdminLaunchOptions {
19 pub db_path: PathBuf,
21 pub table: String,
23 pub readonly: bool,
25 pub focus_type: Option<String>,
27}
28
29impl AdminLaunchOptions {
30 pub fn new(db_path: impl Into<PathBuf>) -> Self {
31 Self {
32 db_path: db_path.into(),
33 table: "configs".into(),
34 readonly: false,
35 focus_type: None,
36 }
37 }
38
39 pub fn table(mut self, table: impl Into<String>) -> Self {
40 self.table = table.into();
41 self
42 }
43
44 pub fn readonly(mut self, readonly: bool) -> Self {
45 self.readonly = readonly;
46 self
47 }
48
49 pub fn focus_type(mut self, focus_type: impl Into<String>) -> Self {
50 self.focus_type = Some(focus_type.into());
51 self
52 }
53
54 pub fn absolute_db_path(&self) -> Result<PathBuf, AdminError> {
56 absolute_path(&self.db_path)
57 }
58
59 pub fn args(&self) -> Result<Vec<String>, AdminError> {
61 let db = self.absolute_db_path()?;
62 let mut args = vec![
63 "--db".into(),
64 db.to_string_lossy().into_owned(),
65 "--table".into(),
66 self.table.clone(),
67 ];
68 if self.readonly {
69 args.push("--readonly".into());
70 }
71 if let Some(ref t) = self.focus_type {
72 args.push("--focus-type".into());
73 args.push(t.clone());
74 }
75 Ok(args)
76 }
77}
78
79#[derive(Debug, Error)]
80pub enum AdminError {
81 #[error("invalid database path: {0}")]
82 InvalidPath(String),
83 #[error("kvconfig-admin binary not found: {0}")]
84 BinaryNotFound(String),
85 #[error("failed to spawn kvconfig-admin: {0}")]
86 Spawn(#[from] std::io::Error),
87 #[cfg(feature = "tauri")]
88 #[error("tauri shell/sidecar error: {0}")]
89 Tauri(String),
90}
91
92fn absolute_path(path: &Path) -> Result<PathBuf, AdminError> {
93 if path.is_absolute() {
94 return Ok(path.to_path_buf());
95 }
96 let cwd = std::env::current_dir()
97 .map_err(|e| AdminError::InvalidPath(format!("{} ({e})", path.display())))?;
98 Ok(cwd.join(path))
99}
100
101pub fn resolve_admin_binary(explicit: Option<&Path>) -> Result<PathBuf, AdminError> {
105 if let Some(p) = explicit {
106 if p.is_file() {
107 return Ok(p.to_path_buf());
108 }
109 return Err(AdminError::BinaryNotFound(p.display().to_string()));
110 }
111
112 let exe_name = if cfg!(windows) {
113 "kvconfig-admin.exe"
114 } else {
115 "kvconfig-admin"
116 };
117
118 if let Ok(current) = std::env::current_exe() {
119 if let Some(dir) = current.parent() {
120 let candidate = dir.join(exe_name);
121 if candidate.is_file() {
122 return Ok(candidate);
123 }
124 }
125 }
126
127 Ok(PathBuf::from(exe_name))
129}
130
131pub fn spawn_admin_process(
133 options: &AdminLaunchOptions,
134 binary: Option<&Path>,
135) -> Result<Child, AdminError> {
136 let bin = resolve_admin_binary(binary)?;
137 let args = options.args()?;
138 let child = Command::new(&bin).args(&args).spawn().map_err(|e| {
139 if e.kind() == std::io::ErrorKind::NotFound {
140 AdminError::BinaryNotFound(bin.display().to_string())
141 } else {
142 AdminError::Spawn(e)
143 }
144 })?;
145 Ok(child)
146}
147
148#[cfg(feature = "tauri")]
149mod tauri_ext {
150 use super::*;
151 use tauri::{AppHandle, Runtime};
152 use tauri_plugin_shell::ShellExt;
153
154 pub fn open_sqlite_admin<R: Runtime>(
160 app: &AppHandle<R>,
161 options: &AdminLaunchOptions,
162 ) -> Result<(), AdminError> {
163 let args = options.args()?;
164 let sidecar = app
165 .shell()
166 .sidecar(ADMIN_SIDECAR_NAME)
167 .map_err(|e| AdminError::Tauri(e.to_string()))?
168 .args(args);
169
170 sidecar
171 .spawn()
172 .map_err(|e| AdminError::Tauri(e.to_string()))?;
173 Ok(())
174 }
175}
176
177#[cfg(feature = "tauri")]
178pub use tauri_ext::open_sqlite_admin;