2016-12-20 16:26:49 -05:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-09-29 11:21:51 -04:00
|
|
|
var (
|
|
|
|
delimiter = "\\$"
|
|
|
|
substitution = "[_a-z][_a-z0-9]*(?::?[-?][^}]*)?"
|
|
|
|
)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
|
|
|
var patternString = fmt.Sprintf(
|
|
|
|
"%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?P<braced>%s)}|(?P<invalid>))",
|
|
|
|
delimiter, delimiter, substitution, substitution,
|
|
|
|
)
|
|
|
|
|
2018-08-02 10:50:40 -04:00
|
|
|
var defaultPattern = regexp.MustCompile(patternString)
|
2016-12-20 16:26:49 -05:00
|
|
|
|
2018-12-31 11:05:24 -05:00
|
|
|
// DefaultSubstituteFuncs contains the default SubstituteFunc used by the docker cli
|
2018-06-25 11:15:26 -04:00
|
|
|
var DefaultSubstituteFuncs = []SubstituteFunc{
|
|
|
|
softDefault,
|
|
|
|
hardDefault,
|
|
|
|
requiredNonEmpty,
|
|
|
|
required,
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
// InvalidTemplateError is returned when a variable template is not in a valid
|
|
|
|
// format
|
|
|
|
type InvalidTemplateError struct {
|
|
|
|
Template string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e InvalidTemplateError) Error() string {
|
|
|
|
return fmt.Sprintf("Invalid template: %#v", e.Template)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mapping is a user-supplied function which maps from variable names to values.
|
|
|
|
// Returns the value as a string and a bool indicating whether
|
|
|
|
// the value is present, to distinguish between an empty string
|
|
|
|
// and the absence of a value.
|
|
|
|
type Mapping func(string) (string, bool)
|
|
|
|
|
2018-06-25 11:15:26 -04:00
|
|
|
// SubstituteFunc is a user-supplied function that apply substitution.
|
|
|
|
// Returns the value as a string, a bool indicating if the function could apply
|
|
|
|
// the substitution and an error.
|
|
|
|
type SubstituteFunc func(string, Mapping) (string, bool, error)
|
|
|
|
|
|
|
|
// SubstituteWith subsitute variables in the string with their values.
|
|
|
|
// It accepts additional substitute function.
|
|
|
|
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
|
2017-05-11 06:17:16 -04:00
|
|
|
var err error
|
|
|
|
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
|
2016-12-20 16:26:49 -05:00
|
|
|
matches := pattern.FindStringSubmatch(substring)
|
2018-08-02 10:50:40 -04:00
|
|
|
groups := matchGroups(matches, pattern)
|
2018-02-21 15:16:12 -05:00
|
|
|
if escaped := groups["escaped"]; escaped != "" {
|
|
|
|
return escaped
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
substitution := groups["named"]
|
|
|
|
if substitution == "" {
|
|
|
|
substitution = groups["braced"]
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:15:26 -04:00
|
|
|
if substitution == "" {
|
2018-02-21 15:16:12 -05:00
|
|
|
err = &InvalidTemplateError{Template: template}
|
|
|
|
return ""
|
2018-06-25 11:15:26 -04:00
|
|
|
}
|
2018-02-21 15:16:12 -05:00
|
|
|
|
2018-06-25 11:15:26 -04:00
|
|
|
for _, f := range subsFuncs {
|
|
|
|
var (
|
|
|
|
value string
|
|
|
|
applied bool
|
|
|
|
)
|
|
|
|
value, applied, err = f(substitution, mapping)
|
|
|
|
if err != nil {
|
2018-02-21 15:16:12 -05:00
|
|
|
return ""
|
2018-02-21 06:29:08 -05:00
|
|
|
}
|
2018-06-25 11:15:26 -04:00
|
|
|
if !applied {
|
|
|
|
continue
|
2016-12-20 16:26:49 -05:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2018-02-21 15:16:12 -05:00
|
|
|
value, _ := mapping(substitution)
|
|
|
|
return value
|
2016-12-20 16:26:49 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:15:26 -04:00
|
|
|
// Substitute variables in the string with their values
|
|
|
|
func Substitute(template string, mapping Mapping) (string, error) {
|
2018-08-02 10:50:40 -04:00
|
|
|
return SubstituteWith(template, mapping, defaultPattern, DefaultSubstituteFuncs...)
|
2018-06-25 11:15:26 -04:00
|
|
|
}
|
|
|
|
|
2018-08-01 10:12:49 -04:00
|
|
|
// ExtractVariables returns a map of all the variables defined in the specified
|
|
|
|
// composefile (dict representation) and their default value if any.
|
2018-08-02 10:50:40 -04:00
|
|
|
func ExtractVariables(configDict map[string]interface{}, pattern *regexp.Regexp) map[string]string {
|
|
|
|
if pattern == nil {
|
|
|
|
pattern = defaultPattern
|
|
|
|
}
|
|
|
|
return recurseExtract(configDict, pattern)
|
2018-08-01 10:12:49 -04:00
|
|
|
}
|
|
|
|
|
2018-08-02 10:50:40 -04:00
|
|
|
func recurseExtract(value interface{}, pattern *regexp.Regexp) map[string]string {
|
2018-08-01 10:12:49 -04:00
|
|
|
m := map[string]string{}
|
|
|
|
|
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
2018-08-02 10:50:40 -04:00
|
|
|
if values, is := extractVariable(value, pattern); is {
|
|
|
|
for _, v := range values {
|
|
|
|
m[v.name] = v.value
|
|
|
|
}
|
2018-08-01 10:12:49 -04:00
|
|
|
}
|
|
|
|
case map[string]interface{}:
|
|
|
|
for _, elem := range value {
|
2018-08-02 10:50:40 -04:00
|
|
|
submap := recurseExtract(elem, pattern)
|
2018-08-01 10:12:49 -04:00
|
|
|
for key, value := range submap {
|
|
|
|
m[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
for _, elem := range value {
|
2018-08-02 10:50:40 -04:00
|
|
|
if values, is := extractVariable(elem, pattern); is {
|
|
|
|
for _, v := range values {
|
|
|
|
m[v.name] = v.value
|
|
|
|
}
|
2018-08-01 10:12:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-06-25 11:15:26 -04:00
|
|
|
}
|
2018-08-01 10:12:49 -04:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type extractedValue struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:50:40 -04:00
|
|
|
func extractVariable(value interface{}, pattern *regexp.Regexp) ([]extractedValue, bool) {
|
2018-08-01 10:12:49 -04:00
|
|
|
sValue, ok := value.(string)
|
|
|
|
if !ok {
|
2018-08-02 10:50:40 -04:00
|
|
|
return []extractedValue{}, false
|
2018-06-25 11:15:26 -04:00
|
|
|
}
|
2018-08-02 10:50:40 -04:00
|
|
|
matches := pattern.FindAllStringSubmatch(sValue, -1)
|
2018-08-01 10:12:49 -04:00
|
|
|
if len(matches) == 0 {
|
2018-08-02 10:50:40 -04:00
|
|
|
return []extractedValue{}, false
|
2018-08-01 10:12:49 -04:00
|
|
|
}
|
2018-08-02 10:50:40 -04:00
|
|
|
values := []extractedValue{}
|
|
|
|
for _, match := range matches {
|
|
|
|
groups := matchGroups(match, pattern)
|
|
|
|
if escaped := groups["escaped"]; escaped != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val := groups["named"]
|
|
|
|
if val == "" {
|
|
|
|
val = groups["braced"]
|
|
|
|
}
|
|
|
|
name := val
|
|
|
|
var defaultValue string
|
|
|
|
switch {
|
|
|
|
case strings.Contains(val, ":?"):
|
|
|
|
name, _ = partition(val, ":?")
|
|
|
|
case strings.Contains(val, "?"):
|
|
|
|
name, _ = partition(val, "?")
|
|
|
|
case strings.Contains(val, ":-"):
|
|
|
|
name, defaultValue = partition(val, ":-")
|
|
|
|
case strings.Contains(val, "-"):
|
|
|
|
name, defaultValue = partition(val, "-")
|
|
|
|
}
|
|
|
|
values = append(values, extractedValue{name: name, value: defaultValue})
|
2018-08-01 10:12:49 -04:00
|
|
|
}
|
2018-08-02 10:50:40 -04:00
|
|
|
return values, len(values) > 0
|
2018-08-01 10:12:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Soft default (fall back if unset or empty)
|
|
|
|
func softDefault(substitution string, mapping Mapping) (string, bool, error) {
|
2018-09-25 12:35:39 -04:00
|
|
|
sep := ":-"
|
|
|
|
if !strings.Contains(substitution, sep) {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
name, defaultValue := partition(substitution, sep)
|
|
|
|
value, ok := mapping(name)
|
|
|
|
if !ok || value == "" {
|
|
|
|
return defaultValue, true, nil
|
|
|
|
}
|
|
|
|
return value, true, nil
|
2018-06-25 11:15:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hard default (fall back if-and-only-if empty)
|
|
|
|
func hardDefault(substitution string, mapping Mapping) (string, bool, error) {
|
2018-09-25 12:35:39 -04:00
|
|
|
sep := "-"
|
2018-08-01 10:12:49 -04:00
|
|
|
if !strings.Contains(substitution, sep) {
|
2018-06-25 11:15:26 -04:00
|
|
|
return "", false, nil
|
|
|
|
}
|
2018-08-01 10:12:49 -04:00
|
|
|
name, defaultValue := partition(substitution, sep)
|
2018-06-25 11:15:26 -04:00
|
|
|
value, ok := mapping(name)
|
|
|
|
if !ok {
|
|
|
|
return defaultValue, true, nil
|
|
|
|
}
|
|
|
|
return value, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func requiredNonEmpty(substitution string, mapping Mapping) (string, bool, error) {
|
2018-08-01 10:12:49 -04:00
|
|
|
return withRequired(substitution, mapping, ":?", func(v string) bool { return v != "" })
|
2018-06-25 11:15:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func required(substitution string, mapping Mapping) (string, bool, error) {
|
2018-08-01 10:12:49 -04:00
|
|
|
return withRequired(substitution, mapping, "?", func(_ string) bool { return true })
|
|
|
|
}
|
|
|
|
|
|
|
|
func withRequired(substitution string, mapping Mapping, sep string, valid func(string) bool) (string, bool, error) {
|
|
|
|
if !strings.Contains(substitution, sep) {
|
2018-06-25 11:15:26 -04:00
|
|
|
return "", false, nil
|
|
|
|
}
|
2018-08-01 10:12:49 -04:00
|
|
|
name, errorMessage := partition(substitution, sep)
|
2018-06-25 11:15:26 -04:00
|
|
|
value, ok := mapping(name)
|
2018-08-01 10:12:49 -04:00
|
|
|
if !ok || !valid(value) {
|
2018-06-25 11:15:26 -04:00
|
|
|
return "", true, &InvalidTemplateError{
|
|
|
|
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, true, nil
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:50:40 -04:00
|
|
|
func matchGroups(matches []string, pattern *regexp.Regexp) map[string]string {
|
2018-02-21 15:16:12 -05:00
|
|
|
groups := make(map[string]string)
|
|
|
|
for i, name := range pattern.SubexpNames()[1:] {
|
|
|
|
groups[name] = matches[i+1]
|
|
|
|
}
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:26:49 -05:00
|
|
|
// Split the string at the first occurrence of sep, and return the part before the separator,
|
|
|
|
// and the part after the separator.
|
|
|
|
//
|
|
|
|
// If the separator is not found, return the string itself, followed by an empty string.
|
|
|
|
func partition(s, sep string) (string, string) {
|
|
|
|
if strings.Contains(s, sep) {
|
|
|
|
parts := strings.SplitN(s, sep, 2)
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
return s, ""
|
|
|
|
}
|