1
0
mirror of https://github.com/go-task/task.git synced 2025-11-29 22:48:03 +02:00
Files
task/taskfile/ast/precondition.go

97 lines
1.8 KiB
Go
Raw Normal View History

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"
"gopkg.in/yaml.v3"
2019-05-17 13:13:47 -07: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
}
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.
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 errors.NewTaskfileDecodeError(err, node)
}
2019-05-17 13:13:47 -07:00
p.Sh = cmd
p.Msg = fmt.Sprintf("`%s` failed", cmd)
return nil
case yaml.MappingNode:
var sh struct {
Sh string
Msg string
}
if err := node.Decode(&sh); err != nil {
return errors.NewTaskfileDecodeError(err, node)
}
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
}
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
}