2019-05-17 22:13:47 +02:00
|
|
|
package taskfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-12-19 03:11:31 +02:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2019-05-17 22:13:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2022-12-19 03:11:31 +02:00
|
|
|
func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
switch node.Kind {
|
|
|
|
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
var cmd string
|
|
|
|
if err := node.Decode(&cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-17 22:13:47 +02:00
|
|
|
p.Sh = cmd
|
|
|
|
p.Msg = fmt.Sprintf("`%s` failed", cmd)
|
|
|
|
return nil
|
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
case yaml.MappingNode:
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
if err := node.Decode(&sh); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Sh = sh.Sh
|
|
|
|
p.Msg = sh.Msg
|
|
|
|
if p.Msg == "" {
|
|
|
|
p.Msg = fmt.Sprintf("%s failed", sh.Sh)
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-17 22:13:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into precondition", node.Line, node.ShortTag())
|
2019-05-17 22:13:47 +02:00
|
|
|
}
|