Skip to content

Commit

Permalink
feat: make more parameter available to read from env
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisaAkiron committed Mar 8, 2024
1 parent 47984af commit 04438e5
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 53 deletions.
3 changes: 3 additions & 0 deletions example/storage/azure-blob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ storage:
type: "Azure"

# Azure Storage Account name
# Use env=ENV_NAME to get the value from the environment variable
account: ""

# Azure Storage Account access key
# Use env=ENV_NAME to get the value from the environment variable
access_key: ""

# Azure Blob Container name
# Use env=ENV_NAME to get the value from the environment variable
container: ""

# Azure Blob Path in the container
Expand Down
5 changes: 5 additions & 0 deletions example/storage/s3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ storage:
type: "S3"

# S3 Access key
# Use env=ENV_NAME to get the value from the environment variable
access_key: ""

# S3 Secret key
# Use env=ENV_NAME to get the value from the environment variable
access_secret: ""

# S3 Endpoint
# Use env=ENV_NAME to get the value from the environment variable
endpoint: ""

# S3 Region
# Use env=ENV_NAME to get the value from the environment variable
region: ""

# S3 Bucket
# Use env=ENV_NAME to get the value from the environment variable
bucket: ""

# S3 Enforce path style
Expand Down
49 changes: 5 additions & 44 deletions src/configs/connection/postgresql.rs
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(())
}
1 change: 0 additions & 1 deletion src/configs/database/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl From<&String> for PostgreSQLDatabase {
impl PreflightCheck for PostgreSQL {
async fn preflight_check(&self) -> Result<(), String> {
self.client.preflight_check().await?;
self.connection.preflight_check().await?;

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion src/configs/storage/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use log::{debug, error, info};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};

use crate::traits::Storage;
use crate::{extension::string_or_env, traits::Storage};

#[derive(Debug, Serialize, Deserialize)]
pub struct Azure {
path: Option<String>,
#[serde(deserialize_with = "string_or_env")]
account: String,
#[serde(deserialize_with = "string_or_env")]
access_key: String,
#[serde(deserialize_with = "string_or_env")]
container: String,
}

Expand Down
7 changes: 6 additions & 1 deletion src/configs/storage/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ use log::{debug, error, info};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};

use crate::traits::Storage;
use crate::{extension::string_or_env, traits::Storage};

#[derive(Debug, Serialize, Deserialize)]
pub struct S3 {
#[serde(deserialize_with = "string_or_env")]
pub access_key: String,
#[serde(deserialize_with = "string_or_env")]
pub access_secret: String,
#[serde(deserialize_with = "string_or_env")]
pub endpoint: String,
#[serde(deserialize_with = "string_or_env")]
pub region: String,
#[serde(deserialize_with = "string_or_env")]
pub bucket: String,
pub path: Option<String>,
pub enforce_path_style: bool,
Expand Down
28 changes: 28 additions & 0 deletions src/extension/deserializer.rs
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),
}
}
3 changes: 3 additions & 0 deletions src/extension/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod deserializer;

pub use deserializer::string_or_env;
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::traits::PreflightCheck;

mod configs;
mod enums;
mod extension;
mod job;
mod provider;
mod traits;
Expand Down
13 changes: 7 additions & 6 deletions src/provider/pg_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ impl Dump<PgDump, PostgreSQLConnection> for PostgreSQLDatabase {
None => &pg_dump.format,
};

let conn = &connection.parse();

let mut binding = Command::new(&pg_dump.binary_path);
let cmd = binding
.arg(format!("--host={host}", host = &conn.host))
.arg(format!("--port={port}", port = &conn.port))
.arg(format!("--username={username}", username = &conn.user))
.arg(format!("--host={host}", host = &connection.host))
.arg(format!("--port={port}", port = &connection.port))
.arg(format!(
"--username={username}",
username = &connection.user
))
.arg(format!("--format={format}", format = format))
.arg(format!("--file={output}", output = output));

Expand Down Expand Up @@ -175,7 +176,7 @@ impl Dump<PgDump, PostgreSQLConnection> for PostgreSQLDatabase {
let commands = format!("{:?}", cmd);
trace!("Executing pg_dump command: {}", commands);

cmd.env("PGPASSWORD", &conn.password);
cmd.env("PGPASSWORD", &connection.password);

let output = cmd.output().await.expect("Failed to execute pg_dump");

Expand Down

0 comments on commit 04438e5

Please sign in to comment.