aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/command/client/store/pull.rs
blob: 3a0865bee0d250e653584a435783a0b3816de6e3 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use clap::Args;
use eyre::Result;

use crate::atuin_client::{
    database::ClientSqlite,
    encryption::load_key,
    record::{
        sqlite_store::SqliteStore,
        sync::{self, Operation},
    },
    settings::Settings,
};

#[derive(Args, Debug)]
pub(crate) struct Pull {
    /// The tag to push (eg, 'history'). Defaults to all tags
    #[arg(long, short)]
    pub(crate) tag: Option<String>,

    /// Force push records
    /// This will first wipe the local store, and then download all records from the remote
    #[arg(long, default_value = "false")]
    pub(crate) force: bool,

    /// Page Size
    /// How many records to download at once. Defaults to 100
    #[arg(long, default_value = "100")]
    pub(crate) page: u64,
}

impl Pull {
    pub(crate) async fn run(
        &self,
        settings: &Settings,
        store: SqliteStore,
        db: &ClientSqlite,
    ) -> Result<()> {
        if self.force {
            println!("Forcing local overwrite!");
            println!("Clearing local store");

            store.delete_all().await?;
        }

        // We can actually just use the existing diff/etc to push
        // 1. Diff
        // 2. Get operations
        // 3. Filter operations by
        //  a) are they a download op?
        //  b) are they for the host/tag we are pushing here?
        let client = sync::build_client(settings)?;
        let (diff, remote_index) = sync::diff(&client, &store).await?;

        // Skip on --force: local was already wiped above, mismatch is the user's call.
        if !self.force {
            let key: [u8; 32] = load_key(settings)?.into();
            sync::check_encryption_key(&client, &remote_index, &key)
                .await
                .map_err(crate::print_error::format_sync_error)?;
        }

        let operations = sync::operations(diff, &store)?;

        let operations = operations
            .into_iter()
            .filter(|op| match op {
                // No noops or downloads thx
                Operation::Noop { .. } | Operation::Upload { .. } => false,

                // pull, so yes plz to downloads!
                Operation::Download { tag, .. } => {
                    if self.force {
                        return true;
                    }

                    if let Some(t) = self.tag.clone()
                        && t != *tag
                    {
                        return false;
                    }

                    true
                }
            })
            .collect();

        let (_, downloaded) = sync::sync_remote(&client, operations, &store, self.page).await?;

        println!("Downloaded {} records", downloaded.len());

        crate::sync::build(settings, &store, db, Some(&downloaded)).await?;

        Ok(())
    }
}