Skip to content

Commit

Permalink
feat: add env substitution (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatisseB committed Jun 27, 2024
1 parent 9cd89d9 commit 666684e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
91 changes: 91 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ indicatif = "0.17"
serde_json = "1.0"
clap = { version = "4.5", features = ["derive", "env", "cargo"] }
figment = { version = "0.10", features = ["yaml", "env"] }
envsubst = "0.2"

# Local dependencies
logcraft-common = { path = "crates/common", version = "0.1.1" }
Expand Down
9 changes: 9 additions & 0 deletions crates/common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ pub fn ensure_kebab_case(name: &str) -> Result<&str> {

Ok(name)
}

pub fn env_forbidden_chars(s: &str) -> bool {
for c in s.chars() {
if c == '$' || c == '{' || c == '}' {
return true;
}
}
false
}
28 changes: 25 additions & 3 deletions src/bin/lgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use anyhow::Result;
use clap::builder::styling;
use clap::{crate_version, Subcommand};
use clap::{CommandFactory, FromArgMatches, Parser};
use figment::providers::{Format, Yaml};
use figment::providers::{Env, Format, Yaml};
use figment::Figment;
use lgc::commands::{
deploy::DeployCommand, destroy::DestroyCommand, diff::DiffCommand,
environments::EnvironmentsCommands, init::InitCommand, plugins::PluginsCommands,
services::ServicesCommands, validate::ValidateCommand,
};
use logcraft_common::configuration::{ProjectConfiguration, LGC_CONFIG_PATH};
use logcraft_common::utils::env_forbidden_chars;
use std::collections::HashMap;
use std::{env, fs};
use std::path::PathBuf;
use tracing::Level;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -89,10 +92,29 @@ impl LogCraftCli {
LogCraftCommands::Init(cmd) => return cmd.run(),
_ => {
let configuration_path = PathBuf::from(LGC_CONFIG_PATH);

if configuration_path.is_file() {
let mut configuration_file = fs::read_to_string(configuration_path)?;

// Environment variables substitution
if envsubst::is_templated(&configuration_file) {
configuration_file = envsubst::substitute(
configuration_file,
&env::vars()
.filter_map(|(key, value)| {
if !env_forbidden_chars(&key) && !env_forbidden_chars(&value) {
Some((key, value))
} else {
None
}
})
.collect::<HashMap<String, String>>()
)?;
}

cli.config = match Figment::new()
.merge(Yaml::file(LGC_CONFIG_PATH))
// .merge(Env::prefixed("LGC_"))
.merge(Yaml::string(&configuration_file))
.merge(Env::prefixed("LGC_").split("_"))
.extract()
{
Ok(config) => config,
Expand Down

0 comments on commit 666684e

Please sign in to comment.