Skip to content

Commit

Permalink
Merge pull request #367 from chainbound/lore/fix/config
Browse files Browse the repository at this point in the history
fix!(config): remove empty environment variables set like 'VAR=' when parsing options
  • Loading branch information
thedevbirb authored Nov 8, 2024
2 parents dccb14d + 16205d2 commit 4ad631f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
10 changes: 9 additions & 1 deletion bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use clap::Parser;
use eyre::{bail, Result};
use tracing::info;

use bolt_sidecar::{config::Opts, telemetry::init_telemetry_stack, SidecarDriver};
use bolt_sidecar::{
config::{strip_empty_envs, Opts},
telemetry::init_telemetry_stack,
SidecarDriver,
};

const BOLT: &str = r#"
██████╗ ██████╗ ██╗ ████████╗
Expand All @@ -14,6 +18,10 @@ const BOLT: &str = r#"

#[tokio::main]
async fn main() -> Result<()> {
if dotenvy::dotenv().is_ok() {
strip_empty_envs()?;
info!("Loaded .env file");
}
let opts = Opts::parse();

if let Err(err) = init_telemetry_stack(opts.telemetry.metrics_port()) {
Expand Down
28 changes: 26 additions & 2 deletions bolt-sidecar/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub const DEFAULT_CONSTRAINTS_PROXY_PORT: u16 = 18550;

/// Command-line options for the Bolt sidecar
#[derive(Debug, Parser, Deserialize)]
#[clap(trailing_var_arg = true)]
pub struct Opts {
/// Port to listen on for incoming JSON-RPC requests of the Commitments API.
/// This port should be open on your firewall in order to receive external requests!
Expand Down Expand Up @@ -96,15 +95,40 @@ pub struct Opts {
/// Additional unrecognized arguments. Useful for CI and testing
/// to avoid issues on potential extra flags provided (e.g. "--exact" from cargo nextest).
#[cfg(test)]
#[clap(allow_hyphen_values = true)]
#[clap(allow_hyphen_values = true, trailing_var_arg = true)]
#[serde(default)]
pub extra_args: Vec<String>,
}

/// It removes environment variables that are set as empty strings, i.e. like `MY_VAR=`. This is
/// useful to avoid unexpected edge cases and because we don't have options that make sense with an
/// empty string value.
pub fn strip_empty_envs() -> eyre::Result<()> {
for item in dotenvy::dotenv_iter()? {
let (key, val) = item?;
if val.is_empty() {
std::env::remove_var(key)
}
}

Ok(())
}

#[cfg(test)]
mod tests {
use dotenvy::dotenv;

use super::*;

#[test]
#[ignore = "Doesn't need to run in CI, only for local development"]
fn test_strip_empty_envs() {
let _ = dotenv().expect("to load .env file");
strip_empty_envs().expect("to strip empty envs");
let opts = Opts::parse();
println!("{:#?}", opts);
}

#[test]
fn test_validate_cli_flags() {
use clap::CommandFactory;
Expand Down

0 comments on commit 4ad631f

Please sign in to comment.