Skip to content

Commit

Permalink
Create initial file import for atuin_histdb.
Browse files Browse the repository at this point in the history
  • Loading branch information
mijoharas committed Jun 19, 2024
1 parent aaa7de9 commit 0c01890
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
73 changes: 73 additions & 0 deletions crates/atuin-client/src/import/atuin_histdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// import old shell history from atuin-histdb file!
// automatically hoover up all that we can find

use std::path::{Path, PathBuf};

use async_trait::async_trait;
use eyre::{eyre, Result};

use crate::database::{Sqlite, Database, current_context};
use crate::settings::FilterMode;

use super::Importer;
use crate::history::History;
use crate::import::Loader;

pub struct AtuinExternalDb {
histdb: Vec<History>,
}
impl AtuinExternalDb {
pub fn histpath() -> Result<PathBuf> {
let path = Path::new("/tmp/history.db").to_path_buf();
if path.exists() {
Ok(path)
} else {
Err(eyre!("Could not find history file hardcodded at /tmp/history.db"))
}
}
}

/// Read db at given file, return vector of entries.
async fn hist_from_db(dbpath: PathBuf) -> Result<Vec<History>> {
let db_to_migrate = Sqlite::new(dbpath, 0.1).await?;
let external_history = db_to_migrate.list(
&[FilterMode::Global],
&current_context(), // this is passed to list, but only used for the workspace
None,
false,
true,
).await.unwrap();
// debug!("{}", external_history);
Ok(external_history)
}

#[async_trait]
impl Importer for AtuinExternalDb {
const NAME: &'static str = "history.db";

/// Creates a new AtuinExternalDb imports the history
/// Does no duplicate checking.
async fn new() -> Result<Self> {
let dbpath = AtuinExternalDb::histpath()?;
let histdb_entry_vec = hist_from_db(dbpath).await?;
Ok(Self {
histdb: histdb_entry_vec,
})
}

async fn entries(&mut self) -> Result<usize> {
Ok(self.histdb.len())
}

async fn load(self, h: &mut impl Loader) -> Result<()> {
for entry in self.histdb {
h.push(entry).await?;
}
Ok(())
}
}

#[cfg(test)]
mod test {
// TODO
}
1 change: 1 addition & 0 deletions crates/atuin-client/src/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod xonsh;
pub mod xonsh_sqlite;
pub mod zsh;
pub mod zsh_histdb;
pub mod atuin_histdb;

#[async_trait]
pub trait Importer: Sized {
Expand Down
5 changes: 4 additions & 1 deletion crates/atuin/src/command/client/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use atuin_client::{
database::Database,
history::History,
import::{
bash::Bash, fish::Fish, nu::Nu, nu_histdb::NuHistDb, replxx::Replxx, resh::Resh,
atuin_histdb::AtuinExternalDb, bash::Bash, fish::Fish, nu::Nu, nu_histdb::NuHistDb, replxx::Replxx, resh::Resh,
xonsh::Xonsh, xonsh_sqlite::XonshSqlite, zsh::Zsh, zsh_histdb::ZshHistDb, Importer, Loader,
},
};
Expand Down Expand Up @@ -40,6 +40,8 @@ pub enum Cmd {
Xonsh,
/// Import history from xonsh sqlite db
XonshSqlite,
/// Import history from a separate atuin history.db file
AtuinExternalDb
}

const BATCH_SIZE: usize = 100;
Expand Down Expand Up @@ -116,6 +118,7 @@ impl Cmd {
Self::NuHistDb => import::<NuHistDb, DB>(db).await,
Self::Xonsh => import::<Xonsh, DB>(db).await,
Self::XonshSqlite => import::<XonshSqlite, DB>(db).await,
Self::AtuinExternalDb => import::<AtuinExternalDb, DB>(db).await,
}
}
}
Expand Down

0 comments on commit 0c01890

Please sign in to comment.