Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!(config): remove empty environment variables set like 'VAR=' when parsing options #367

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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