1
0
mirror of https://github.com/go-task/task.git synced 2024-12-14 10:52:43 +02:00
task/taskfile/precondition.go

46 lines
816 B
Go
Raw Normal View History

2019-05-17 22:13:47 +02:00
package taskfile
import (
"errors"
"fmt"
)
var (
// ErrCantUnmarshalPrecondition is returned for invalid precond YAML.
2019-06-11 20:46:22 +02:00
ErrCantUnmarshalPrecondition = errors.New("task: Can't unmarshal precondition value")
2019-05-17 22:13:47 +02:00
)
// Precondition represents a precondition necessary for a task to run
type Precondition struct {
2019-05-28 22:02:59 +02:00
Sh string
Msg string
2019-05-17 22:13:47 +02:00
}
// 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)
return nil
}
var sh struct {
2019-05-28 22:02:59 +02:00
Sh string
Msg string
2019-05-17 22:13:47 +02:00
}
2019-05-28 22:02:59 +02:00
if err := unmarshal(&sh); err != nil {
return err
}
2019-05-17 22:13:47 +02:00
2019-05-28 22:02:59 +02:00
p.Sh = sh.Sh
p.Msg = sh.Msg
if p.Msg == "" {
p.Msg = fmt.Sprintf("%s failed", sh.Sh)
2019-05-17 22:13:47 +02:00
}
2019-05-28 22:02:59 +02:00
return nil
2019-05-17 22:13:47 +02:00
}