44 gabs "github.com/Jeffail/gabs/v2"
55 consolelogger "github.com/semaphoreci/spc/pkg/consolelogger"
66 changein "github.com/semaphoreci/spc/pkg/when/changein"
7+ env "github.com/semaphoreci/spc/pkg/when/env"
78 whencli "github.com/semaphoreci/spc/pkg/when/whencli"
89)
910
@@ -17,7 +18,22 @@ type WhenExpression struct {
1718
1819func (w * WhenExpression ) Eval () error {
1920 for _ , requirment := range w .ListChangeInFunctions (w .Requirments ) {
20- result , err := w .EvalFunction (requirment )
21+ result , err := w .EvaluateChangeInFunction (requirment )
22+ if err != nil {
23+ return err
24+ }
25+
26+ input := map [string ]interface {}{}
27+ input ["name" ] = w .functionName (requirment )
28+ input ["params" ] = w .functionParams (requirment )
29+ input ["result" ] = result
30+
31+ w .ReduceInputs .Keywords = map [string ]interface {}{}
32+ w .ReduceInputs .Functions = append (w .ReduceInputs .Functions , input )
33+ }
34+
35+ for _ , requirment := range w .ListEnvFunctions (w .Requirments ) {
36+ result , err := w .EvaluateEnvFunction (requirment )
2137 if err != nil {
2238 return err
2339 }
@@ -46,6 +62,18 @@ func (w *WhenExpression) ListChangeInFunctions(requirments *gabs.Container) []*g
4662 return result
4763}
4864
65+ func (w * WhenExpression ) ListEnvFunctions (requirments * gabs.Container ) []* gabs.Container {
66+ result := []* gabs.Container {}
67+
68+ for _ , input := range requirments .Children () {
69+ if w .IsEnvFunction (input ) {
70+ result = append (result , input )
71+ }
72+ }
73+
74+ return result
75+ }
76+
4977func (w * WhenExpression ) IsChangeInFunction (input * gabs.Container ) bool {
5078 elType := input .Search ("type" ).Data ().(string )
5179 if elType != "fun" {
@@ -59,7 +87,20 @@ func (w *WhenExpression) IsChangeInFunction(input *gabs.Container) bool {
5987 return true
6088}
6189
62- func (w * WhenExpression ) EvalFunction (input * gabs.Container ) (bool , error ) {
90+ func (w * WhenExpression ) IsEnvFunction (input * gabs.Container ) bool {
91+ elType := input .Search ("type" ).Data ().(string )
92+ if elType != "fun" {
93+ return false
94+ }
95+
96+ if w .functionName (input ) != "env" {
97+ return false
98+ }
99+
100+ return true
101+ }
102+
103+ func (w * WhenExpression ) EvaluateChangeInFunction (input * gabs.Container ) (bool , error ) {
63104 consolelogger .EmptyLine ()
64105
65106 consolelogger .Infof ("%s(%+v)\n " , w .functionName (input ), w .functionParams (input ))
@@ -72,6 +113,19 @@ func (w *WhenExpression) EvalFunction(input *gabs.Container) (bool, error) {
72113 return changein .Eval (fun )
73114}
74115
116+ func (w * WhenExpression ) EvaluateEnvFunction (input * gabs.Container ) (string , error ) {
117+ consolelogger .EmptyLine ()
118+
119+ consolelogger .Infof ("%s(%+v)\n " , w .functionName (input ), w .functionParams (input ))
120+
121+ fun , err := env .Parse (input )
122+ if err != nil {
123+ return "" , err
124+ }
125+
126+ return env .Eval (fun )
127+ }
128+
75129func (w * WhenExpression ) functionName (input * gabs.Container ) string {
76130 return input .Search ("name" ).Data ().(string )
77131}
0 commit comments