diff options
Diffstat (limited to 'atuin-dotfiles/src/shell/fish.rs')
| -rw-r--r-- | atuin-dotfiles/src/shell/fish.rs | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/atuin-dotfiles/src/shell/fish.rs b/atuin-dotfiles/src/shell/fish.rs index c6277f34..bf4e1a3b 100644 --- a/atuin-dotfiles/src/shell/fish.rs +++ b/atuin-dotfiles/src/shell/fish.rs @@ -1,12 +1,40 @@ -use super::Alias; - // Configuration for fish -pub fn build(aliases: &[Alias]) -> String { - let mut config = String::new(); +use std::path::PathBuf; + +use crate::store::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}'",) + }) + } + } +} + +/// Return fish 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 config(store: &AliasStore) -> String { + // First try to read the cached config + let aliases = atuin_common::utils::dotfiles_cache_dir().join("aliases.fish"); + + if aliases.exists() { + return cached_aliases(aliases, store).await; + } - for alias in aliases { - config.push_str(&format!("alias {}='{}'\n", alias.name, alias.value)); + if let Err(e) = store.build().await { + return format!("echo 'Atuin: failed to generate aliases: {}'", e); } - config + cached_aliases(aliases, store).await } |
