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:
parent
05600601ff
commit
108cb91d95
29
README.md
29
README.md
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
6
task.go
6
task.go
@ -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
|
||||
|
28
task_test.go
28
task_test.go
@ -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"}))
|
||||
})
|
||||
}
|
||||
|
24
testdata/ignore_errors/Taskfile.yml
vendored
24
testdata/ignore_errors/Taskfile.yml
vendored
@ -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
|
Loading…
x
Reference in New Issue
Block a user