fix: allow number/bool in .env.json (#3594)

This commit is contained in:
jdx 2024-12-15 16:35:31 -06:00 committed by GitHub
parent 0a3a25eebc
commit cc356179c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 95 additions and 13 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash
cat <<'EOF' >.mise.toml
cat <<'EOF' >mise.toml
[env]
mise.file = ['.test-env']
EOF
@ -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"

View File

@ -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"

8
e2e/env/test_source vendored
View File

@ -1,8 +0,0 @@
#!/usr/bin/env bash
cat <<'EOF' >.mise.toml
[env]
_.file = 'not_present'
EOF
assert "mise env" # does not error

View File

@ -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"

View File

@ -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};
@ -14,7 +14,7 @@ struct Env<V> {
#[serde(default)]
sops: IndexMap<String, V>,
#[serde(flatten)]
env: EnvMap,
env: IndexMap<String, V>,
}
impl EnvResults {
@ -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 {
@ -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())
}
@ -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())
}