atuin_dotfiles/shell/
bash.rs

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
use std::path::PathBuf;

use crate::store::{var::VarStore, AliasStore};

async fn cached_aliases(path: PathBuf, store: &AliasStore) -> String {
    match tokio::fs::read_to_string(path).await {
        Ok(aliases) => aliases,
        Err(r) => {
            // we failed to read the file for some reason, but the file does exist
            // fallback to generating new aliases on the fly

            store.posix().await.unwrap_or_else(|e| {
                format!("echo 'Atuin: failed to read and generate aliases: \n{r}\n{e}'",)
            })
        }
    }
}

async fn cached_vars(path: PathBuf, store: &VarStore) -> String {
    match tokio::fs::read_to_string(path).await {
        Ok(vars) => vars,
        Err(r) => {
            // we failed to read the file for some reason, but the file does exist
            // fallback to generating new vars on the fly

            store.posix().await.unwrap_or_else(|e| {
                format!("echo 'Atuin: failed to read and generate vars: \n{r}\n{e}'",)
            })
        }
    }
}

/// Return bash dotfile config
///
/// Do not return an error. We should not prevent the shell from starting.
///
/// In the worst case, Atuin should not function but the shell should start correctly.
///
/// While currently this only returns aliases, it will be extended to also return other synced dotfiles
pub async fn alias_config(store: &AliasStore) -> String {
    // First try to read the cached config
    let aliases = atuin_common::utils::dotfiles_cache_dir().join("aliases.bash");

    if aliases.exists() {
        return cached_aliases(aliases, store).await;
    }

    if let Err(e) = store.build().await {
        return format!("echo 'Atuin: failed to generate aliases: {}'", e);
    }

    cached_aliases(aliases, store).await
}

pub async fn var_config(store: &VarStore) -> String {
    // First try to read the cached config
    let vars = atuin_common::utils::dotfiles_cache_dir().join("vars.bash");

    if vars.exists() {
        return cached_vars(vars, store).await;
    }

    if let Err(e) = store.build().await {
        return format!("echo 'Atuin: failed to generate vars: {}'", e);
    }

    cached_vars(vars, store).await
}