diff options
Diffstat (limited to 'src/command/import.rs')
-rw-r--r-- | src/command/import.rs | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/src/command/import.rs b/src/command/import.rs index 931e7af4..09df5839 100644 --- a/src/command/import.rs +++ b/src/command/import.rs @@ -7,7 +7,7 @@ use structopt::StructOpt; use atuin_client::database::Database; use atuin_client::history::History; -use atuin_client::import::Zsh; +use atuin_client::import::{bash::Bash, zsh::Zsh}; use indicatif::ProgressBar; #[derive(StructOpt)] @@ -23,11 +23,17 @@ pub enum Cmd { aliases=&["z", "zs"], )] Zsh, + + #[structopt( + about="import history from the bash history file", + aliases=&["b", "ba", "bas"], + )] + Bash, } impl Cmd { pub async fn run(&self, db: &mut (impl Database + Send + Sync)) -> Result<()> { - println!(" A'Tuin "); + println!(" Atuin "); println!("======================"); println!(" \u{1f30d} "); println!(" \u{1f418}\u{1f418}\u{1f418}\u{1f418} "); @@ -49,6 +55,7 @@ impl Cmd { } Self::Zsh => import_zsh(db).await, + Self::Bash => import_bash(db).await, } } } @@ -120,3 +127,61 @@ async fn import_zsh(db: &mut (impl Database + Send + Sync)) -> Result<()> { Ok(()) } + +// TODO: don't just copy paste this lol +async fn import_bash(db: &mut (impl Database + Send + Sync)) -> Result<()> { + // oh-my-zsh sets HISTFILE=~/.zhistory + // zsh has no default value for this var, but uses ~/.zhistory. + // we could maybe be smarter about this in the future :) + + let histpath = env::var("HISTFILE"); + + let histpath = if let Ok(p) = histpath { + let histpath = PathBuf::from(p); + + if !histpath.exists() { + return Err(eyre!( + "Could not find history file {:?}. try updating $HISTFILE", + histpath + )); + } + + histpath + } else { + let user_dirs = UserDirs::new().unwrap(); + let home_dir = user_dirs.home_dir(); + + home_dir.join(".bash_history") + }; + + let bash = Bash::new(histpath)?; + + let progress = ProgressBar::new(bash.loc); + + let buf_size = 100; + let mut buf = Vec::<History>::with_capacity(buf_size); + + for i in bash + .filter_map(Result::ok) + .filter(|x| !x.command.trim().is_empty()) + { + buf.push(i); + + if buf.len() == buf_size { + db.save_bulk(&buf).await?; + progress.inc(buf.len() as u64); + + buf.clear(); + } + } + + if !buf.is_empty() { + db.save_bulk(&buf).await?; + progress.inc(buf.len() as u64); + } + + progress.finish(); + println!("Import complete!"); + + Ok(()) +} |