From 6a885cf4abaf03ab9bf235db498e11499a153747 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Wed, 21 Feb 2024 11:54:46 +0000 Subject: [PATCH] feat: add alias feedback and list command (#1747) --- atuin/src/command/client/config/alias.rs | 35 ++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/atuin/src/command/client/config/alias.rs b/atuin/src/command/client/config/alias.rs index 9d34d84a..56f24c12 100644 --- a/atuin/src/command/client/config/alias.rs +++ b/atuin/src/command/client/config/alias.rs @@ -3,23 +3,54 @@ use eyre::{Context, Result}; use atuin_client::{encryption, record::sqlite_store::SqliteStore, settings::Settings}; -use atuin_config::store::AliasStore; +use atuin_config::{shell::Alias, store::AliasStore}; #[derive(Subcommand, Debug)] #[command(infer_subcommands = true)] pub enum Cmd { Set { name: String, value: String }, Delete { name: String }, + List, } impl Cmd { async fn set(&self, store: AliasStore, name: String, value: String) -> Result<()> { + let aliases = store.aliases().await?; + let found: Vec = aliases.into_iter().filter(|a| a.name == name).collect(); + + if found.is_empty() { + println!("Aliasing {name}={value}"); + } else { + println!( + "Overwriting alias {name}={} with {name}={value}", + found[0].value + ); + } + store.set(&name, &value).await?; Ok(()) } + async fn list(&self, store: AliasStore) -> Result<()> { + let aliases = store.aliases().await?; + + for i in aliases { + println!("{}={}", i.name, i.value); + } + + Ok(()) + } + async fn delete(&self, store: AliasStore, name: String) -> Result<()> { + let aliases = store.aliases().await?; + let found = aliases.into_iter().any(|a| a.name == name); + + if !found { + eprintln!("Alias not found - \"{name}\" - could not delete"); + return Ok(()); + } + store.delete(&name).await?; Ok(()) @@ -35,8 +66,8 @@ impl Cmd { match self { Self::Set { name, value } => self.set(alias_store, name.clone(), value.clone()).await, - Self::Delete { name } => self.delete(alias_store, name.clone()).await, + Self::List => self.list(alias_store).await, } } }