1
0
mirror of https://github.com/go-task/task.git synced 2024-12-12 10:45:49 +02:00

feat: allow providing single or multi prompts (#1866)

* Add new type to handle single or multi prompts

* update docs

* apply review
This commit is contained in:
Matheus Mina 2024-10-29 10:37:03 -03:00 committed by GitHub
parent c4f708b222
commit 5581954fb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 78 additions and 12 deletions

16
task.go
View File

@ -235,13 +235,15 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
} }
} }
if t.Prompt != "" && !e.Dry { for _, p := range t.Prompt {
if err := e.Logger.Prompt(logger.Yellow, t.Prompt, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) { if p != "" && !e.Dry {
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task} if err := e.Logger.Prompt(logger.Yellow, p, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
} else if errors.Is(err, logger.ErrPromptCancelled) { return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
return &errors.TaskCancelledByUserError{TaskName: call.Task} } else if errors.Is(err, logger.ErrPromptCancelled) {
} else if err != nil { return &errors.TaskCancelledByUserError{TaskName: call.Task}
return err } else if err != nil {
return err
}
} }
} }

29
taskfile/ast/prompt.go Normal file
View File

@ -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")
}

View File

@ -18,7 +18,7 @@ type Task struct {
Deps []*Dep Deps []*Dep
Label string Label string
Desc string Desc string
Prompt string Prompt Prompt
Summary string Summary string
Requires *Requires Requires *Requires
Aliases []string Aliases []string
@ -115,7 +115,7 @@ func (t *Task) UnmarshalYAML(node *yaml.Node) error {
Deps []*Dep Deps []*Dep
Label string Label string
Desc string Desc string
Prompt string Prompt Prompt
Summary string Summary string
Aliases []string Aliases []string
Sources []*Glob Sources []*Glob

View File

@ -14,3 +14,10 @@ tasks:
prompt: Do you want to continue? prompt: Do you want to continue?
cmds: cmds:
- echo 'show-prompt' - echo 'show-prompt'
multi-prompt:
prompt:
- Do you want to continue?
- Are you sure?
cmds:
- echo 'multi-prompt'

View File

@ -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. | | `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. | | `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`. | | `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]`. | | `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. | | `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. | | `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. |

View File

@ -1878,6 +1878,24 @@ tasks:
task: "This is a dangerous command... Do you want to continue?" [y/N] 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 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 will exit with [exit code](/api#exit-codes) 205. If approved, Task will continue
as normal. as normal.

View File

@ -59,8 +59,18 @@
"type": "string" "type": "string"
}, },
"prompt": { "prompt": {
"description": "A prompt that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks.", "description": "One or more prompts that will be presented before a task is run. Declining will cancel running the current and any subsequent tasks.",
"type": "string" "oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
}, },
"summary": { "summary": {
"description": "A longer description of the task. This is displayed when calling `task --summary [task]`.", "description": "A longer description of the task. This is displayed when calling `task --summary [task]`.",