2014-05-24 06:58:06 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-05-24 07:07:33 +02:00
|
|
|
"fmt"
|
2014-05-24 06:58:06 +02:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-05-24 08:15:00 +02:00
|
|
|
|
|
|
|
"github.com/mitchellh/reflectwalk"
|
2014-05-24 06:58:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// varRegexp is a regexp that matches variables such as ${foo.bar}
|
|
|
|
var varRegexp *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
2014-07-04 05:41:26 +02:00
|
|
|
varRegexp = regexp.MustCompile(`(?i)(\$+)\{([*-.a-z0-9_]+)\}`)
|
2014-05-24 06:58:06 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:41:12 +02:00
|
|
|
// ReplaceVariables takes a configuration and a mapping of variables
|
2014-06-05 21:52:07 +02:00
|
|
|
// and performs the structure walking necessary to properly replace
|
|
|
|
// all the variables.
|
2014-06-20 20:41:12 +02:00
|
|
|
func ReplaceVariables(
|
|
|
|
c interface{},
|
|
|
|
vs map[string]string) ([]string, error) {
|
2014-06-05 21:52:07 +02:00
|
|
|
w := &variableReplaceWalker{Values: vs}
|
2014-06-20 20:41:12 +02:00
|
|
|
if err := reflectwalk.Walk(c, w); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.UnknownKeys, nil
|
2014-06-05 21:52:07 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 06:58:06 +02:00
|
|
|
// variableDetectWalker implements interfaces for the reflectwalk package
|
|
|
|
// (github.com/mitchellh/reflectwalk) that can be used to automatically
|
|
|
|
// pull out the variables that need replacing.
|
|
|
|
type variableDetectWalker struct {
|
|
|
|
Variables map[string]InterpolatedVariable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *variableDetectWalker) Primitive(v reflect.Value) error {
|
|
|
|
// We only care about strings
|
2014-05-24 08:15:00 +02:00
|
|
|
if v.Kind() == reflect.Interface {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
2014-05-24 06:58:06 +02:00
|
|
|
if v.Kind() != reflect.String {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: This can be a lot more efficient if we used a real
|
|
|
|
// parser. A regexp is a hammer though that will get this working.
|
|
|
|
|
|
|
|
matches := varRegexp.FindAllStringSubmatch(v.String(), -1)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
dollars := len(match[1])
|
|
|
|
|
|
|
|
// If there are even amounts of dollar signs, then it is escaped
|
|
|
|
if dollars%2 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, record it
|
|
|
|
key := match[2]
|
|
|
|
if w.Variables == nil {
|
|
|
|
w.Variables = make(map[string]InterpolatedVariable)
|
|
|
|
}
|
|
|
|
if _, ok := w.Variables[key]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var iv InterpolatedVariable
|
2014-05-24 07:07:33 +02:00
|
|
|
if strings.Index(key, ".") == -1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Interpolated variable '%s' has bad format. "+
|
|
|
|
"Did you mean 'var.%s'?",
|
|
|
|
key, key)
|
|
|
|
}
|
|
|
|
|
2014-05-24 06:58:06 +02:00
|
|
|
if strings.HasPrefix(key, "var.") {
|
|
|
|
iv, err = NewUserVariable(key)
|
|
|
|
} else {
|
|
|
|
iv, err = NewResourceVariable(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Variables[key] = iv
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-24 08:15:00 +02:00
|
|
|
|
|
|
|
// variableReplaceWalker implements interfaces for reflectwalk that
|
|
|
|
// is used to replace variables with their values.
|
|
|
|
//
|
|
|
|
// If Values does not have every available value, then the program
|
|
|
|
// will _panic_. The variableDetectWalker will tell you all variables
|
|
|
|
// you need.
|
|
|
|
type variableReplaceWalker struct {
|
2014-06-13 02:00:50 +02:00
|
|
|
Values map[string]string
|
|
|
|
UnknownKeys []string
|
2014-05-24 08:15:00 +02:00
|
|
|
|
2014-06-13 02:00:50 +02:00
|
|
|
key []string
|
2014-06-13 01:40:53 +02:00
|
|
|
loc reflectwalk.Location
|
|
|
|
cs []reflect.Value
|
|
|
|
csData interface{}
|
2014-05-24 08:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *variableReplaceWalker) Enter(loc reflectwalk.Location) error {
|
|
|
|
w.loc = loc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *variableReplaceWalker) Exit(loc reflectwalk.Location) error {
|
|
|
|
w.loc = reflectwalk.None
|
2014-06-13 01:40:53 +02:00
|
|
|
|
|
|
|
switch loc {
|
|
|
|
case reflectwalk.Map:
|
|
|
|
w.cs = w.cs[:len(w.cs)-1]
|
2014-06-13 02:00:50 +02:00
|
|
|
case reflectwalk.MapValue:
|
|
|
|
w.key = w.key[:len(w.key)-1]
|
2014-06-13 01:40:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 08:15:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-13 01:40:53 +02:00
|
|
|
func (w *variableReplaceWalker) Map(m reflect.Value) error {
|
|
|
|
w.cs = append(w.cs, m)
|
2014-06-13 01:24:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-24 08:15:00 +02:00
|
|
|
func (w *variableReplaceWalker) MapElem(m, k, v reflect.Value) error {
|
2014-06-13 01:40:53 +02:00
|
|
|
w.csData = k
|
2014-06-13 02:00:50 +02:00
|
|
|
w.key = append(w.key, k.String())
|
2014-05-24 08:15:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *variableReplaceWalker) Primitive(v reflect.Value) error {
|
|
|
|
// We only care about strings
|
|
|
|
setV := v
|
|
|
|
if v.Kind() == reflect.Interface {
|
|
|
|
setV = v
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
if v.Kind() != reflect.String {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := varRegexp.FindAllStringSubmatch(v.String(), -1)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := v.String()
|
|
|
|
for _, match := range matches {
|
|
|
|
dollars := len(match[1])
|
|
|
|
|
|
|
|
// If there are even amounts of dollar signs, then it is escaped
|
|
|
|
if dollars%2 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the key
|
|
|
|
key := match[2]
|
|
|
|
value, ok := w.Values[key]
|
|
|
|
if !ok {
|
|
|
|
panic("no value for variable key: " + key)
|
|
|
|
}
|
|
|
|
|
2014-06-13 01:40:53 +02:00
|
|
|
// If this is an unknown variable, then we remove it from
|
|
|
|
// the configuration.
|
|
|
|
if value == UnknownVariableValue {
|
|
|
|
w.removeCurrent()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-24 08:15:00 +02:00
|
|
|
// Replace
|
|
|
|
result = strings.Replace(result, match[0], value, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultVal := reflect.ValueOf(result)
|
|
|
|
if w.loc == reflectwalk.MapValue {
|
|
|
|
// If we're in a map, then the only way to set a map value is
|
|
|
|
// to set it directly.
|
2014-06-13 02:03:15 +02:00
|
|
|
m := w.cs[len(w.cs)-1]
|
|
|
|
mk := w.csData.(reflect.Value)
|
|
|
|
m.SetMapIndex(mk, resultVal)
|
2014-05-24 08:15:00 +02:00
|
|
|
} else {
|
|
|
|
// Otherwise, we should be addressable
|
|
|
|
setV.Set(resultVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-13 01:40:53 +02:00
|
|
|
|
|
|
|
func (w *variableReplaceWalker) removeCurrent() {
|
|
|
|
c := w.cs[len(w.cs)-1]
|
|
|
|
switch c.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
// Zero value so that we delete the map key
|
|
|
|
var val reflect.Value
|
|
|
|
|
|
|
|
// Get the key and delete it
|
|
|
|
k := w.csData.(reflect.Value)
|
|
|
|
c.SetMapIndex(k, val)
|
|
|
|
}
|
2014-06-13 02:00:50 +02:00
|
|
|
|
|
|
|
// Append the key to the unknown keys
|
|
|
|
w.UnknownKeys = append(w.UnknownKeys, strings.Join(w.key, "."))
|
2014-06-13 01:40:53 +02:00
|
|
|
}
|