diff options
| author | Vladislav Stepanov <8uk.8ak@gmail.com> | 2023-04-14 23:18:58 +0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-14 20:18:58 +0100 |
| commit | c05d2850420a2c163b8f62c33a6cef7c0ae1ad8d (patch) | |
| tree | 2c44a44eda7e76fa74e78ac1fd02f55c1ed4d804 /src/command/client/import.rs | |
| parent | Switch to uuidv7 (#864) (diff) | |
| download | atuin-c05d2850420a2c163b8f62c33a6cef7c0ae1ad8d.zip | |
Workspace reorder (#868)
* Try different workspace structure
Move main crate (atuin) to be on the same level with other crates in
this workspace
* extract common dependencies to the workspace definition
* fix base64 v0.21 deprecation warning
* questionable: update deps & fix chrono deprecations
possible panic sites are unchanged, they're just more visible now
* Revert "questionable: update deps & fix chrono deprecations"
This reverts commit 993e60f8dea81a1625a04285a617959ad09a0866.
Diffstat (limited to 'src/command/client/import.rs')
| -rw-r--r-- | src/command/client/import.rs | 152 |
1 files changed, 0 insertions, 152 deletions
diff --git a/src/command/client/import.rs b/src/command/client/import.rs deleted file mode 100644 index 7abc3d44..00000000 --- a/src/command/client/import.rs +++ /dev/null @@ -1,152 +0,0 @@ -use std::env; - -use async_trait::async_trait; -use clap::Parser; -use eyre::Result; -use indicatif::ProgressBar; - -use atuin_client::{ - database::Database, - history::History, - import::{ - bash::Bash, fish::Fish, nu::Nu, nu_histdb::NuHistDb, resh::Resh, zsh::Zsh, - zsh_histdb::ZshHistDb, Importer, Loader, - }, -}; - -#[derive(Parser)] -#[command(infer_subcommands = true)] -pub enum Cmd { - /// Import history for the current shell - Auto, - - /// Import history from the zsh history file - Zsh, - /// Import history from the zsh history file - ZshHistDb, - /// Import history from the bash history file - Bash, - /// Import history from the resh history file - Resh, - /// Import history from the fish history file - Fish, - /// Import history from the nu history file - Nu, - /// Import history from the nu history file - NuHistDb, -} - -const BATCH_SIZE: usize = 100; - -impl Cmd { - pub async fn run<DB: Database>(&self, db: &mut DB) -> Result<()> { - println!(" Atuin "); - println!("======================"); - println!(" \u{1f30d} "); - println!(" \u{1f418}\u{1f418}\u{1f418}\u{1f418} "); - println!(" \u{1f422} "); - println!("======================"); - println!("Importing history..."); - - match self { - Self::Auto => { - if cfg!(windows) { - println!("This feature does not work on windows. Please run atuin import <SHELL>. To view a list of shells, run atuin import."); - return Ok(()); - } - - let shell = env::var("SHELL").unwrap_or_else(|_| String::from("NO_SHELL")); - if shell.ends_with("/zsh") { - if ZshHistDb::histpath().is_ok() { - println!( - "Detected Zsh-HistDb, using :{}", - ZshHistDb::histpath().unwrap().to_str().unwrap() - ); - import::<ZshHistDb, DB>(db).await - } else { - println!("Detected ZSH"); - import::<Zsh, DB>(db).await - } - } else if shell.ends_with("/fish") { - println!("Detected Fish"); - import::<Fish, DB>(db).await - } else if shell.ends_with("/bash") { - println!("Detected Bash"); - import::<Bash, DB>(db).await - } else if shell.ends_with("/nu") { - if NuHistDb::histpath().is_ok() { - println!( - "Detected Nu-HistDb, using :{}", - NuHistDb::histpath().unwrap().to_str().unwrap() - ); - import::<NuHistDb, DB>(db).await - } else { - println!("Detected Nushell"); - import::<Nu, DB>(db).await - } - } else { - println!("cannot import {shell} history"); - Ok(()) - } - } - - Self::Zsh => import::<Zsh, DB>(db).await, - Self::ZshHistDb => import::<ZshHistDb, DB>(db).await, - Self::Bash => import::<Bash, DB>(db).await, - Self::Resh => import::<Resh, DB>(db).await, - Self::Fish => import::<Fish, DB>(db).await, - Self::Nu => import::<Nu, DB>(db).await, - Self::NuHistDb => import::<NuHistDb, DB>(db).await, - } - } -} - -pub struct HistoryImporter<'db, DB: Database> { - pb: ProgressBar, - buf: Vec<History>, - db: &'db mut DB, -} - -impl<'db, DB: Database> HistoryImporter<'db, DB> { - fn new(db: &'db mut DB, len: usize) -> Self { - Self { - pb: ProgressBar::new(len as u64), - buf: Vec::with_capacity(BATCH_SIZE), - db, - } - } - - async fn flush(self) -> Result<()> { - if !self.buf.is_empty() { - self.db.save_bulk(&self.buf).await?; - } - self.pb.finish(); - Ok(()) - } -} - -#[async_trait] -impl<'db, DB: Database> Loader for HistoryImporter<'db, DB> { - async fn push(&mut self, hist: History) -> Result<()> { - self.pb.inc(1); - self.buf.push(hist); - if self.buf.len() == self.buf.capacity() { - self.db.save_bulk(&self.buf).await?; - self.buf.clear(); - } - Ok(()) - } -} - -async fn import<I: Importer + Send, DB: Database>(db: &mut DB) -> Result<()> { - println!("Importing history from {}", I::NAME); - - let mut importer = I::new().await?; - let len = importer.entries().await.unwrap(); - let mut loader = HistoryImporter::new(db, len); - importer.load(&mut loader).await?; - loader.flush().await?; - - println!("Import complete!"); - Ok(()) -} |
