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

IgnoreError

* Document ignore_error
* ignore_error only for commands
This commit is contained in:
Tobias Salzmann 2018-08-01 10:44:53 +02:00
parent 05600601ff
commit 108cb91d95
6 changed files with 55 additions and 56 deletions

View File

@ -632,6 +632,35 @@ tasks:
- echo "This will print nothing" > /dev/null
```
## Ignore errors
You have the option to ignore errors during command execution.
Given the following Taskfile:
```yml
version: '2'
tasks:
echo:
cmds:
- exit 1
- echo "Hello World"
```
Task will abort the execution after running `exit 1` because the status code `1` stands for `EXIT_FAILURE`.
However it is possible to continue with execution using `ignore_errors`:
```yml
version: '2'
tasks:
echo:
cmds:
- cmd: exit 1
ignore_errors: true
- echo "Hello World"
```
## Output syntax
By default, Task just redirect the STDOUT and STDERR of the running commands

View File

@ -2,7 +2,6 @@ package taskfile
// Call is the parameters to a task call
type Call struct {
Task string
Vars Vars
IgnoreError bool
Task string
Vars Vars
}

View File

@ -16,9 +16,8 @@ type Cmd struct {
// Dep is a task dependency
type Dep struct {
Task string
Vars Vars
IgnoreError bool
Task string
Vars Vars
}
var (
@ -42,7 +41,7 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
var cmdStruct struct {
Cmd string
Silent bool
IgnoreError bool `yaml:"ignoreError"`
IgnoreError bool `yaml:"ignore_error"`
}
if err := unmarshal(&cmdStruct); err == nil && cmdStruct.Cmd != "" {
c.Cmd = cmdStruct.Cmd
@ -51,14 +50,12 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}
var taskCall struct {
Task string
Vars Vars
IgnoreError bool `yaml:"ignoreError"`
Task string
Vars Vars
}
if err := unmarshal(&taskCall); err == nil {
c.Task = taskCall.Task
c.Vars = taskCall.Vars
c.IgnoreError = taskCall.IgnoreError
return nil
}
return ErrCantUnmarshalCmd
@ -72,14 +69,12 @@ func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}
var taskCall struct {
Task string
Vars Vars
IgnoreError bool `yaml:"ignoreError"`
Task string
Vars Vars
}
if err := unmarshal(&taskCall); err == nil {
d.Task = taskCall.Task
d.Vars = taskCall.Vars
d.IgnoreError = taskCall.IgnoreError
return nil
}
return ErrCantUnmarshalDep

View File

@ -188,7 +188,7 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
d := d
g.Go(func() error {
return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars, IgnoreError: d.IgnoreError})
return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
})
}
@ -200,7 +200,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
switch {
case cmd.Task != "":
return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars, IgnoreError: cmd.IgnoreError || call.IgnoreError})
return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
case cmd.Cmd != "":
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
e.Logger.Errf(cmd.Cmd)
@ -219,7 +219,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
Stdin: e.Stdin,
Stdout: stdOut,
Stderr: stdErr,
IgnoreErrorCode: cmd.IgnoreError || call.IgnoreError,
IgnoreErrorCode: cmd.IgnoreError,
})
default:
return nil

View File

@ -415,43 +415,23 @@ func TestTaskVersion(t *testing.T) {
func TestTaskIgnoreErrors(t *testing.T) {
const dir = "testdata/ignore_errors"
t.Run("CmdShouldPass", func(t *testing.T) {
t.Run("cmd-should-pass", func(t *testing.T) {
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(taskfile.Call{Task: "CmdShouldPass"}))
assert.NoError(t, e.Run(taskfile.Call{Task: "cmd-should-pass"}))
})
t.Run("CmdShouldFail", func(t *testing.T) {
t.Run("cmd-should-fail", func(t *testing.T) {
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.Setup())
assert.Error(t, e.Run(taskfile.Call{Task: "CmdShouldFail"}))
})
t.Run("TaskShouldPass", func(t *testing.T) {
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(taskfile.Call{Task: "TaskShouldPass"}))
})
t.Run("TaskShouldFail", func(t *testing.T) {
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.Setup())
assert.Error(t, e.Run(taskfile.Call{Task: "TaskShouldFail"}))
assert.Error(t, e.Run(taskfile.Call{Task: "cmd-should-fail"}))
})
}

View File

@ -1,15 +1,11 @@
CmdShouldPass:
cmds:
- cmd: UnknownCommandThatNeverWillExist
ignoreError: true
CmdShouldFail:
cmds:
- cmd: UnknownCommandThatNeverWillExist
version: 2
TaskShouldPass:
cmds:
- task: CmdShouldFail
ignoreError: true
TaskShouldFail:
cmds:
- task: CmdShouldFail
tasks:
cmd-should-pass:
cmds:
- cmd: exit 1
ignore_error: true
cmd-should-fail:
cmds:
- cmd: exit 1