Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature atuin import from file #2170

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 0 additions & 5 deletions crates/atuin-client/src/import/zsh_histdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ use crate::history::History;
use crate::import::Loader;
use crate::utils::{get_hostname, get_username};

#[derive(sqlx::FromRow, Debug)]
pub struct HistDbEntryCount {
pub count: usize,
}

#[derive(sqlx::FromRow, Debug)]
pub struct HistDbEntry {
pub id: i64,
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
Loading