Skip to content

Commit

Permalink
fix: allow number/bool in .env.json (jdx#3594)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored and miguelmig committed Dec 21, 2024
1 parent 1ea03a4 commit 3c472e7
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 13 deletions.
File renamed without changes.
17 changes: 16 additions & 1 deletion e2e/cli/test_env_file → e2e/env/test_env_file
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

cat <<'EOF' >.mise.toml
cat <<'EOF' >mise.toml
[env]
mise.file = ['.test-env']
EOF
Expand All @@ -10,3 +10,18 @@ echo TEST_ENV2=foo >.test-env2

assert "mise x -- env | grep FOO_FROM_FILE" "FOO_FROM_FILE=foo_from_file"
assert "MISE_ENV_FILE=.test-env2 mise x -- env | grep TEST_ENV2" "TEST_ENV2=foo"

cat <<'EOF' >mise.toml
[env]
_.file = 'not_present'
EOF
assert "mise env" # does not error

cat <<'EOF' >mise.toml
[env]
_.file = ['a', 'b.json']
EOF
echo 'export A=1' >a
echo '{"B": 2}' >b.json
assert "mise env | grep -v PATH" "export A=1
export B=2"
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions e2e/cli/test_env_path → e2e/env/test_env_path
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ mise use dummy@2.0.0
assert_contains "mise env -s bash | grep PATH" "/installs/dummy/2.0.0/bin"

assert_contains "mise env -s bash dummy@1.0.1 | grep PATH" "/installs/dummy/1.0.1/bin"

cat <<'EOF' >mise.toml
[env]
_.path = ['a', 'b']
EOF
assert "mise dr path" "$PWD/a
$PWD/b"
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 0 additions & 8 deletions e2e/env/test_source

This file was deleted.

41 changes: 41 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,47 @@
"vars": {
"description": "variables to set",
"type": "object",
"properties": {
"_": {
"description": "vars modules",
"additionalProperties": true,
"properties": {
"file": {
"oneOf": [
{
"description": "dotenv file to load",
"type": "string"
},
{
"description": "dotenv files to load",
"items": {
"description": "dotenv file to load",
"type": "string"
},
"type": "array"
}
]
},
"source": {
"oneOf": [
{
"description": "bash script to load",
"type": "string"
},
{
"description": "bash scripts to load",
"items": {
"description": "bash script to load",
"type": "string"
},
"type": "array"
}
]
}
},
"type": "object"
}
},
"additionalProperties": {
"description": "value of variable",
"type": "string"
Expand Down
35 changes: 31 additions & 4 deletions src/config/env_directive/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::env_directive::EnvResults;
use crate::file::display_path;
use crate::{file, sops, Result};
use eyre::{eyre, WrapErr};
use eyre::{bail, eyre, WrapErr};
use indexmap::IndexMap;
use rops::file::format::{JsonFileFormat, YamlFileFormat};
use std::path::{Path, PathBuf};
Expand All @@ -14,7 +14,7 @@ struct Env<V> {
#[serde(default)]
sops: IndexMap<String, V>,
#[serde(flatten)]
env: EnvMap,
env: IndexMap<String, V>,
}

impl EnvResults {
Expand All @@ -38,6 +38,7 @@ impl EnvResults {
let new_vars = match ext.as_str() {
"json" => Self::json(&p, parse_template)?,
"yaml" => Self::yaml(&p, parse_template)?,
"toml" => unimplemented!("toml"),
_ => Self::dotenv(&p)?,
};
for (k, v) in new_vars {
Expand All @@ -59,7 +60,20 @@ impl EnvResults {
let raw = sops::decrypt::<_, JsonFileFormat>(&raw, parse_template)?;
f = serde_json::from_str(&raw).wrap_err_with(errfn)?;
}
Ok(f.env)
f.env
.into_iter()
.map(|(k, v)| {
Ok((
k,
match v {
serde_json::Value::String(s) => s,
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::Bool(b) => b.to_string(),
_ => bail!("unsupported json value: {v:?}"),
},
))
})
.collect()
} else {
Ok(EnvMap::new())
}
Expand All @@ -76,7 +90,20 @@ impl EnvResults {
let raw = sops::decrypt::<_, YamlFileFormat>(&raw, parse_template)?;
f = serde_yaml::from_str(&raw).wrap_err_with(errfn)?;
}
Ok(f.env)
f.env
.into_iter()
.map(|(k, v)| {
Ok((
k,
match v {
serde_yaml::Value::String(s) => s,
serde_yaml::Value::Number(n) => n.to_string(),
serde_yaml::Value::Bool(b) => b.to_string(),
_ => bail!("unsupported yaml value: {v:?}"),
},
))
})
.collect()
} else {
Ok(EnvMap::new())
}
Expand Down

0 comments on commit 3c472e7

Please sign in to comment.