Skip to content

Commit

Permalink
fix default variables
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-touron committed Nov 17, 2021
1 parent 6c42298 commit a8a664b
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions rocktest/rocktest/scenario/contextGetter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package scenario

import "os"
import (
"os"
"regexp"

log "github.com/sirupsen/logrus"
)

type ContextGetter struct {
scenario *Scenario
Expand All @@ -14,18 +19,58 @@ func NewContextGetter(s *Scenario) *ContextGetter {

func (x ContextGetter) Lookup(s string) (string, bool, error) {

ret, found := x.scenario.GetContext(s)
// Do we have expression like
// ${variable?value if set::value if not set} or
// ${variable::value if not set}

re, err := regexp.Compile(`([^?]+)(?:\?(.*))?::(.*)`)

if err != nil {
log.Errorf(err.Error())
return s, false, nil
}

if re.Match([]byte(s)) {
res := re.FindAllStringSubmatch(s, -1)

name := res[0][1]
valset := res[0][2]
valunset := res[0][3]

log.Tracef("%v", res)
log.Tracef("Name: \"%s\", Val is set: \"%s\", val if not set: \"%s\"", name, valset, valunset)

if !found {
env := os.Getenv(s)
if env == "" {
return s, false, nil
val, found := x.scenario.GetContext(name)

if found {
if valset != "" {
return valset, true, nil
} else {
return val, true, nil
}
} else {
return env, true, nil
if valunset != "" {
return valunset, true, nil
} else {
return s, false, nil
}
}

} else {
return ret, found, nil

ret, found := x.scenario.GetContext(s)

if !found {
env := os.Getenv(s)
if env == "" {
return s, false, nil
} else {
return env, true, nil
}

} else {
return ret, found, nil
}
}

}

0 comments on commit a8a664b

Please sign in to comment.