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

A way to enforce & check required variables #1203

Closed
benc-uk opened this issue Jun 4, 2023 · 0 comments · Fixed by #1204
Closed

A way to enforce & check required variables #1203

benc-uk opened this issue Jun 4, 2023 · 0 comments · Fixed by #1204
Labels
area: variables Changes related to variables.

Comments

@benc-uk
Copy link
Contributor

benc-uk commented Jun 4, 2023

There have been several requests (#889, #714, #495) for a way to enforce or check that variables are set before running a task. There have been several solutions proposed, but most of them require some fairly obscure use of template functions, bash tricks, or are a little verbose/clumsy.

Being able to define what vars are required before running a task is pretty important IMO, esp for developer experience. It prevents that "oh shoot I forgot that one var in my .env file" problem, guides people towards the answer, rather than letting some command or script depending on that variable fail in cryptic & possibly dangerous ways. Sometimes when running a deployment or release a missing variable might not be obvious until a significant time has elapsed.

Proposal

A new field called requires is added to the task struct, this field is an array of strings. These strings should be variable names, and these variables are checked for existence before the task is run. If one or more is found to be missing, the task will not run and an error describing the missing variable(s) is returned.

Simple example

version: '3'

tasks:
  docker-build:
    cmds:
      - 'docker build . -t {{.IMAGE_NAME}}:{{.IMAGE_TAG}}'

    # Make sure these variables are set before running
    requires: [IMAGE_NAME, IMAGE_TAG]

For discussion:

  • Is requires a good name? It's short and elegant, but maybe implies some other dependency? I considered requiredVars but the camel case made me cringe a bit.
  • I considered the idea of allowing users to specify the message in a failed check, but this significantly distracts from the elegance of a simple array of strings

I believe this important enough to be a "1st class" feature of task, so I shall be submitting a PR shortly to implement this feature

@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Jun 4, 2023
@andreynering andreynering added type: feature A new feature or functionality. area: variables Changes related to variables. and removed state: needs triage Waiting to be triaged by a maintainer. labels Jun 17, 2023
@andreynering andreynering linked a pull request Jun 18, 2023 that will close this issue
@pd93 pd93 removed the type: feature A new feature or functionality. label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: variables Changes related to variables.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants