-
Notifications
You must be signed in to change notification settings - Fork 55
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
Possibility to set global variables #80
Comments
Strongly for that. That would follow DRY principle and simplify a lot of automations! |
I agree! (As noted below, optional variables are also something that we could use well)
As a workaround, I tried to do this and a few variants of it: (imagine an
The issue is, of course, that each (Note that I assume here that I want more capabilities than adding environment variables. This alone could be done with |
I've got around this by creating a helper task: ## var (v)
> Some global vars that can be used by other tasks
You can override these by setting the respective env var before running `mask`
~~~sh
THING=${THING:-"world!"}
echo "${!v}"
~~~
## hello-shell
**OPTIONS**
* thing
* flags: -t --thing
* type: string
* desc: Thing
~~~sh
T=${thing:-"$($MASK var THING)"}
echo "Hello, $T"
~~~
## hello-py
**OPTIONS**
* thing
* flags: -t --thing
* type: string
* desc: Thing
~~~python
import os
mask = os.getenv("MASK")
thing = os.getenv("thing", os.popen('{} var THING'.format(mask)).read())
print("Hello, {}".format(thing))
~~~ The resultsUsing defaults: $ mask hello-shell
Hello, world! $ mask hello-py
Hello, world!
Override default using an option: $ mask hello-shell -t "You ;)"
Hello, You ;) $ mask hello-py -t "You ;)"
Hello, You ;) Override using an environment variable: THING="Planet Earth" mask hello-shell
Hello, Planet Earth THING="Planet Earth" mask hello-py
Hello, Planet Earth I am not overly keen on this approach, but it solves my problem. I would love to see native support for global vars/envs. |
A new workaround which works better for me (note that I specifically don't need to override from an external environment variable)
I then run |
So I currently work around this by sourcing an env file whenever I need it: ## build
~~~bash
source "$MASKFILE_DIR/.env"
echo $WHATEVER_VAR
~~~ But I do see a need to have this happen automatically, especially for other runtimes where sourcing an env file is not as easy. I see three ways this could be implemented.
I think # 3 is the most simple solution and probably results in the best experience... no need to specify mask args meaning that you can call |
I just thought of a 4th option which I believe is better than options 2 & 3 and is more flexible. Mask could look for a top-level code block that uses an # Maskfile Title
~~~env
# You could either source a file that includes env vars...
source "$MASKFILE_DIR/.env"
# And/or write out your vars here...
MY_VAR=value
ANOTHER_VAR=value
~~~
## some_command
~~~bash
# No need to source anything
echo $MY_VAR
~~~ This is much more flexible and explicit than the other options and allows you to both specify env vars and source any file if necessary. |
It can simplify a lot if we can setup variables that are global per whole script.
The text was updated successfully, but these errors were encountered: