-
-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ff9ba9
commit d533aca
Showing
14 changed files
with
373 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ dist/ | |
|
||
# intellij idea/goland | ||
.idea/ | ||
|
||
# exuberant ctags | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package taskfile | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
// ErrCantUnmarshalPrecondition is returned for invalid precond YAML. | ||
ErrCantUnmarshalPrecondition = errors.New("task: can't unmarshal precondition value") | ||
) | ||
|
||
// Precondition represents a precondition necessary for a task to run | ||
type Precondition struct { | ||
Sh string | ||
Msg string | ||
IgnoreError bool | ||
} | ||
|
||
// UnmarshalYAML implements yaml.Unmarshaler interface. | ||
func (p *Precondition) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var cmd string | ||
|
||
if err := unmarshal(&cmd); err == nil { | ||
p.Sh = cmd | ||
p.Msg = fmt.Sprintf("`%s` failed", cmd) | ||
p.IgnoreError = false | ||
return nil | ||
} | ||
|
||
var sh struct { | ||
Sh string | ||
Msg string | ||
IgnoreError bool `yaml:"ignore_error"` | ||
} | ||
|
||
err := unmarshal(&sh) | ||
|
||
if err == nil { | ||
p.Sh = sh.Sh | ||
p.Msg = sh.Msg | ||
if p.Msg == "" { | ||
p.Msg = fmt.Sprintf("%s failed", sh.Sh) | ||
} | ||
|
||
p.IgnoreError = sh.IgnoreError | ||
return nil | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package taskfile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-task/task/v2/internal/taskfile" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestPreconditionParse(t *testing.T) { | ||
tests := []struct { | ||
content string | ||
v interface{} | ||
expected interface{} | ||
}{ | ||
{ | ||
"test -f foo.txt", | ||
&taskfile.Precondition{}, | ||
&taskfile.Precondition{Sh: `test -f foo.txt`, Msg: "`test -f foo.txt` failed", IgnoreError: false}, | ||
}, | ||
{ | ||
"sh: '[ 1 = 0 ]'", | ||
&taskfile.Precondition{}, | ||
&taskfile.Precondition{Sh: "[ 1 = 0 ]", Msg: "[ 1 = 0 ] failed", IgnoreError: false}, | ||
}, | ||
{` | ||
sh: "[ 1 = 2 ]" | ||
msg: "1 is not 2" | ||
`, | ||
&taskfile.Precondition{}, | ||
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2", IgnoreError: false}, | ||
}, | ||
{` | ||
sh: "[ 1 = 2 ]" | ||
msg: "1 is not 2" | ||
ignore_error: true | ||
`, | ||
&taskfile.Precondition{}, | ||
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2", IgnoreError: true}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
err := yaml.Unmarshal([]byte(test.content), test.v) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expected, test.v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Package task provides ... | ||
package task | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/go-task/task/v2/internal/execext" | ||
"github.com/go-task/task/v2/internal/taskfile" | ||
) | ||
|
||
var ( | ||
// ErrPreconditionFailed is returned when a precondition fails | ||
ErrNecessaryPreconditionFailed = errors.New("task: precondition not met") | ||
ErrOptionalPreconditionFailed = errors.New("task: optional precondition not met") | ||
) | ||
|
||
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task) (bool, error) { | ||
var optionalPreconditionFailed bool | ||
for _, p := range t.Precondition { | ||
err := execext.RunCommand(ctx, &execext.RunCommandOptions{ | ||
Command: p.Sh, | ||
Dir: t.Dir, | ||
Env: getEnviron(t), | ||
}) | ||
|
||
if err != nil { | ||
e.Logger.Outf(p.Msg) | ||
if p.IgnoreError == true { | ||
optionalPreconditionFailed = true | ||
} else { | ||
return false, ErrNecessaryPreconditionFailed | ||
} | ||
} | ||
} | ||
|
||
if optionalPreconditionFailed == true { | ||
return true, ErrOptionalPreconditionFailed | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.