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

How to set environment variables conditionally depending on command line arguments. #876

Open
hydra opened this issue Jun 22, 2023 · 5 comments

Comments

@hydra
Copy link

hydra commented Jun 22, 2023

Feature Description

After skimming and searching the docs I cannot find out how to conditionally set or modify environment variables based on command line arguments.

  1. Example command, without arguments:
cargo make examples
  1. Example command, with argument:
cargo make examples verbose

When the 'verbose' argument is present on the command line, as above, the following environment variable needs to be set:

env = { "RUSTC_LOG" = "rustc_codegen_ssa::back::link=info" }

My use-case is that I need to see output from the linker sometimes (e.g. on CI server logs) and locally when builds fail, and also want a less-verbose build before pushing via git.
My tasks require specific 'target' setting, features, cargo args and cwd, and I use a cargo workspace.

Here's an excerpt from the Makefile.toml

[tasks.examples]
workspace = false
dependencies = [
    "examples-nucleoh563zi",
]

[tasks.examples-nucleoh563zi]
workspace = false
dependencies = [
    "examples-nucleoh563zi-hello-world",
]

[tasks.examples-nucleoh563zi-hello-world]
workspace = false
cwd = "crates/examples/nucleoh563zi"
command = "cargo"
env = { "RUSTC_LOG" = "rustc_codegen_ssa::back::link=info" }
args = ["build", "--bin=hello_world", "--target=thumbv7em-none-eabihf", "--release", "--features=mcu-stm32h563", "-vv"]

Did I miss something in the documentation? I read up on: tasks, conditions (they're for tasks), environment variables (creating ones, globals, etc), and arguments.

@sagiegurari
Copy link
Owner

sagiegurari commented Jun 22, 2023

there is no condition on cli arg, but you can look at env setup scripts:
https://github.com/sagiegurari/cargo-make#env-setup-scripts
and write a small duckscript to check args and setup env var

this for example, will set the env var only if you provided at least 1 arg on the cargo-make command and will run at the start of every cargo-make invocation.

env_scripts = [
'''
#!@duckscript

arg_defined = is_defined 1
if ${arg_defined}
  set_env MY_ENV_VAR "arg is provided"
end
'''
]

@sagiegurari
Copy link
Owner

sagiegurari commented Jun 22, 2023

@hydra and here is the exact script you need

env_scripts = [
'''
#!@duckscript

for arg in ${@}
  verbose_found = eq ${arg} verbose
  if ${verbose_found}
    set_env RUSTC_LOG rustc_codegen_ssa::back::link=info
    goto :postloop
  end
end
:postloop
'''
]

@hydra
Copy link
Author

hydra commented Jun 24, 2023

Firstly, thanks for the reply and scripts, much appreciated.

But hmm, I didn't really want to resort to have to writing scripts in yet another programming language that I don't know (duckscript) or have no real desire to learn - and neither do I want to force the rest of the devs working on the project to have to learn duckscript. I kinda just wanted some built-in syntax to achieve it, with examples in the main documentation, something like

[tasks.mytask]
env = {
  {  "RUSTC_LOG" = "rustc_codegen_ssa::back::link=info", conditions = { arg_set = "verbose" } }
}
...

Conditions option when setting an environment variable seems like a reasonable thing to want to do, we already have conditions for tasks, so all the condition logic is probably implemented, except for the 'arg_set' condition, and the application of conditions to environment variables. Perhaps 'arg_not_set', 'args', 'args_contains', etc, in the same manner as 'env*' conditions for tasks, would also be very useful.

Thoughts?

@sagiegurari
Copy link
Owner

another programming language

duckscript is similar to simple shell but much more limited and simple. not much to learn.
read it for 5 mins at https://github.com/sagiegurari/duckscript and tell me if its a bump or not.
It is meant as simple workaround for you so you won't be blocked.

I kinda just wanted some built-in syntax to achieve it

Conditions are supported for env vars, but that type of condition does not exist.
Do you want to PR that?

@hydra
Copy link
Author

hydra commented Jul 19, 2023

duckscript is similar to simple shell but much more limited and simple. not much to learn. read it for 5 mins at https://github.com/sagiegurari/duckscript and tell me if its a bump or not. It is meant as simple workaround for you so you won't be blocked.

Yup, and that was appreciated! I will look at duckscript next time I work on the cargo make files for the project, in doesn't fill me with joy though.

I kinda just wanted some built-in syntax to achieve it

Conditions are supported for env vars, but that type of condition does not exist. Do you want to PR that?

I would love for someone else to, and I'm happy to try any PRs and give feedback, but alas my time is split too many ways right now for me to delve into the cargo make codebase and make a PR myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants