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

fix: allow passing integers to task env #3177

Merged
merged 1 commit into from
Nov 24, 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
4 changes: 4 additions & 0 deletions e2e/tasks/test_task_run_toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ run = 'echo "{{arg()}} {{flag(name="force")}} {{option(name="user")}}"'
run = 'echo {{arg(default="arg1")}} {{option(name="user", default="user1")}}'
[tasks.test-with-template]
run = 'echo {{1 + 1}}'
[tasks.int]
env = { DEBUG = 0 } # should be parsed as a string
run = 'echo DEBUG=\$DEBUG'
EOF

assert "mise run multi c" "a
Expand All @@ -23,6 +26,7 @@ assert "mise run test-with-args foo --force --user=user" "foo true user"
assert "mise run test-with-defaults" "arg1 user1"
assert "mise run test-with-defaults arg2 --user=user2" "arg2 user2"
assert "mise run test-with-template" "2"
assert "mise run int" "DEBUG=0"

cat <<EOF >mise.windows.toml
tasks.configtask = "echo configtask:windows"
Expand Down
21 changes: 14 additions & 7 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::cmd::CmdLineRunner;
use crate::config::{CONFIG, SETTINGS};
use crate::errors::Error;
use crate::file::display_path;
use crate::task::{Deps, GetMatchingExt, Task};
use crate::task::{Deps, EitherIntOrBool, GetMatchingExt, Task};
use crate::toolset::{InstallOptions, ToolsetBuilder};
use crate::ui::{ctrlc, prompt, style, time};
use crate::{dirs, env, exit, file, ui};
Expand Down Expand Up @@ -298,21 +298,28 @@ impl Run {
return Ok(());
}

let string_env = task.env.iter().filter_map(|(k, v)| match &v.0 {
Either::Left(v) => Some((k, v)),
_ => None,
});
let string_env: Vec<(String, String)> = task
.env
.iter()
.filter_map(|(k, v)| match &v.0 {
Either::Left(v) => Some((k.to_string(), v.to_string())),
Either::Right(EitherIntOrBool(Either::Left(v))) => {
Some((k.to_string(), v.to_string()))
}
_ => None,
})
.collect_vec();
let rm_env = task
.env
.iter()
.filter(|(_, v)| v.0 == Either::Right(false))
.filter(|(_, v)| v.0 == Either::Right(EitherIntOrBool(Either::Right(false))))
.map(|(k, _)| k)
.collect::<HashSet<_>>();
let env: BTreeMap<String, String> = env
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.chain(string_env)
.filter(|(k, _)| !rm_env.contains(k))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();

let timer = std::time::Instant::now();
Expand Down
22 changes: 13 additions & 9 deletions src/config/config_file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use either::Either;
use serde::de;
use tera::{Context, Tera};

use crate::task::EitherStringOrBool;
use crate::task::{EitherIntOrBool, EitherStringOrIntOrBool};

pub struct TomlParser<'a> {
table: &'a toml::Value,
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<'a> TomlParser<'a> {
pub fn parse_env(
&self,
key: &str,
) -> eyre::Result<Option<BTreeMap<String, EitherStringOrBool>>> {
) -> eyre::Result<Option<BTreeMap<String, EitherStringOrIntOrBool>>> {
self.table
.get(key)
.and_then(|value| value.as_table())
Expand All @@ -64,16 +64,20 @@ impl<'a> TomlParser<'a> {
.map(|(key, value)| {
let v = value
.as_str()
.map(|v| Ok(EitherStringOrBool(Either::Left(self.render_tmpl(v)?))))
.map(|v| {
Ok(EitherStringOrIntOrBool(Either::Left(self.render_tmpl(v)?)))
})
.or_else(|| {
value
.as_integer()
.map(|v| Ok(EitherStringOrBool(Either::Left(v.to_string()))))
value.as_integer().map(|v| {
Ok(EitherStringOrIntOrBool(Either::Left(v.to_string())))
})
})
.or_else(|| {
value
.as_bool()
.map(|v| Ok(EitherStringOrBool(Either::Right(v))))
value.as_bool().map(|v| {
Ok(EitherStringOrIntOrBool(Either::Right(EitherIntOrBool(
Either::Right(v),
))))
})
})
.unwrap_or_else(|| {
Err(eyre::eyre!("invalid env value: {:?}", value))
Expand Down
26 changes: 20 additions & 6 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Task {
#[serde(default)]
pub wait_for: Vec<String>,
#[serde(default)]
pub env: BTreeMap<String, EitherStringOrBool>,
pub env: BTreeMap<String, EitherStringOrIntOrBool>,
#[serde(default)]
pub dir: Option<PathBuf>,
#[serde(default)]
Expand Down Expand Up @@ -82,18 +82,32 @@ pub struct Task {
}

#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct EitherStringOrBool(#[serde(with = "either::serde_untagged")] pub Either<String, bool>);
pub struct EitherStringOrIntOrBool(
#[serde(with = "either::serde_untagged")] pub Either<String, EitherIntOrBool>,
);

impl Display for EitherStringOrBool {
#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct EitherIntOrBool(#[serde(with = "either::serde_untagged")] pub Either<i64, bool>);

impl Display for EitherIntOrBool {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.0 {
Either::Left(i) => write!(f, "{i}"),
Either::Right(b) => write!(f, "{b}"),
}
}
}

impl Display for EitherStringOrIntOrBool {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.0 {
Either::Left(s) => write!(f, "\"{}\"", s),
Either::Right(b) => write!(f, "{}", b),
Either::Left(s) => write!(f, "\"{s}\""),
Either::Right(b) => write!(f, "{b}"),
}
}
}

impl Debug for EitherStringOrBool {
impl Debug for EitherStringOrIntOrBool {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
Expand Down