1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

feat(prompts): add ability for tasks to prompt user pre execution (#1163)

This commit is contained in:
Max Cheetham
2023-06-04 02:33:00 +01:00
committed by GitHub
parent 105756eb27
commit f815ce2901
11 changed files with 244 additions and 17 deletions

25
task.go
View File

@@ -1,11 +1,13 @@
package task
import (
"bufio"
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
@@ -34,6 +36,11 @@ const (
MaximumTaskCall = 100
)
func shouldPromptContinue(input string) bool {
input = strings.ToLower(strings.TrimSpace(input))
return slices.Contains([]string{"y", "yes"}, input)
}
// Executor executes a Taskfile
type Executor struct {
Taskfile *taskfile.Taskfile
@@ -45,6 +52,7 @@ type Executor struct {
Watch bool
Verbose bool
Silent bool
AssumeYes bool
Dry bool
Summary bool
Parallel bool
@@ -94,6 +102,7 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
}
return &errors.TaskInternalError{TaskName: call.Task}
}
}
if e.Summary {
@@ -139,6 +148,22 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
release := e.acquireConcurrencyLimit()
defer release()
// check if the given task has a warning prompt
if t.Prompt != "" && !e.AssumeYes {
e.Logger.Outf(logger.Yellow, "task: %q [y/N]\n", t.Prompt)
reader := bufio.NewReader(e.Stdin)
userInput, err := reader.ReadString('\n')
if err != nil {
return err
}
userInput = strings.ToLower(strings.TrimSpace(userInput))
if !shouldPromptContinue(userInput) {
return &errors.TaskCancelledError{TaskName: call.Task}
}
}
return e.startExecution(ctx, t, func(ctx context.Context) error {
if !shouldRunOnCurrentPlatform(t.Platforms) {
e.Logger.VerboseOutf(logger.Yellow, `task: %q not for current platform - ignored\n`, call.Task)