@@ -13,11 +13,31 @@ import (
1313)
1414
1515type ParsedTask struct {
16- Shell []string `json:"shell"`
17- WorkingDir string `json:"workingDir"`
18- Env map [string ]string `json:"environ"`
19- Interactive bool `json:"interactive,omitempty"`
20- Commands []CommandJson `json:"commands"`
16+ Shell []string `json:"shell"`
17+ WorkingDir string `json:"workingDir"`
18+ Env map [string ]string `json:"environ"`
19+ Interactive bool `json:"interactive,omitempty"`
20+ Commands []ParsedCommandJson `json:"commands"`
21+ }
22+
23+ func evalGoTemplateCondition (tpl string ) (bool , * Error ) {
24+ t := template .New ("requirement" )
25+ t = t .Funcs (sprig .FuncMap ())
26+ templateExpr := fmt .Sprintf (`{{ %s }}` , tpl )
27+ t , err := t .Parse (templateExpr )
28+ if err != nil {
29+ return false , TaskRequirementIncorrect .WithErr (err ).WithMetadata ("requirement" , tpl )
30+ }
31+ b := new (bytes.Buffer )
32+ if err := t .ExecuteTemplate (b , "requirement" , map [string ]string {}); err != nil {
33+ return false , TaskRequirementIncorrect .WithErr (err ).WithMetadata ("requirement" , tpl )
34+ }
35+
36+ if b .String () != "true" {
37+ return false , TaskRequirementFailed .WithErr (fmt .Errorf ("template must have evaluated to true" )).WithMetadata ("requirement" , tpl )
38+ }
39+
40+ return true , nil
2141}
2242
2343func ParseTask (ctx Context , rf * Runfile , task Task ) (* ParsedTask , * Error ) {
@@ -71,22 +91,9 @@ func ParseTask(ctx Context, rf *Runfile, task Task) (*ParsedTask, *Error) {
7191 }
7292
7393 if requirement .GoTmpl != nil {
74- t := template .New ("requirement" )
75- t = t .Funcs (sprig .FuncMap ())
76- templateExpr := fmt .Sprintf (`{{ %s }}` , * requirement .GoTmpl )
77- t , err := t .Parse (templateExpr )
78- if err != nil {
79- return nil , TaskRequirementIncorrect .WithErr (err ).WithMetadata ("requirement" , * requirement .GoTmpl )
80- }
81- b := new (bytes.Buffer )
82- if err := t .ExecuteTemplate (b , "requirement" , map [string ]string {}); err != nil {
83- return nil , TaskRequirementIncorrect .WithErr (err ).WithMetadata ("requirement" , * requirement .GoTmpl )
84- }
85-
86- if b .String () != "true" {
87- return nil , TaskRequirementFailed .WithErr (fmt .Errorf ("template must have evaluated to true" )).WithMetadata ("requirement" , * requirement .GoTmpl )
94+ if _ , err := evalGoTemplateCondition (* requirement .GoTmpl ); err != nil {
95+ return nil , err
8896 }
89-
9097 continue
9198 }
9299 }
@@ -127,7 +134,7 @@ func ParseTask(ctx Context, rf *Runfile, task Task) (*ParsedTask, *Error) {
127134 return nil , err
128135 }
129136
130- commands := make ([]CommandJson , 0 , len (task .Commands ))
137+ commands := make ([]ParsedCommandJson , 0 , len (task .Commands ))
131138 for i := range task .Commands {
132139 c2 , err := parseCommand (rf , task .Commands [i ])
133140 if err != nil {
@@ -169,11 +176,11 @@ func resolveDotEnvFiles(pwd string, dotEnvFiles ...string) ([]string, *Error) {
169176 return paths , nil
170177}
171178
172- func parseCommand (rf * Runfile , command any ) (* CommandJson , * Error ) {
179+ func parseCommand (rf * Runfile , command any ) (* ParsedCommandJson , * Error ) {
173180 switch c := command .(type ) {
174181 case string :
175182 {
176- return & CommandJson {Command : c }, nil
183+ return & ParsedCommandJson {Command : c }, nil
177184 }
178185 case map [string ]any :
179186 {
@@ -187,15 +194,27 @@ func parseCommand(rf *Runfile, command any) (*CommandJson, *Error) {
187194 return nil , CommandInvalid .WithErr (err ).WithMetadata ("command" , command )
188195 }
189196
190- if cj .Run == "" {
191- return nil , CommandInvalid .WithErr (fmt .Errorf ("key: 'run', must be specified when setting command in json format" )).WithMetadata ("command" , command )
197+ if cj .Run == "" && cj . Command == "" {
198+ return nil , CommandInvalid .WithErr (fmt .Errorf ("key: 'run'/'cmd' , must be specified when setting command in json format" )).WithMetadata ("command" , cj )
192199 }
193200
194- if _ , ok := rf .Tasks [cj .Run ]; ! ok {
195- return nil , CommandInvalid .WithErr (fmt .Errorf ("run target, not found" )).WithMetadata ("command" , command , "run-target" , cj .Run )
201+ var pcj ParsedCommandJson
202+ pcj .Run = cj .Run
203+ pcj .Command = cj .Command
204+
205+ if cj .If != nil {
206+ ok , _ := evalGoTemplateCondition (* cj .If )
207+ // if err != nil {
208+ // return nil, err
209+ // }
210+ pcj .If = & ok
196211 }
197212
198- return & cj , nil
213+ // if _, ok := rf.Tasks[cj.Run]; !ok {
214+ // return nil, CommandInvalid.WithErr(fmt.Errorf("run target, not found")).WithMetadata("command", command, "run-target", cj.Run)
215+ // }
216+
217+ return & pcj , nil
199218 }
200219 default :
201220 {
0 commit comments