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

feat: add env substitution #12

Merged
merged 1 commit into from
Jun 27, 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
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