From 128891f53ecb2f78c5eb059495114a66f25a58de Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Thu, 25 Jul 2024 13:40:04 +0100 Subject: [PATCH] fix(gui): do not hardcode db path (#2309) * feat(gui/backend): add cli_settings tauri command * chore(gui/backend): overdue cargo fmt * fix(gui): use configured db path, not hardcoded --- ui/backend/Cargo.lock | 9 +++++---- ui/backend/src/main.rs | 36 +++++++++++++++++++++++++----------- ui/backend/src/run/pty.rs | 8 +++----- ui/src/state/models.ts | 11 +++++------ 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/ui/backend/Cargo.lock b/ui/backend/Cargo.lock index 1459e7e7..7bacf9d5 100644 --- a/ui/backend/Cargo.lock +++ b/ui/backend/Cargo.lock @@ -351,7 +351,7 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "atuin-client" -version = "18.4.0-beta.2" +version = "18.4.0-beta.3" dependencies = [ "async-trait", "atuin-common", @@ -404,7 +404,7 @@ dependencies = [ [[package]] name = "atuin-common" -version = "18.4.0-beta.2" +version = "18.4.0-beta.3" dependencies = [ "base64 0.22.1", "directories", @@ -424,7 +424,7 @@ dependencies = [ [[package]] name = "atuin-dotfiles" -version = "18.4.0-beta.2" +version = "18.4.0-beta.3" dependencies = [ "atuin-client", "atuin-common", @@ -438,7 +438,7 @@ dependencies = [ [[package]] name = "atuin-history" -version = "18.4.0-beta.2" +version = "18.4.0-beta.3" dependencies = [ "async-trait", "atuin-client", @@ -1108,6 +1108,7 @@ dependencies = [ "libc", "mio", "parking_lot", + "serde", "signal-hook", "signal-hook-mio", "winapi", diff --git a/ui/backend/src/main.rs b/ui/backend/src/main.rs index 13e02c98..4339e4ba 100644 --- a/ui/backend/src/main.rs +++ b/ui/backend/src/main.rs @@ -116,8 +116,7 @@ async fn login(username: String, password: String, key: String) -> Result Result<(), String> { let settings = Settings::new().map_err(|e| e.to_string())?; - atuin_client::logout::logout(&settings) - .map_err(|e| e.to_string())?; + atuin_client::logout::logout(&settings).map_err(|e| e.to_string())?; Ok(()) } @@ -154,11 +153,18 @@ async fn home_info() -> Result { .await .map_err(|e| e.to_string())?; - let history = db.list(None, None).await?; - let stats = stats::compute(&settings, &history, 10, 1).map_or(vec![], |stats|stats.top[0..5].to_vec()).iter().map(|(commands, count)| (commands.join(" "), *count as u64)).collect(); - let recent = if history.len() > 5 {history[0..5].to_vec()} else {vec![]}; - let recent = recent.into_iter().map(|h|h.into()).collect(); + let stats = stats::compute(&settings, &history, 10, 1) + .map_or(vec![], |stats| stats.top[0..5].to_vec()) + .iter() + .map(|(commands, count)| (commands.join(" "), *count as u64)) + .collect(); + let recent = if history.len() > 5 { + history[0..5].to_vec() + } else { + vec![] + }; + let recent = recent.into_iter().map(|h| h.into()).collect(); let info = if !settings.logged_in() { HomeInfo { @@ -257,6 +263,12 @@ async fn prefix_search(query: &str) -> Result, String> { Ok(commands) } +#[tauri::command] +async fn cli_settings() -> Result { + let settings = Settings::new().map_err(|e| e.to_string())?; + Ok(settings) +} + fn show_window(app: &AppHandle) { let windows = app.webview_windows(); @@ -284,6 +296,7 @@ fn main() { logout, register, history_calendar, + cli_settings, run::pty::pty_open, run::pty::pty_write, run::pty::pty_resize, @@ -298,16 +311,17 @@ fn main() { dotfiles::vars::delete_var, dotfiles::vars::set_var, ]) - .plugin(tauri_plugin_sql::Builder::default().add_migrations("sqlite:runbooks.db", run::migrations::migrations()).build()) + .plugin( + tauri_plugin_sql::Builder::default() + .add_migrations("sqlite:runbooks.db", run::migrations::migrations()) + .build(), + ) .plugin(tauri_plugin_http::init()) .plugin(tauri_plugin_single_instance::init(|app, args, cwd| { let _ = show_window(app); - })) .manage(state::AtuinState::default()) - .setup(|app|{ - Ok(()) - }) + .setup(|app| Ok(())) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/ui/backend/src/run/pty.rs b/ui/backend/src/run/pty.rs index 2af617dd..1ede44a0 100644 --- a/ui/backend/src/run/pty.rs +++ b/ui/backend/src/run/pty.rs @@ -89,14 +89,12 @@ pub(crate) async fn pty_kill( let pty = state.pty_sessions.write().await.remove(&pid); match pty { - Some(pty)=>{ - - pty.kill_child().await.map_err(|e|e.to_string())?; + Some(pty) => { + pty.kill_child().await.map_err(|e| e.to_string())?; println!("RIP {pid:?}"); } - None=>{} + None => {} } - Ok(()) } diff --git a/ui/src/state/models.ts b/ui/src/state/models.ts index 4ca79006..891f7a55 100644 --- a/ui/src/state/models.ts +++ b/ui/src/state/models.ts @@ -1,3 +1,4 @@ +import { invoke } from "@tauri-apps/api/core"; import Database from "@tauri-apps/plugin-sql"; export class User { @@ -122,9 +123,8 @@ interface Sync { export async function inspectCommandHistory( h: ShellHistory, ): Promise { - const db = await Database.load( - "sqlite:/Users/ellie/.local/share/atuin/history.db", - ); + const settings: Settings = await invoke("cli_settings"); + const db = await Database.load("sqlite:" + settings.db_path); let other: any[] = await db.select( "select * from history where command=?1 order by timestamp desc", @@ -151,9 +151,8 @@ export async function inspectCommandHistory( export async function inspectDirectoryHistory( h: ShellHistory, ): Promise { - const db = await Database.load( - "sqlite:/Users/ellie/.local/share/atuin/history.db", - ); + const settings: Settings = await invoke("cli_settings"); + const db = await Database.load("sqlite:" + settings.db_path); let other: any[] = await db.select( "select * from history where cwd=?1 order by timestamp desc",