aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/command/client/store/rebuild.rs
blob: 6be67cd092579fad5a8abc137737ddec116afe7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use clap::Args;
use eyre::{Result, bail};

use crate::command::client::daemon as daemon_cmd;

use crate::atuin_client::{
    database::ClientSqlite, encryption, history::store::HistoryStore,
    record::sqlite_store::SqliteStore, settings::Settings,
};

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

impl Rebuild {
    pub(crate) async fn run(
        &self,
        settings: &Settings,
        store: SqliteStore,
        database: &ClientSqlite,
    ) -> 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?;
            }

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

        Ok(())
    }

    async fn rebuild_history(
        &self,
        settings: &Settings,
        store: SqliteStore,
        database: &ClientSqlite,
    ) -> Result<()> {
        let encryption_key: [u8; 32] = encryption::load_key(settings)?.into();

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

        history_store.build(database).await?;

        daemon_cmd::emit_event(settings, crate::atuin_daemon::DaemonEvent::HistoryRebuilt).await;

        Ok(())
    }
}