mirror of
https://github.com/go-task/task.git
synced 2026-06-24 05:05:49 +00:00
feat(cmd): add ask option to commands
Add an `ask` attribute to commands that shows a y/n confirmation
before executing. If the user declines, the command is skipped but
the task continues with the next command.
Example:
```yaml
cmds:
- cmd: echo "Deploying..."
ask: "Deploy to production?"
- task: run-migrations
ask: "Run database migrations?"
```
Behavior:
- Default: asks for y/n confirmation
- --yes: auto-confirms all asks
- --dry: shows commands without asking
This differs from task-level `prompt:` which cancels the entire task
if declined. Command-level `ask:` only skips the individual command.
This commit is contained in:
16
task.go
16
task.go
@@ -365,6 +365,22 @@ func (e *Executor) runCommand(ctx context.Context, t *ast.Task, call *Call, i in
|
||||
}
|
||||
}
|
||||
|
||||
// Handle ask attached to command (y/n confirmation)
|
||||
if cmd.Ask != "" && !e.Dry {
|
||||
if e.AssumeYes {
|
||||
e.Logger.VerboseOutf(logger.Yellow, "task: [%s] %s [assuming yes]\n", t.Name(), cmd.Ask)
|
||||
} else {
|
||||
if err := e.Logger.Prompt(logger.Yellow, cmd.Ask, "n", "y", "yes"); errors.Is(err, logger.ErrNoTerminal) {
|
||||
return &errors.TaskCancelledNoTerminalError{TaskName: call.Task}
|
||||
} else if errors.Is(err, logger.ErrPromptCancelled) {
|
||||
e.Logger.VerboseOutf(logger.Yellow, "task: [%s] ask declined - skipped\n", t.Name())
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case cmd.Task != "":
|
||||
reacquire := e.releaseConcurrencyLimit()
|
||||
|
||||
Reference in New Issue
Block a user