Skip to content

feat: add mise_env tera variable for templates#5002

Merged
jdx merged 4 commits into
jdx:mainfrom
auxesis:feat/add-mise_env-tera-variable
May 5, 2025
Merged

feat: add mise_env tera variable for templates#5002
jdx merged 4 commits into
jdx:mainfrom
auxesis:feat/add-mise_env-tera-variable

Conversation

@auxesis

@auxesis auxesis commented May 4, 2025

Copy link
Copy Markdown
Contributor

Allows the user to query in a template what configuration environment a task is running in:

cat <<EOF >mise.toml
[tasks.print]
run = '{% if mise_env %}echo {{mise_env}}{% endif %}'
EOF
mise run print # output:
MISE_ENV=env1 mise run print # output: [env1]
mise --env env1,env2 run print # output: [env1, env2]

This can be useful when you want to mise set an environment variable for the current configuration environment:

[tasks.test]
run = "mise set --file mise.{{ mise_env | split(pat=',') | last }}.yaml KEY={{arg(name='value')}}"

@auxesis

auxesis commented May 4, 2025

Copy link
Copy Markdown
Contributor Author

Additionally, I'm seeking feedback on the behaviour if an environment isn't specified.

The current implementation always sets mise_env in Tera, even if the user hasn't specified an environment.

When a user hasn't specified an environment, the "undefined variables are considered falsy" Tera convention doesn't work as expected, because mise_env will be an empty string. This leads to all sorts of hoop jumping for users in templates to check the length of mise_env.

I think it may be better to not set mise_env if the environment is not specified, but I'm keen to hear what others think.

Comment thread src/tera.rs Outdated
context.insert("env", &*env::PRISTINE_ENV);
context.insert("mise_bin", &*env::MISE_BIN);
context.insert("mise_pid", &*env::MISE_PID);
context.insert("mise_env", &*env::MISE_ENV.join(","));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case we probably can keep the array type in the template

Allows the user to query what environment a task is running in.
@auxesis auxesis force-pushed the feat/add-mise_env-tera-variable branch from 54e4838 to b91bee8 Compare May 5, 2025 13:15
Allows the use of conditionals:

```
{% if mise_env %}
echo {{ mise_env }}
{% endif %}
```

And easier use of other Tera functions:

```
{% if mise_env %}
echo {{ mise_env | last }}
{% endif %}
```
Comment thread src/tera.rs Outdated
context.insert("env", &*env::PRISTINE_ENV);
context.insert("mise_bin", &*env::MISE_BIN);
context.insert("mise_pid", &*env::MISE_PID);
if !(&*env::MISE_ENV).is_empty() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to get this from settings or something because if it's specified as a CLI argument this will miss that (I think, I could be wrong)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked and it looks like it works:

$ cat <<EOF >mise.toml
[tasks.print]
run = '{% if mise_env %}echo {{mise_env}}{% endif %}'
EOF
$ MISE_ENV=production mise run print
[print] $ echo [production]
[production]
$ mise --env production run print
[print] $ echo [production]
[production]

I believe this is because the environment function in src/env.rs reads from both CLI arguments and environment varables.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness, I have added some additional e2e tests (38eb2a6) that cover the different ways the configuration environment can be set.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for checking

@jdx jdx merged commit b282bb6 into jdx:main May 5, 2025
20 checks passed
jdx pushed a commit that referenced this pull request May 5, 2025
### 🚀 Features

- **(registry)** use aqua for taplo by
[@risu729](https://github.com/risu729) in
[#4991](#4991)
- add mise_env tera variable for templates by
[@auxesis](https://github.com/auxesis) in
[#5002](#5002)

### 🐛 Bug Fixes

- **(env)** fix dotenv files cascading (fix #4688) by
[@noirbizarre](https://github.com/noirbizarre) in
[#4996](#4996)

### Ci

- **(registry)** increaset timeout to 30 mins by
[@risu729](https://github.com/risu729) in
[#5006](#5006)
@auxesis auxesis deleted the feat/add-mise_env-tera-variable branch May 6, 2025 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants