Skip to content

Commit

Permalink
Consider empty environment variables as not set (#96)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Aug 17, 2023
1 parent 7d56861 commit 521789b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/crunchbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ pub(crate) async fn collect_crunchbase_data(
};

// Setup Crunchbase API client if an api key was provided
let cb: Option<DynCB> = if let Ok(api_key) = env::var(CRUNCHBASE_API_KEY) {
let api_key = match env::var(CRUNCHBASE_API_KEY) {
Ok(api_key) if !api_key.is_empty() => Some(api_key),
Ok(_) | Err(_) => None,
};
let cb: Option<DynCB> = if let Some(api_key) = api_key {
Some(Arc::new(CBApi::new(&api_key)?))
} else {
warn!("crunchbase api key not provided: no information will be collected from crunchbase");
Expand Down
15 changes: 10 additions & 5 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ pub(crate) async fn collect_github_data(cache: &Cache, landscape_data: &Landscap
};

// Setup GitHub API clients pool if any tokens have been provided
let tokens: Result<Vec<String>> = env::var(GITHUB_TOKENS)
.map(|t| t.split(',').map(ToString::to_string).collect())
.map_err(Into::into);
let gh_pool: Option<Pool<DynGH>> = if let Ok(tokens) = &tokens {
let tokens: Option<Vec<String>> = match env::var(GITHUB_TOKENS) {
Ok(tokens) if !tokens.is_empty() => Some(tokens.split(',').map(ToString::to_string).collect()),
Ok(_) | Err(_) => None,
};
let gh_pool: Option<Pool<DynGH>> = if let Some(tokens) = &tokens {
let mut gh_clients: Vec<DynGH> = vec![];
for token in tokens {
let gh = Box::new(GHApi::new(token)?);
Expand All @@ -73,7 +74,11 @@ pub(crate) async fn collect_github_data(cache: &Cache, landscape_data: &Landscap
urls.dedup();

// Collect repositories information from GitHub, reusing cached data when available
let concurrency = if let Ok(tokens) = tokens { tokens.len() } else { 1 };
let concurrency = if let Some(tokens) = tokens {
tokens.len()
} else {
1
};
let github_data: GithubData = stream::iter(urls)
.map(|url| async {
let url = url.clone();
Expand Down
20 changes: 19 additions & 1 deletion src/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use futures::stream::{self, StreamExt};
use mime_guess::mime;
use std::{
collections::HashMap,
fs,
env, fs,
path::{Path, PathBuf},
time::Instant,
};
Expand All @@ -33,6 +33,9 @@ pub(crate) async fn deploy(args: &S3Args) -> Result<()> {
info!("deploying landscape website..");
let start = Instant::now();

// Check required environment variables
check_env_vars()?;

// Setup AWS S3 client
let config = aws_config::load_from_env().await;
let s3_client = aws_sdk_s3::Client::new(&config);
Expand All @@ -52,6 +55,21 @@ pub(crate) async fn deploy(args: &S3Args) -> Result<()> {
Ok(())
}

/// Check that the required environment variables have been provided.
#[instrument(skip_all, err)]
fn check_env_vars() -> Result<()> {
let required_env_vars = ["AWS_REGION", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"];

for var in required_env_vars {
let result = env::var(var);
if result.is_err() || result.expect("var to be set").is_empty() {
return Err(format_err!("required environment variable {var} not provided"));
}
}

Ok(())
}

/// Get objects already deployed, returning their key and the creation date of
/// the object.
#[instrument(skip_all, err)]
Expand Down

0 comments on commit 521789b

Please sign in to comment.