atuin 18.24.1

atuin - magical shell history
use atuin_client::database::Sqlite;
use atuin_client::history::store::HistoryStore;
use atuin_client::record::sqlite_store::SqliteStore;
use atuin_client::settings::Settings;
use atuin_common::encryption::paseto_v4;
use atuin_scripts::store::ScriptStore;
use clap::Args;
use eyre::{Context as _, Result, bail};

#[cfg(feature = "daemon")]
use crate::command::client::daemon;

#[derive(Args, Debug)]
pub struct Rebuild {
    pub tag: String,
}

impl Rebuild {
    pub async fn run(
        &self,
        settings: &Settings,
        store: SqliteStore,
        database: &Sqlite,
    ) -> Result<()> {
        // keep it as a string and not an enum atm
        // would be super cool to build this dynamically in the future
        // eg register handles for rebuilding various tags without having to make this part of the
        // binary big
        match self.tag.as_str() {
            "history" => {
                self.rebuild_history(settings, store.clone(), database).await?;
            }

            "scripts" => {
                self.rebuild_scripts(settings, store.clone()).await?;
            }

            // The daemon owns the sidecar, and rebuilds it. `ai-session` is the threads' record tag.
            "threads" | "ai-session" => {
                super::reset_ai_sessions(settings).await?;
                println!("The daemon is rebuilding the threads index");
            }

            tag => {
                bail!("unknown tag: {tag}");
            }
        }

        Ok(())
    }

    /// The daemon owns the rebuild when enabled; otherwise we rebuild locally.
    async fn rebuild_history(
        &self,
        settings: &Settings,
        store: SqliteStore,
        database: &Sqlite,
    ) -> Result<()> {
        #[cfg(feature = "daemon")]
        if settings.daemon.enabled {
            daemon::rebuild_history(settings).await?;
            return Ok(());
        }

        let encryption_key = paseto_v4::Key::try_load_from_path(&settings.key_path)
            .context("could not load encryption key")?;

        let host_id = settings.host_id().await?;
        let history_store = HistoryStore::new(store, host_id, encryption_key);

        history_store.build(database).await?;

        Ok(())
    }

    async fn rebuild_scripts(&self, settings: &Settings, store: SqliteStore) -> Result<()> {
        let encryption_key = paseto_v4::Key::try_load_from_path(&settings.key_path)
            .context("could not load encryption key")?;
        let host_id = settings.host_id().await?;
        let script_store = ScriptStore::new(store, host_id, encryption_key);
        let database = atuin_scripts::database::Database::new(
            settings.scripts.db_path.clone(),
            std::time::Duration::from_secs(1),
        )
        .await?;

        script_store.build(database).await?;

        Ok(())
    }
}