From 403b9c7385acac419535f9dfa03a65070d2097d9 Mon Sep 17 00:00:00 2001 From: ryo watanabe <39510845+susiyaki@users.noreply.github.com> Date: Wed, 3 Nov 2021 17:02:17 +0900 Subject: [PATCH] feature connection alias (#129) * feature connection alias * add database_url_with_name method to Connection * Revert "feature connection alias" This reverts commit 88b70c1c3314c406f0dec9ece5cf466be127361e. * fix struct error * fix review #129 * fix review * fix type error --- src/components/connections.rs | 2 +- src/config.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/components/connections.rs b/src/components/connections.rs index 5f5c57e..659f3db 100644 --- a/src/components/connections.rs +++ b/src/components/connections.rs @@ -89,7 +89,7 @@ impl StatefulDrawableComponent for ConnectionsComponent { let mut connections: Vec = Vec::new(); for c in conns { connections.push( - ListItem::new(vec![Spans::from(Span::raw(c.database_url()?))]) + ListItem::new(vec![Spans::from(Span::raw(c.database_url_with_name()?))]) .style(Style::default()), ) } diff --git a/src/config.rs b/src/config.rs index a7f116a..3d41831 100644 --- a/src/config.rs +++ b/src/config.rs @@ -51,6 +51,7 @@ impl Default for Config { Self { conn: vec![Connection { r#type: DatabaseType::MySql, + name: None, user: Some("root".to_string()), host: Some("localhost".to_string()), port: Some(3306), @@ -67,6 +68,7 @@ impl Default for Config { #[derive(Debug, Deserialize, Clone)] pub struct Connection { r#type: DatabaseType, + name: Option, user: Option, host: Option, port: Option, @@ -265,6 +267,19 @@ impl Connection { } } + pub fn database_url_with_name(&self) -> anyhow::Result { + let database_url = self.database_url()?; + + Ok(match &self.name { + Some(name) => format!( + "[{name}] {database_url}", + name = name, + database_url = database_url + ), + None => database_url, + }) + } + pub fn is_mysql(&self) -> bool { matches!(self.r#type, DatabaseType::MySql) }