atuin_dotfiles/shell/
bash.rs

1use std::path::PathBuf;
2
3use crate::store::{var::VarStore, AliasStore};
4
5async fn cached_aliases(path: PathBuf, store: &AliasStore) -> String {
6    match tokio::fs::read_to_string(path).await {
7        Ok(aliases) => aliases,
8        Err(r) => {
9            // we failed to read the file for some reason, but the file does exist
10            // fallback to generating new aliases on the fly
11
12            store.posix().await.unwrap_or_else(|e| {
13                format!("echo 'Atuin: failed to read and generate aliases: \n{r}\n{e}'",)
14            })
15        }
16    }
17}
18
19async fn cached_vars(path: PathBuf, store: &VarStore) -> String {
20    match tokio::fs::read_to_string(path).await {
21        Ok(vars) => vars,
22        Err(r) => {
23            // we failed to read the file for some reason, but the file does exist
24            // fallback to generating new vars on the fly
25
26            store.posix().await.unwrap_or_else(|e| {
27                format!("echo 'Atuin: failed to read and generate vars: \n{r}\n{e}'",)
28            })
29        }
30    }
31}
32
33/// Return bash dotfile config
34///
35/// Do not return an error. We should not prevent the shell from starting.
36///
37/// In the worst case, Atuin should not function but the shell should start correctly.
38///
39/// While currently this only returns aliases, it will be extended to also return other synced dotfiles
40pub async fn alias_config(store: &AliasStore) -> String {
41    // First try to read the cached config
42    let aliases = atuin_common::utils::dotfiles_cache_dir().join("aliases.bash");
43
44    if aliases.exists() {
45        return cached_aliases(aliases, store).await;
46    }
47
48    if let Err(e) = store.build().await {
49        return format!("echo 'Atuin: failed to generate aliases: {}'", e);
50    }
51
52    cached_aliases(aliases, store).await
53}
54
55pub async fn var_config(store: &VarStore) -> String {
56    // First try to read the cached config
57    let vars = atuin_common::utils::dotfiles_cache_dir().join("vars.bash");
58
59    if vars.exists() {
60        return cached_vars(vars, store).await;
61    }
62
63    if let Err(e) = store.build().await {
64        return format!("echo 'Atuin: failed to generate vars: {}'", e);
65    }
66
67    cached_vars(vars, store).await
68}