Skip to content

Commit d7e992e

Browse files
authored
feat(env): support env files in toml (#4618)
Resolves #4372. Since sops doesn't yet support toml (getsops/sops#812), I ignored it for `toml`.
1 parent 97d5305 commit d7e992e

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/config/env_directive/file.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type EnvMap = IndexMap<String, String>;
1111

1212
#[derive(serde::Serialize, serde::Deserialize)]
1313
struct Env<V> {
14-
#[serde(default)]
14+
#[serde(default = "IndexMap::new")]
1515
sops: IndexMap<String, V>,
1616
#[serde(flatten)]
1717
env: IndexMap<String, V>,
@@ -40,7 +40,7 @@ impl EnvResults {
4040
*env = match ext.as_str() {
4141
"json" => Self::json(&p, parse_template)?,
4242
"yaml" => Self::yaml(&p, parse_template)?,
43-
"toml" => unimplemented!("toml"),
43+
"toml" => Self::toml(&p)?,
4444
_ => Self::dotenv(&p)?,
4545
};
4646
}
@@ -107,6 +107,31 @@ impl EnvResults {
107107
}
108108
}
109109

110+
fn toml(p: &Path) -> Result<EnvMap> {
111+
let errfn = || eyre!("failed to parse toml file: {}", display_path(p));
112+
// sops does not support toml yet, so no need to parse sops
113+
if let Ok(raw) = file::read_to_string(p) {
114+
toml::from_str::<Env<toml::Value>>(&raw)
115+
.wrap_err_with(errfn)?
116+
.env
117+
.into_iter()
118+
.map(|(k, v)| {
119+
Ok((
120+
k,
121+
match v {
122+
toml::Value::String(s) => s,
123+
toml::Value::Integer(n) => n.to_string(),
124+
toml::Value::Boolean(b) => b.to_string(),
125+
_ => bail!("unsupported toml value: {v:?}"),
126+
},
127+
))
128+
})
129+
.collect()
130+
} else {
131+
Ok(EnvMap::new())
132+
}
133+
}
134+
110135
fn dotenv(p: &Path) -> Result<EnvMap> {
111136
let errfn = || eyre!("failed to parse dotenv file: {}", display_path(p));
112137
let mut env = EnvMap::new();

0 commit comments

Comments
 (0)