2023-12-29 20:32:03 +00:00
|
|
|
package ast
|
2019-05-17 13:13:47 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-12-19 01:11:31 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2019-05-17 13:13:47 -07:00
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
"github.com/go-task/task/v3/errors"
|
|
|
|
)
|
2019-05-17 13:13:47 -07:00
|
|
|
|
|
|
|
// Precondition represents a precondition necessary for a task to run
|
|
|
|
type Precondition struct {
|
2019-05-28 13:02:59 -07:00
|
|
|
Sh string
|
|
|
|
Msg string
|
2019-05-17 13:13:47 -07:00
|
|
|
}
|
|
|
|
|
2023-03-25 19:13:06 +00:00
|
|
|
func (p *Precondition) DeepCopy() *Precondition {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Precondition{
|
|
|
|
Sh: p.Sh,
|
|
|
|
Msg: p.Msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 13:13:47 -07:00
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
2022-12-19 01:11:31 +00: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 {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
2019-05-17 13:13:47 -07:00
|
|
|
p.Sh = cmd
|
|
|
|
p.Msg = fmt.Sprintf("`%s` failed", cmd)
|
|
|
|
return nil
|
|
|
|
|
2022-12-19 01:11:31 +00:00
|
|
|
case yaml.MappingNode:
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
if err := node.Decode(&sh); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
|
|
|
p.Sh = sh.Sh
|
|
|
|
p.Msg = sh.Msg
|
|
|
|
if p.Msg == "" {
|
|
|
|
p.Msg = fmt.Sprintf("%s failed", sh.Sh)
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-17 13:13:47 -07:00
|
|
|
}
|
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("precondition")
|
2019-05-17 13:13:47 -07:00
|
|
|
}
|