1
0
mirror of https://github.com/go-task/task.git synced 2025-08-08 22:36:57 +02:00
This commit is contained in:
Valentin Maerten
2025-01-02 22:18:25 +01:00
parent 8558e0c48a
commit 10d7123f60
2 changed files with 75 additions and 30 deletions

View File

@@ -2,6 +2,8 @@ package ast
import (
"fmt"
"github.com/go-task/task/v3/internal/deepcopy"
"sync"
"gopkg.in/yaml.v3"
@@ -9,9 +11,27 @@ import (
)
// Precondition represents a precondition necessary for a task to run
type Precondition struct {
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),
}
}
func (p *Precondition) DeepCopy() *Precondition {
@@ -24,6 +44,12 @@ func (p *Precondition) DeepCopy() *Precondition {
}
}
func NewPreconditions() *Preconditions {
return &Preconditions{
preconditions: make([]*Precondition, 0),
}
}
// UnmarshalYAML implements yaml.Unmarshaler interface.
func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
@@ -55,3 +81,16 @@ func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("precondition")
}
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
}

View File

@@ -29,6 +29,7 @@ type Taskfile struct {
Shopt []string
Vars *Vars
Env *Vars
Preconditions *Preconditions
Tasks *Tasks
Silent bool
Dotenv []string
@@ -72,6 +73,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
Output Output
Method string
Includes *Includes
Preconditions *Preconditions
Set []string
Shopt []string
Vars *Vars
@@ -110,6 +112,10 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
if tf.Tasks == nil {
tf.Tasks = NewTasks()
}
if tf.Preconditions == nil {
tf.Preconditions = NewPreconditions()
}
return nil
}