use crate::shell::{Alias, Var};
use crate::store::{AliasStore, var::VarStore};
use std::path::PathBuf;
async fn cached_aliases(path: PathBuf, store: &AliasStore) -> String {
match tokio::fs::read_to_string(path).await {
Ok(aliases) => aliases,
Err(r) => {
store.powershell().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) => {
store.powershell().await.unwrap_or_else(|e| {
format!("echo 'Atuin: failed to read and generate vars: \n{r}\n{e}'",)
})
}
}
}
pub async fn alias_config(store: &AliasStore) -> String {
let aliases = atuin_common::utils::dotfiles_cache_dir().join("aliases.ps1");
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 {
let vars = atuin_common::utils::dotfiles_cache_dir().join("vars.ps1");
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
}
pub fn format_alias(alias: &Alias) -> String {
let mut result = secure_command(&format!(
"function {} {{\n {}{} @args\n}}",
alias.name,
if alias.value.starts_with(['"', '\'']) {
"& "
} else {
""
},
alias.value
));
result.insert(0, '\n');
result
}
pub fn format_var(var: &Var) -> String {
secure_command(&format!(
"${}{} = '{}'",
if var.export { "env:" } else { "" },
var.name,
var.value.replace("'", "''")
))
}
fn secure_command(command: &str) -> String {
format!(
"Invoke-Expression -ErrorAction Continue -Command '{}'\n",
command.replace("'", "''")
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn aliases() {
assert_eq!(
format_alias(&Alias {
name: "gp".to_string(),
value: "git push".to_string(),
}),
"\n".to_string()
+ &secure_command(
"function gp {
git push @args
}"
)
);
assert_eq!(
format_alias(&Alias {
name: "spc".to_string(),
value: "\"path with spaces\" arg".to_string(),
}),
"\n".to_string()
+ &secure_command(
"function spc {
& \"path with spaces\" arg @args
}"
)
);
}
#[test]
fn vars() {
assert_eq!(
format_var(&Var {
name: "FOO".to_owned(),
value: "bar 'baz'".to_owned(),
export: true,
}),
secure_command("$env:FOO = 'bar ''baz'''")
);
assert_eq!(
format_var(&Var {
name: "TEST".to_owned(),
value: "1".to_owned(),
export: false,
}),
secure_command("$TEST = '1'")
);
}
#[test]
fn invoke_expression() {
assert_eq!(
secure_command("echo 'foo'"),
"Invoke-Expression -ErrorAction Continue -Command 'echo ''foo'''\n"
)
}
}