-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make more parameter available to read from env
- Loading branch information
1 parent
47984af
commit 04438e5
Showing
10 changed files
with
62 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,15 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::traits::PreflightCheck; | ||
use crate::extension::string_or_env; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct PostgreSQLConnection { | ||
#[serde(deserialize_with = "string_or_env")] | ||
pub host: String, | ||
#[serde(deserialize_with = "string_or_env")] | ||
pub port: String, | ||
#[serde(deserialize_with = "string_or_env")] | ||
pub user: String, | ||
#[serde(deserialize_with = "string_or_env")] | ||
pub password: String, | ||
} | ||
|
||
impl PostgreSQLConnection { | ||
pub fn parse(&self) -> PostgreSQLConnection { | ||
PostgreSQLConnection { | ||
host: Self::from_env_or_default(self.host.clone()), | ||
port: Self::from_env_or_default(self.port.clone()), | ||
user: Self::from_env_or_default(self.user.clone()), | ||
password: Self::from_env_or_default(self.password.clone()), | ||
} | ||
} | ||
|
||
fn from_env_or_default(val: String) -> String { | ||
if val.starts_with("env=") { | ||
let env_var = val.split('=').collect::<Vec<&str>>()[1]; | ||
match std::env::var(env_var) { | ||
Ok(val) => val, | ||
Err(_) => panic!("Environment variable {} not found", env_var), | ||
} | ||
} else { | ||
val | ||
} | ||
} | ||
} | ||
|
||
impl PreflightCheck for PostgreSQLConnection { | ||
async fn preflight_check(&self) -> Result<(), String> { | ||
let conn = self.parse(); | ||
|
||
check_empty(conn.host, "Host is empty")?; | ||
check_empty(conn.port, "Port is empty")?; | ||
check_empty(conn.user, "User is empty")?; | ||
check_empty(conn.password, "Password is empty")?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn check_empty(field: String, error_message: &str) -> Result<(), String> { | ||
if field.is_empty() { | ||
return Err(error_message.to_string()); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use serde::{Deserialize, Deserializer}; | ||
|
||
pub fn string_or_env<'de, D>(deserializer: D) -> Result<String, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
use serde::de::Error; | ||
let value = String::deserialize(deserializer)?; | ||
match value.strip_prefix("env=") { | ||
Some(key) => match std::env::var(key) { | ||
Ok(val) => { | ||
if val.is_empty() { | ||
Err(D::Error::custom(format!( | ||
"Environment variable {} is empty", | ||
key | ||
))) | ||
} else { | ||
Ok(val) | ||
} | ||
} | ||
Err(_) => Err(D::Error::custom(format!( | ||
"Environment variable {} not found", | ||
key | ||
))), | ||
}, | ||
None => Ok(value), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod deserializer; | ||
|
||
pub use deserializer::string_or_env; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters