Skip to content

Commit

Permalink
Default support for env variables
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Jul 29, 2024
1 parent 1a97874 commit 63e00bd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
13 changes: 12 additions & 1 deletion crates/factor-variables/src/spin_cli/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ const DEFAULT_ENV_PREFIX: &str = "SPIN_VARIABLE";

type EnvFetcherFn = Box<dyn Fn(&str) -> Result<String, VarError> + Send + Sync>;

/// A config Provider that uses environment variables.
/// A [`Provider`] that uses environment variables.
pub struct EnvVariablesProvider {
prefix: Option<String>,
env_fetcher: EnvFetcherFn,
dotenv_path: Option<PathBuf>,
dotenv_cache: OnceLock<HashMap<String, String>>,
}

impl Default for EnvVariablesProvider {
fn default() -> Self {
Self {
prefix: None,
env_fetcher: Box::new(|s| std::env::var(s)),
dotenv_path: Some(".env".into()),
dotenv_cache: Default::default(),
}
}
}

impl EnvVariablesProvider {
/// Creates a new EnvProvider.
///
Expand Down
23 changes: 14 additions & 9 deletions crates/factor-variables/src/spin_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
mod env;
mod statik;

pub use env::*;
pub use statik::*;

use serde::Deserialize;
use spin_expressions::Provider;
use spin_factors::anyhow;
use statik::StaticVariablesProvider;

use crate::runtime_config::RuntimeConfig;

/// Resolves a runtime configuration for the variables factor from a TOML table.
pub fn runtime_config_from_toml(table: &toml::Table) -> anyhow::Result<Option<RuntimeConfig>> {
pub fn runtime_config_from_toml(table: &toml::Table) -> anyhow::Result<RuntimeConfig> {
// Always include the environment variable provider.
let mut providers = vec![Box::new(EnvVariablesProvider::default()) as _];
let Some(array) = table.get("variable_provider") else {
return Ok(None);
return Ok(RuntimeConfig { providers });
};

let provider_configs: Vec<VariableProviderConfiguration> = array.clone().try_into()?;
let providers = provider_configs
.into_iter()
.map(VariableProviderConfiguration::into_provider)
.collect();
Ok(Some(RuntimeConfig { providers }))
providers.extend(
provider_configs
.into_iter()
.map(VariableProviderConfiguration::into_provider),
);
Ok(RuntimeConfig { providers })
}

/// A runtime configuration used in the Spin CLI for one type of variable provider.
Expand All @@ -31,7 +36,7 @@ pub enum VariableProviderConfiguration {
/// A static provider of variables.
Static(StaticVariablesProvider),
/// An environment variable provider.
Env(env::EnvVariablesConfig),
Env(EnvVariablesConfig),
}

impl VariableProviderConfiguration {
Expand Down
2 changes: 1 addition & 1 deletion crates/factor-variables/src/spin_cli/statik.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Deserialize;
use spin_expressions::{async_trait::async_trait, Key, Provider};
use spin_factors::anyhow;

/// A variables provider that reads variables from an static map.
/// A [`Provider`] that reads variables from an static map.
#[derive(Debug, Deserialize, Clone)]
pub struct StaticVariablesProvider {
values: Arc<HashMap<String, String>>,
Expand Down
4 changes: 2 additions & 2 deletions crates/factor-variables/tests/factor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct TestFactors {
variables: VariablesFactor,
}

#[tokio::test]
#[tokio::test(flavor = "multi_thread")]
async fn static_provider_works() -> anyhow::Result<()> {
let factors = TestFactors {
variables: VariablesFactor::default(),
Expand Down Expand Up @@ -58,7 +58,7 @@ impl FactorRuntimeConfigSource<VariablesFactor> for TomlConfig {
fn get_runtime_config(
&mut self,
) -> anyhow::Result<Option<<VariablesFactor as Factor>::RuntimeConfig>> {
spin_cli::runtime_config_from_toml(&self.table)
spin_cli::runtime_config_from_toml(&self.table).map(Some)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/factors/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl FactorRuntimeConfigSource<VariablesFactor> for TestSource {
[variable_provider.values]
foo = "bar"
})
.map(Some)
}
}

Expand Down

0 comments on commit 63e00bd

Please sign in to comment.