Skip to content

Commit

Permalink
Add command-level warning prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerduckworth committed Jul 20, 2020
1 parent 8137517 commit a6f7984
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/taskfile/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Cmd struct {
Silent bool
Task string
Vars *Vars
Warning string
IgnoreError bool
}

Expand Down Expand Up @@ -41,11 +42,13 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
var cmdStruct struct {
Cmd string
Silent bool
Warning string
IgnoreError bool `yaml:"ignore_error"`
}
if err := unmarshal(&cmdStruct); err == nil && cmdStruct.Cmd != "" {
c.Cmd = cmdStruct.Cmd
c.Silent = cmdStruct.Silent
c.Warning = cmdStruct.Warning
c.IgnoreError = cmdStruct.IgnoreError
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/taskfile/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Task struct {
Silent bool
Method string
Prefix string
Warning string
IgnoreError bool
}

Expand Down Expand Up @@ -69,6 +70,7 @@ func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
Silent bool
Method string
Prefix string
Warning string
IgnoreError bool `yaml:"ignore_error"`
}
if err := unmarshal(&task); err == nil {
Expand All @@ -87,6 +89,7 @@ func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
t.Silent = task.Silent
t.Method = task.Method
t.Prefix = task.Prefix
t.Warning = task.Warning
t.IgnoreError = task.IgnoreError

return nil
Expand Down
3 changes: 3 additions & 0 deletions internal/taskfile/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Taskfile struct {
Env *Vars
Tasks Tasks
Silent bool
Warning string
}

// UnmarshalYAML implements yaml.Unmarshaler interface
Expand All @@ -30,6 +31,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
Env *Vars
Tasks Tasks
Silent bool
Warning string
}
if err := unmarshal(&taskfile); err != nil {
return err
Expand All @@ -43,6 +45,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
tf.Env = taskfile.Env
tf.Tasks = taskfile.Tasks
tf.Silent = taskfile.Silent
tf.Warning = taskfile.Warning
if tf.Expansions <= 0 {
tf.Expansions = 2
}
Expand Down
24 changes: 24 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -34,6 +35,7 @@ type Executor struct {

Dir string
Entrypoint string
Warning string
Force bool
Watch bool
Verbose bool
Expand Down Expand Up @@ -345,6 +347,17 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
}
return nil
case cmd.Cmd != "":
if cmd.Warning != "" {
fmt.Printf("%s (y/N): ", cmd.Warning)

var response string
fmt.Scanln(&response)

if !isConfirmed(response) {
return errors.New("cancelled at warning")
}
}

if e.Verbose || (!cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
e.Logger.Errf(logger.Green, "task: %s", cmd.Cmd)
}
Expand Down Expand Up @@ -399,3 +412,14 @@ func getEnviron(t *taskfile.Task) []string {
}
return environ
}

func isConfirmed(response string) bool {
affirmativeResponses := []string{"y", "yes"}

for _, affirm := range affirmativeResponses {
if strings.ToLower(response) == affirm {
return true
}
}
return false
}
2 changes: 2 additions & 0 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
Vars: nil,
Env: nil,
Silent: origTask.Silent,
Warning: origTask.Warning,
Method: r.Replace(origTask.Method),
Prefix: r.Replace(origTask.Prefix),
IgnoreError: origTask.IgnoreError,
Expand Down Expand Up @@ -77,6 +78,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
new.Cmds[i] = &taskfile.Cmd{
Task: r.Replace(cmd.Task),
Silent: cmd.Silent,
Warning: cmd.Warning,
Cmd: r.Replace(cmd.Cmd),
Vars: r.ReplaceVars(cmd.Vars),
IgnoreError: cmd.IgnoreError,
Expand Down

0 comments on commit a6f7984

Please sign in to comment.