2023-12-29 20:32:03 +00:00
|
|
|
package ast
|
2019-05-17 13:13:47 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-01-02 22:18:25 +01:00
|
|
|
"github.com/go-task/task/v3/internal/deepcopy"
|
|
|
|
|
"sync"
|
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
|
2025-01-02 22:18:25 +01:00
|
|
|
type (
|
|
|
|
|
Preconditions struct {
|
|
|
|
|
preconditions []*Precondition
|
|
|
|
|
mutex sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Precondition struct {
|
|
|
|
|
Sh string
|
|
|
|
|
Msg string
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (p *Preconditions) DeepCopy() *Preconditions {
|
|
|
|
|
if p == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
defer p.mutex.RUnlock()
|
|
|
|
|
p.mutex.RLock()
|
|
|
|
|
return &Preconditions{
|
|
|
|
|
preconditions: deepcopy.Slice(p.preconditions),
|
|
|
|
|
}
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-02 22:18:25 +01:00
|
|
|
func NewPreconditions() *Preconditions {
|
|
|
|
|
return &Preconditions{
|
|
|
|
|
preconditions: make([]*Precondition, 0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2025-01-02 22:18:25 +01:00
|
|
|
|
|
|
|
|
func (p *Preconditions) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
|
|
|
|
|
|
if p == nil || p.preconditions == nil {
|
|
|
|
|
*p = *NewPreconditions()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := node.Decode(&p.preconditions); err != nil {
|
|
|
|
|
return errors.NewTaskfileDecodeError(err, node).WithTypeMessage("precondition")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|