grim/convey

convey/import is new in convey 0.15.0 and not on our current build agents
package environment
import (
"os"
log "github.com/sirupsen/logrus"
)
func (e Environment) expandMapper(name string) string {
// We might have something like $(pwd), in which case we don't want
// to wrap the name in `{}`, which would cause it to be ${}(pwd) and
// cannot be evaluated by a run task.
if name == "" {
return "$"
}
if val, found := e[name]; found {
if val != "" {
return val
}
return os.Getenv(name)
}
return ""
}
// Expand looks for the given key and returns the value if one is found
// recursing if the value contains another variable.
func (e Environment) Expand(key string) string {
last := os.Expand(key, e.expandMapper)
n_tries := 10
for i := 0; i < n_tries; i++ {
next := os.Expand(last, e.expandMapper)
if next == last {
return next
}
last = next
}
log.Warnf("failed to fully expand %q after %d recursions", key, n_tries)
return ""
}
func (e Environment) Expandv(items []string) []string {
expanded := make([]string, len(items))
for idx, item := range items {
expanded[idx] = e.Expand(item)
}
return expanded
}