2016-12-20 16:26:49 -05:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var delimiter = "\\$"
|
2018-02-26 15:23:52 -05:00
|
|
|
var 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,
|
|
|
|
)
|
|
|
|
|
|
|
|
var pattern = regexp.MustCompile(patternString)
|
|
|
|
|
2018-06-25 11:15:26 -04:00
|
|
|
// DefaultSubstituteFuncs contains the default SubstitueFunc used by the docker cli
|
|
|
|
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-02-21 15:16:12 -05:00
|
|
|
groups := matchGroups(matches)
|
|
|
|
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) {
|
|
|
|
return SubstituteWith(template, mapping, pattern, DefaultSubstituteFuncs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Soft default (fall back if unset or empty)
|
|
|
|
func softDefault(substitution string, mapping Mapping) (string, bool, error) {
|
|
|
|
if !strings.Contains(substitution, ":-") {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
name, defaultValue := partition(substitution, ":-")
|
|
|
|
value, ok := mapping(name)
|
|
|
|
if !ok || value == "" {
|
|
|
|
return defaultValue, true, nil
|
|
|
|
}
|
|
|
|
return value, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hard default (fall back if-and-only-if empty)
|
|
|
|
func hardDefault(substitution string, mapping Mapping) (string, bool, error) {
|
|
|
|
if !strings.Contains(substitution, "-") {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
name, defaultValue := partition(substitution, "-")
|
|
|
|
value, ok := mapping(name)
|
|
|
|
if !ok {
|
|
|
|
return defaultValue, true, nil
|
|
|
|
}
|
|
|
|
return value, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func requiredNonEmpty(substitution string, mapping Mapping) (string, bool, error) {
|
|
|
|
if !strings.Contains(substitution, ":?") {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
name, errorMessage := partition(substitution, ":?")
|
|
|
|
value, ok := mapping(name)
|
|
|
|
if !ok || value == "" {
|
|
|
|
return "", true, &InvalidTemplateError{
|
|
|
|
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func required(substitution string, mapping Mapping) (string, bool, error) {
|
|
|
|
if !strings.Contains(substitution, "?") {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
name, errorMessage := partition(substitution, "?")
|
|
|
|
value, ok := mapping(name)
|
|
|
|
if !ok {
|
|
|
|
return "", true, &InvalidTemplateError{
|
|
|
|
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, true, nil
|
|
|
|
}
|
|
|
|
|
2018-02-21 15:16:12 -05:00
|
|
|
func matchGroups(matches []string) map[string]string {
|
|
|
|
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, ""
|
|
|
|
}
|