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

Fix and modify requires behavior. #1677

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions requires.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ func (e *Executor) areTaskRequiredVarsSet(ctx context.Context, t *ast.Task, call

var missingVars []string
for _, requiredVar := range t.Requires.Vars {
if !vars.Exists(requiredVar) {
missingVars = append(missingVars, requiredVar)
v := vars.Get(requiredVar)

// Check and continue on positive conditions.
if v.Value != nil {
switch val := v.Value.(type) {
case string:
if len(val) > 0 {
continue
}
default:
continue
}
}

// The required variable is not available.
missingVars = append(missingVars, requiredVar)
}

if len(missingVars) > 0 {
Expand Down
8 changes: 5 additions & 3 deletions website/docs/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -911,13 +911,14 @@ tasks:
### Ensuring required variables are set

If you want to check that certain variables are set before running a task then
you can use `requires`. This is useful when might not be clear to users which
you can use `requires`. This is useful when it might not be clear to users which
variables are needed, or if you want clear message about what is required. Also
some tasks could have dangerous side effects if run with un-set variables.

Using `requires` you specify an array of strings in the `vars` sub-section under
`requires`, these strings are variable names which are checked prior to running
the task. If any variables are un-set the the task will error and not run.
the task. If any of the listed variables are either un-set or zero length strings
then the task will error and not run.

Environmental variables are also checked.

Expand All @@ -930,7 +931,8 @@ requires:

:::note

Variables set to empty zero length strings, will pass the `requires` check.
Variables set to zero length (empty) strings will not pass the `requires` check.
Circumvent this by setting the variable to a string with a single space character (i.e. `' '`).

:::

Expand Down