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
This commit is contained in:
Ellie Huxtable 2024-07-25 13:40:04 +01:00 committed by GitHub
parent 3cf52990e4
commit 128891f53e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 26 deletions

9
ui/backend/Cargo.lock generated
View File

@ -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",

View File

@ -116,8 +116,7 @@ async fn login(username: String, password: String, key: String) -> Result<String
async fn logout() -> 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<HomeInfo, String> {
.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<Vec<String>, String> {
Ok(commands)
}
#[tauri::command]
async fn cli_settings() -> Result<Settings, String> {
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");
}

View File

@ -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(())
}

View File

@ -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<InspectHistory> {
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<InspectHistory> {
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",