From 0fb5d77dc6a77b4683a74ecfce865c15d704d1f0 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda <41065217+TaKO8Ki@users.noreply.github.com> Date: Wed, 8 Sep 2021 10:23:32 +0900 Subject: [PATCH] Add password field to config (#74) * create FUNDING.yml * add password field * add a example using password field to README --- README.md | 1 + src/config.rs | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fd0fcc9..2a74430 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ type = "mysql" user = "root" host = "localhost" port = 3306 +password = "password" database = "foo" [[conn]] diff --git a/src/config.rs b/src/config.rs index 3387549..b7c4048 100644 --- a/src/config.rs +++ b/src/config.rs @@ -51,6 +51,7 @@ impl Default for Config { host: Some("localhost".to_string()), port: Some(3306), path: None, + password: None, database: None, }], key_config: KeyConfig::default(), @@ -66,6 +67,7 @@ pub struct Connection { host: Option, port: Option, path: Option, + password: Option, pub database: Option, } @@ -172,18 +174,24 @@ impl Connection { .port .as_ref() .ok_or_else(|| anyhow::anyhow!("type mysql needs the port field"))?; + let password = self + .password + .as_ref() + .map_or(String::new(), |p| p.to_string()); match self.database.as_ref() { Some(database) => Ok(format!( - "mysql://{user}:@{host}:{port}/{database}", + "mysql://{user}:{password}@{host}:{port}/{database}", user = user, + password = password, host = host, port = port, database = database )), None => Ok(format!( - "mysql://{user}:@{host}:{port}", + "mysql://{user}:{password}@{host}:{port}", user = user, + password = password, host = host, port = port, )), @@ -202,18 +210,24 @@ impl Connection { .port .as_ref() .ok_or_else(|| anyhow::anyhow!("type postgres needs the port field"))?; + let password = self + .password + .as_ref() + .map_or(String::new(), |p| p.to_string()); match self.database.as_ref() { Some(database) => Ok(format!( - "postgres://{user}@{host}:{port}/{database}", + "postgres://{user}:{password}@{host}:{port}/{database}", user = user, + password = password, host = host, port = port, database = database )), None => Ok(format!( - "postgres://{user}@{host}:{port}", + "postgres://{user}:{password}@{host}:{port}", user = user, + password = password, host = host, port = port, )),