Skip to content

Commit

Permalink
Reported name of missing environment variable
Browse files Browse the repository at this point in the history
The getenv function ican be used as a wrapper around std::env::var() to
quickly see the name of any missing environment variable.

(NOTE: solution found by Manuel Carrer!)
  • Loading branch information
jo-asplin-met-no committed Feb 10, 2025
1 parent 5dae474 commit 11fd161
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
7 changes: 6 additions & 1 deletion ingestion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ pub enum Error {
#[error("RwLock was poisoned: {0}")]
Lock(String),
#[error("Could not read environment variable: {0}")]
Env(#[from] std::env::VarError),
Env(String),
}

/// Gets an environment variable, providing more details than calling std::env::var() directly.
pub fn getenv(key: &str) -> Result<String, Error> {
std::env::var(key).map_err(|_| Error::Env(format!("Environment variable not found: {}", key)))
}

impl PartialEq for Error {
Expand Down
4 changes: 2 additions & 2 deletions ingestion/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rove_connector::Connector;
use std::sync::{Arc, RwLock};
use tokio_postgres::NoTls;

use lard_ingestion::permissions;
use lard_ingestion::{getenv, permissions};

const PARAMCONV: &str = "resources/paramconversions.csv";

Expand All @@ -27,7 +27,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

// Set up postgres connection pool
let manager =
PostgresConnectionManager::new_from_stringlike(std::env::var("LARD_CONN_STRING")?, NoTls)?;
PostgresConnectionManager::new_from_stringlike(getenv("LARD_CONN_STRING")?, NoTls)?;
let db_pool = bb8::Pool::builder().build(manager).await?;

// QC system
Expand Down
5 changes: 2 additions & 3 deletions ingestion/src/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Error;
use crate::{getenv, Error};
use std::{
collections::HashMap,
sync::{Arc, RwLock},
Expand Down Expand Up @@ -41,8 +41,7 @@ pub type StationPermitTable = HashMap<StationId, PermitId>;
/// Get a fresh cache of permits from stinfosys
pub async fn fetch_permits() -> Result<(ParamPermitTable, StationPermitTable), Error> {
// get stinfo conn
let (client, conn) =
tokio_postgres::connect(&std::env::var("STINFO_CONN_STRING")?, NoTls).await?;
let (client, conn) = tokio_postgres::connect(&getenv("STINFO_CONN_STRING")?, NoTls).await?;

// conn object independently performs communication with database, so needs it's own task.
// it will return when the client is dropped
Expand Down

0 comments on commit 11fd161

Please sign in to comment.