Skip to content

Commit 5581954

Browse files
authored
feat: allow providing single or multi prompts (#1866)
* Add new type to handle single or multi prompts * update docs * apply review
1 parent c4f708b commit 5581954

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
lines changed

task.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,15 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
235235
}
236236
}
237237

238-
if t.Prompt != "" && !e.Dry {
239-
if err := e.Logger.Prompt(logger.Yellow, t.Prompt, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
240-
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
241-
} else if errors.Is(err, logger.ErrPromptCancelled) {
242-
return &errors.TaskCancelledByUserError{TaskName: call.Task}
243-
} else if err != nil {
244-
return err
238+
for _, p := range t.Prompt {
239+
if p != "" && !e.Dry {
240+
if err := e.Logger.Prompt(logger.Yellow, p, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
241+
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
242+
} else if errors.Is(err, logger.ErrPromptCancelled) {
243+
return &errors.TaskCancelledByUserError{TaskName: call.Task}
244+
} else if err != nil {
245+
return err
246+
}
245247
}
246248
}
247249

taskfile/ast/prompt.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ast
2+
3+
import (
4+
"gopkg.in/yaml.v3"
5+
6+
"github.com/go-task/task/v3/errors"
7+
)
8+
9+
type Prompt []string
10+
11+
func (p *Prompt) UnmarshalYAML(node *yaml.Node) error {
12+
switch node.Kind {
13+
case yaml.ScalarNode:
14+
var str string
15+
if err := node.Decode(&str); err != nil {
16+
return errors.NewTaskfileDecodeError(err, node)
17+
}
18+
*p = []string{str}
19+
return nil
20+
case yaml.SequenceNode:
21+
var list []string
22+
if err := node.Decode(&list); err != nil {
23+
return errors.NewTaskfileDecodeError(err, node)
24+
}
25+
*p = list
26+
return nil
27+
}
28+
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("prompt")
29+
}

taskfile/ast/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Task struct {
1818
Deps []*Dep
1919
Label string
2020
Desc string
21-
Prompt string
21+
Prompt Prompt
2222
Summary string
2323
Requires *Requires
2424
Aliases []string
@@ -115,7 +115,7 @@ func (t *Task) UnmarshalYAML(node *yaml.Node) error {
115115
Deps []*Dep
116116
Label string
117117
Desc string
118-
Prompt string
118+
Prompt Prompt
119119
Summary string
120120
Aliases []string
121121
Sources []*Glob

testdata/prompt/Taskfile.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ tasks:
1414
prompt: Do you want to continue?
1515
cmds:
1616
- echo 'show-prompt'
17+
18+
multi-prompt:
19+
prompt:
20+
- Do you want to continue?
21+
- Are you sure?
22+
cmds:
23+
- echo 'multi-prompt'

website/docs/reference/schema.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ vars:
8888
| `deps` | [`[]Dependency`](#dependency) | | A list of dependencies of this task. Tasks defined here will run in parallel before this task. |
8989
| `label` | `string` | | Overrides the name of the task in the output when a task is run. Supports variables. |
9090
| `desc` | `string` | | A short description of the task. This is displayed when calling `task --list`. |
91-
| `prompt` | `string` | | A prompt that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks. |
91+
| `prompt` | `[]string` | | One or more prompts that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks. |
9292
| `summary` | `string` | | A longer description of the task. This is displayed when calling `task --summary [task]`. |
9393
| `aliases` | `[]string` | | A list of alternative names by which the task can be called. |
9494
| `sources` | `[]string` | | A list of sources to check before running this task. Relevant for `checksum` and `timestamp` methods. Can be file paths or star globs. |

website/docs/usage.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,24 @@ tasks:
18781878
task: "This is a dangerous command... Do you want to continue?" [y/N]
18791879
```
18801880

1881+
Prompts can be a single value or a list of prompts, like below:
1882+
1883+
```yaml
1884+
version: '3'
1885+
1886+
tasks:
1887+
example:
1888+
cmds:
1889+
- task: dangerous
1890+
1891+
dangerous:
1892+
prompt:
1893+
- This is a dangerous command... Do you want to continue?
1894+
- Are you sure?
1895+
cmds:
1896+
- echo 'dangerous command'
1897+
```
1898+
18811899
Warning prompts are called before executing a task. If a prompt is denied Task
18821900
will exit with [exit code](/api#exit-codes) 205. If approved, Task will continue
18831901
as normal.

website/static/schema.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@
5959
"type": "string"
6060
},
6161
"prompt": {
62-
"description": "A prompt that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks.",
63-
"type": "string"
62+
"description": "One or more prompts that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks.",
63+
"oneOf": [
64+
{
65+
"type": "string"
66+
},
67+
{
68+
"type": "array",
69+
"items": {
70+
"type": "string"
71+
}
72+
}
73+
]
6474
},
6575
"summary": {
6676
"description": "A longer description of the task. This is displayed when calling `task --summary [task]`.",

0 commit comments

Comments
 (0)