mirror of
https://github.com/go-task/task.git
synced 2026-02-25 04:33:43 +01:00
docs(requires): document enum variable references
This commit is contained in:
@@ -1233,6 +1233,51 @@ This is supported only for string variables.
|
||||
|
||||
:::
|
||||
|
||||
### Using variable references for enum values
|
||||
|
||||
Instead of hardcoding enum values, you can reference a variable containing the
|
||||
allowed values. This is useful when you want to define allowed values once and
|
||||
reuse them, or when the values are computed dynamically.
|
||||
|
||||
Use the `ref` key to reference a variable:
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
|
||||
vars:
|
||||
ALLOWED_ENVS: [dev, staging, prod]
|
||||
|
||||
tasks:
|
||||
deploy:
|
||||
requires:
|
||||
vars:
|
||||
- name: ENV
|
||||
enum:
|
||||
ref: .ALLOWED_ENVS
|
||||
cmds:
|
||||
- echo "Deploying to {{.ENV}}"
|
||||
```
|
||||
|
||||
You can also use template expressions to transform the value:
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
|
||||
vars:
|
||||
CONFIG:
|
||||
sh: cat config.json
|
||||
|
||||
tasks:
|
||||
deploy:
|
||||
requires:
|
||||
vars:
|
||||
- name: ENV
|
||||
enum:
|
||||
ref: .CONFIG | fromJson | .allowed_environments
|
||||
cmds:
|
||||
- echo "Deploying to {{.ENV}}"
|
||||
```
|
||||
|
||||
### Prompting for missing variables interactively
|
||||
|
||||
If you want Task to prompt users for missing required variables instead of
|
||||
|
||||
@@ -678,6 +678,16 @@ tasks:
|
||||
cmds:
|
||||
- echo "Deploying to {{.ENVIRONMENT}} with log level {{.LOG_LEVEL}}"
|
||||
- ./deploy.sh
|
||||
|
||||
# Requirements with enum from variable reference
|
||||
reusable-deploy:
|
||||
requires:
|
||||
vars:
|
||||
- name: ENVIRONMENT
|
||||
enum:
|
||||
ref: .ALLOWED_ENVS
|
||||
cmds:
|
||||
- ./deploy.sh
|
||||
```
|
||||
|
||||
See [Prompting for missing variables interactively](/docs/guide#prompting-for-missing-variables-interactively)
|
||||
|
||||
Reference in New Issue
Block a user