-
-
Notifications
You must be signed in to change notification settings - Fork 634
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
Add if:
keyword to control when a task or command is ran?
#608
Comments
if:
keyword to control when a task or command is ran?
Something I had in mind for a while is that we could potentially have an version: '3'
tasks:
foo:
cmds:
- echo 'bar'
if: '[[ "$ENV" == "bar" ]]' or version: '3'
tasks:
foo:
cmds:
- cmd: echo 'bar'
if: '[[ "$ENV" == "bar" ]]' NOTE: We already have |
isn't that what
|
You have a good point, we'd have some intersection between these two features.
Also, So, I don't have a strong opinion, but maybe it would still make sense to have both |
Thanks @andreynering Another thing we might want to keep in mind is handling the I would like to see an
Both the Currently, with any complex flows that rely on status, things can easily get messy when using |
Probably the best use-case for if would be to add steps that depend only on specific platforms, like Windows, Linux, MacOS. Adding extra code for platform on each called shells script is a PITA, one that could be easily avoided with an "if". |
For those interested, there's an ongoing discussion about a possible key specificly to select given OS/Arch on a task or command: |
A +1 for I really over-use Though I do empathize that it still has lots of overlap with |
@andreynering i have a couple of env vars where |
@titpetric Contributions are welcome! I would advise you to wait until #1220 is merged to avoid any conflicts. Also, that would allow you to test both features together. |
FYI #1220 has been merged |
Though IIUC this is a bit different — |
Right; I'm just saying this is ready for development per this comment |
Sorry, I didn't read up far enough. Thanks @JonZeolla |
|
A |
After reading though the code, and considering the problem a bit more, there seem to be 3 possible paths:
To my mind, adding a "skip" attribute to preconditions, and adjusting the behavior of precondition evaluation, is the better option. |
you can use go template in some case. for example
|
Yes, but very urgly, and hard to maintain if the statement grows bigger. |
use case for if else. I have no idea how to do anything that reaches the same needs with task yet. this checks if a binary is installed, and if not installs it into a special folder that is pathed in, to ensure we do not cross pollute projects. The ".dep" folder holds any binary dependencies I need , so that I can build a project. I also has a OS and ARCH built in, called BASE_GOOS_NAME GH_BIN_NAME=gh
ifeq ($(BASE_GOOS_NAME),windows)
GH_BIN_NAME=gh.exe
endif
GH_BIN_VERSION=v2.59.0
GH_BIN_WHICH=$(shell $(WHICH_BIN_NAME) $(GH_BIN_NAME))
this-release-dep-del:
rm -rf $(GH_BIN_WHICH)
this-release-dep: this-dep
ifeq ($(GH_BIN_WHICH), )
@echo ""
@echo "$(GH_BIN_NAME) dep check: failed"
rm -rf $(BASE_DEP_DOWNLOAD_ROOT)
mkdir -p $(BASE_DEP_DOWNLOAD_ROOT)
@echo $(BASE_DEP_DOWNLOAD_ROOT_NAME) >> .gitignore
# download the go, build it using a temp folder and copy the binary into the .dep folder, and delete the src. This Only happens ONCE so its fine.
cd $(BASE_DEP_DOWNLOAD_ROOT) && git clone https://github.com/cli/cli -b v2.59.0
cd $(BASE_DEP_DOWNLOAD_ROOT) && touch go.work
cd $(BASE_DEP_DOWNLOAD_ROOT) && go work use cli
mkdir -p $(BASE_DEP_ROOT)
cd $(BASE_DEP_DOWNLOAD_ROOT)/cli && go build -o $(BASE_DEP_ROOT)/$(GH_BIN_NAME) ./cmd/gh
rm -rf $(BASE_DEP_DOWNLOAD_ROOT)
else
@echo ""
@echo "$(GH_BIN_NAME) dep check: passed"
@echo ""
endif in case your wondering, "this-dep" installs the "which" that is cross platform:https://github.com/hairyhenderson/go-which. This is the type of Built in, that should be in task IMHO. "this-def" also employs the same "which" based file checking to see if it is needed to be installed or not. So the best solution I know is to writer a golang based thing that does this which based checking , and installs the dep. then dep it at the start is a task run. If anyone has any clues about how else I can do this would be awesome... |
I would not go down the path of IF and ELSE. If you really need such a complexity then just write a bash script and be done with it. Better is to use STATUS to evaluate the condition for which a TASK should run. That effectively gives you the IF/ELSE logic you need, and you will get a better outcome. So, for you that might mean a TASK where the status checks for the absence of an installed dependency, and when missing, the commands install that dependency. This is I think, not very complicated to try. |
I agree that if / else is a bad pattern . did not know about the STATUS keyword, but can I do conditionals of sorts with it ? I really need to solve this . |
Like this ? on mobile - not idea where those single quotes are on iOS mobile, so just raw formatting: https://stackoverflow.com/questions/74863706/taskfile-how-to-run-only-if-binary-not-exists tasks: For every cmd that requires a binary , I just have the “install-binary-X”, as a dep . |
I don't think If I take the following:
That Currently, the task is constructed like so
The tag command is a lot less readable, causing more chance of confusion and errors |
There are some cases where
task:
is called undercmds:
and the task needs to be conditionally run. Take the following as an example:I'm currently getting around the issue by having a task called
donothing
that gets called when the task should not run. It would be nice if we could conditionally run tasks somehow. Maybe something like this:The text was updated successfully, but these errors were encountered: