| 2247 | } |
| 2248 | |
| 2249 | async fn handle_db_command( |
| 2250 | action: Option<DbCommands>, |
| 2251 | query: Option<String>, |
| 2252 | format: DbOutputFormat, |
| 2253 | ) -> anyhow::Result<()> { |
| 2254 | if matches!(action, Some(DbCommands::Path)) { |
| 2255 | println!("{}", local_database_path().display()); |
| 2256 | return Ok(()); |
| 2257 | } |
| 2258 | |
| 2259 | let db_path = local_database_path(); |
| 2260 | if let Some(query) = query { |
| 2261 | let mut args = vec![db_path.display().to_string()]; |
| 2262 | match format { |
| 2263 | DbOutputFormat::Json => args.push("-json".to_string()), |
| 2264 | DbOutputFormat::Tsv => args.push("-tabs".to_string()), |
| 2265 | } |
| 2266 | args.push(query); |
| 2267 | |
| 2268 | let output = ProcessCommand::new("sqlite3") |
| 2269 | .args(&args) |
| 2270 | .output() |
| 2271 | .map_err(|e| anyhow::anyhow!("Failed to run sqlite3: {}", e))?; |
| 2272 | if output.status.success() { |
| 2273 | print!("{}", String::from_utf8_lossy(&output.stdout)); |
| 2274 | return Ok(()); |
| 2275 | } |
| 2276 | anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr)); |
| 2277 | } |
| 2278 | |
| 2279 | let status = ProcessCommand::new("sqlite3") |
| 2280 | .arg(db_path) |
| 2281 | .status() |
| 2282 | .map_err(|e| anyhow::anyhow!("Failed to run sqlite3 interactive shell: {}", e))?; |
| 2283 | if !status.success() { |
| 2284 | anyhow::bail!("sqlite3 exited with status {}", status); |
| 2285 | } |
| 2286 | Ok(()) |
| 2287 | } |
| 2288 | |
| 2289 | async fn handle_stats_command( |
| 2290 | days: Option<i64>, |