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

110 lines
2.1 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
"sync"
2025-01-05 10:42:34 +01:00
"github.com/go-task/task/v3/errors"
2025-01-03 14:03:16 +01:00
"github.com/go-task/task/v3/internal/deepcopy"
"gopkg.in/yaml.v3"
)
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 {
2025-01-03 14:03:16 +01:00
Preconditions []*Precondition
2025-01-02 22:18:25 +01:00
mutex sync.RWMutex
}
Precondition struct {
Sh string
Msg string
}
)
2025-01-05 10:42:34 +01:00
func NewPreconditions() *Preconditions {
return &Preconditions{
Preconditions: make([]*Precondition, 0),
}
}
2025-01-02 22:18:25 +01:00
func (p *Preconditions) DeepCopy() *Preconditions {
if p == nil {
return nil
}
defer p.mutex.RUnlock()
p.mutex.RLock()
return &Preconditions{
2025-01-03 14:03:16 +01:00
Preconditions: deepcopy.Slice(p.Preconditions),
2025-01-02 22:18:25 +01:00
}
2019-05-17 13:13:47 -07:00
}
2025-01-07 21:05:43 +01:00
func (p *Preconditions) Merge(other *Preconditions) {
if p == nil || p.Preconditions == nil || other == nil {
return
}
p.mutex.Lock()
defer p.mutex.Unlock()
other.mutex.RLock()
defer other.mutex.RUnlock()
p.Preconditions = append(p.Preconditions, deepcopy.Slice(other.Preconditions)...)
}
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.
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 {
2025-01-03 14:03:16 +01:00
if p == nil || p.Preconditions == nil {
2025-01-02 22:18:25 +01:00
*p = *NewPreconditions()
}
2025-01-03 14:03:16 +01:00
if err := node.Decode(&p.Preconditions); err != nil {
return errors.NewTaskfileDecodeError(err, node).WithTypeMessage("preconditions")
2025-01-02 22:18:25 +01:00
}
return nil
}