Skip to content

Commit

Permalink
Add new type to handle single or multi prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
mfbmina committed Oct 16, 2024
1 parent 9ecc8fc commit e870378
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
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
21 changes: 21 additions & 0 deletions taskfile/ast/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ast

type Prompt []string

func (p *Prompt) UnmarshalYAML(unmarshal func(any) error) error {
var m []string
err := unmarshal(&m)
if err == nil {
*p = m
return nil
}

var s string
err = unmarshal(&s)
if err != nil {
return err
}

*p = []string{s}
return nil
}
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'

0 comments on commit e870378

Please sign in to comment.