2016-12-20 16:26:49 -05:00
|
|
|
package interpolation
|
|
|
|
|
|
|
|
import (
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/template"
|
2017-03-23 11:05:24 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-12-20 16:26:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Interpolate replaces variables in a string with the values from a mapping
|
2017-03-22 10:42:03 -04:00
|
|
|
func Interpolate(config map[string]interface{}, section string, mapping template.Mapping) (map[string]interface{}, error) {
|
|
|
|
out := map[string]interface{}{}
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
for name, item := range config {
|
|
|
|
if item == nil {
|
|
|
|
out[name] = nil
|
|
|
|
continue
|
|
|
|
}
|
2017-03-23 11:05:24 -04:00
|
|
|
mapItem, ok := item.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("Invalid type for %s : %T instead of %T", name, item, out)
|
|
|
|
}
|
|
|
|
interpolatedItem, err := interpolateSectionItem(name, mapItem, section, mapping)
|
2016-12-20 16:26:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out[name] = interpolatedItem
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func interpolateSectionItem(
|
|
|
|
name string,
|
2017-03-22 10:42:03 -04:00
|
|
|
item map[string]interface{},
|
2016-12-20 16:26:49 -05:00
|
|
|
section string,
|
|
|
|
mapping template.Mapping,
|
2017-03-22 10:42:03 -04:00
|
|
|
) (map[string]interface{}, error) {
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2017-03-22 10:42:03 -04:00
|
|
|
out := map[string]interface{}{}
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
for key, value := range item {
|
|
|
|
interpolatedValue, err := recursiveInterpolate(value, mapping)
|
2017-05-11 06:17:16 -04:00
|
|
|
switch err := err.(type) {
|
|
|
|
case nil:
|
|
|
|
case *template.InvalidTemplateError:
|
2017-03-23 11:05:24 -04:00
|
|
|
return nil, errors.Errorf(
|
2017-02-18 00:29:51 -05:00
|
|
|
"Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.",
|
2016-12-20 16:26:49 -05:00
|
|
|
key, section, name, err.Template,
|
|
|
|
)
|
2017-05-11 06:17:16 -04:00
|
|
|
default:
|
|
|
|
return nil, errors.Wrapf(err, "error while interpolating %s in %s %s", key, section, name)
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
out[key] = interpolatedValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func recursiveInterpolate(
|
|
|
|
value interface{},
|
|
|
|
mapping template.Mapping,
|
2017-05-11 06:17:16 -04:00
|
|
|
) (interface{}, error) {
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
switch value := value.(type) {
|
|
|
|
|
|
|
|
case string:
|
|
|
|
return template.Substitute(value, mapping)
|
|
|
|
|
2017-03-22 10:42:03 -04:00
|
|
|
case map[string]interface{}:
|
|
|
|
out := map[string]interface{}{}
|
2016-12-20 16:26:49 -05:00
|
|
|
for key, elem := range value {
|
|
|
|
interpolatedElem, err := recursiveInterpolate(elem, mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out[key] = interpolatedElem
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
out := make([]interface{}, len(value))
|
|
|
|
for i, elem := range value {
|
|
|
|
interpolatedElem, err := recursiveInterpolate(elem, mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out[i] = interpolatedElem
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return value, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|