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

feat: allow providing single or multi prompts #1866

Open
wants to merge 3 commits 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
16 changes: 9 additions & 7 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,15 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
}
}

if t.Prompt != "" && !e.Dry {
if err := e.Logger.Prompt(logger.Yellow, t.Prompt, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
} else if errors.Is(err, logger.ErrPromptCancelled) {
return &errors.TaskCancelledByUserError{TaskName: call.Task}
} else if err != nil {
return err
for _, p := range t.Prompt {
if p != "" && !e.Dry {
if err := e.Logger.Prompt(logger.Yellow, p, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
} else if errors.Is(err, logger.ErrPromptCancelled) {
return &errors.TaskCancelledByUserError{TaskName: call.Task}
} else if err != nil {
return err
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions taskfile/ast/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ast

import (
"gopkg.in/yaml.v3"

"github.com/go-task/task/v3/errors"
)

type Prompt []string

func (p *Prompt) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case yaml.ScalarNode:
var str string
if err := node.Decode(&str); err != nil {
return errors.NewTaskfileDecodeError(err, node)
}
*p = []string{str}
return nil
case yaml.SequenceNode:
var list []string
if err := node.Decode(&list); err != nil {
return errors.NewTaskfileDecodeError(err, node)
}
*p = list
return nil
}
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("prompt")
}
4 changes: 2 additions & 2 deletions taskfile/ast/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Task struct {
Deps []*Dep
Label string
Desc string
Prompt string
Prompt Prompt
Summary string
Requires *Requires
Aliases []string
Expand Down Expand Up @@ -115,7 +115,7 @@ func (t *Task) UnmarshalYAML(node *yaml.Node) error {
Deps []*Dep
Label string
Desc string
Prompt string
Prompt Prompt
Summary string
Aliases []string
Sources []*Glob
Expand Down
7 changes: 7 additions & 0 deletions testdata/prompt/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ tasks:
prompt: Do you want to continue?
cmds:
- echo 'show-prompt'

multi-prompt:
prompt:
- Do you want to continue?
- Are you sure?
cmds:
- echo 'multi-prompt'
2 changes: 1 addition & 1 deletion website/docs/reference/schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ vars:
| `deps` | [`[]Dependency`](#dependency) | | A list of dependencies of this task. Tasks defined here will run in parallel before this task. |
| `label` | `string` | | Overrides the name of the task in the output when a task is run. Supports variables. |
| `desc` | `string` | | A short description of the task. This is displayed when calling `task --list`. |
| `prompt` | `string` | | A prompt that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks. |
| `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. |
| `summary` | `string` | | A longer description of the task. This is displayed when calling `task --summary [task]`. |
| `aliases` | `[]string` | | A list of alternative names by which the task can be called. |
| `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. |
Expand Down
18 changes: 18 additions & 0 deletions website/docs/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,24 @@ tasks:
task: "This is a dangerous command... Do you want to continue?" [y/N]
```

Prompts can be a single value or a list of prompts, like below:

```yaml
version: '3'

tasks:
example:
cmds:
- task: dangerous

dangerous:
prompt:
- This is a dangerous command... Do you want to continue?
- Are you sure?
cmds:
- echo 'dangerous command'
```

Warning prompts are called before executing a task. If a prompt is denied Task
will exit with [exit code](/api#exit-codes) 205. If approved, Task will continue
as normal.
Expand Down
14 changes: 12 additions & 2 deletions website/static/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@
"type": "string"
},
"prompt": {
"description": "A prompt that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks.",
"type": "string"
"description": "One or more prompts that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks.",
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"summary": {
"description": "A longer description of the task. This is displayed when calling `task --summary [task]`.",
Expand Down
Loading