Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 108 additions & 46 deletions cli/compose/interpolation/interpolation.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,71 @@
package interpolation

import (
"os"
"strings"

"github.com/docker/cli/cli/compose/template"
"github.com/pkg/errors"
)

// Interpolate replaces variables in a string with the values from a mapping
func Interpolate(config map[string]interface{}, section string, mapping template.Mapping) (map[string]interface{}, error) {
out := map[string]interface{}{}
// Options supported by Interpolate
type Options struct {
// LookupValue from a key
LookupValue LookupValue
// TypeCastMapping maps key paths to functions to cast to a type
TypeCastMapping map[Path]Cast
}

for name, item := range config {
if item == nil {
out[name] = nil
continue
}
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)
if err != nil {
return nil, err
}
out[name] = interpolatedItem
}
// LookupValue is a 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 LookupValue func(key string) (string, bool)

return out, nil
}
// Cast a value to a new type, or return an error if the value can't be cast
type Cast func(value string) (interface{}, error)

func interpolateSectionItem(
name string,
item map[string]interface{},
section string,
mapping template.Mapping,
) (map[string]interface{}, error) {
// Interpolate replaces variables in a string with the values from a mapping
func Interpolate(config map[string]interface{}, opts Options) (map[string]interface{}, error) {
if opts.LookupValue == nil {
opts.LookupValue = os.LookupEnv
}
if opts.TypeCastMapping == nil {
opts.TypeCastMapping = make(map[Path]Cast)
}

out := map[string]interface{}{}

for key, value := range item {
interpolatedValue, err := recursiveInterpolate(value, mapping)
switch err := err.(type) {
case nil:
case *template.InvalidTemplateError:
return nil, errors.Errorf(
"Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.",
key, section, name, err.Template,
)
default:
return nil, errors.Wrapf(err, "error while interpolating %s in %s %s", key, section, name)
for key, value := range config {
interpolatedValue, err := recursiveInterpolate(value, NewPath(key), opts)
if err != nil {
return out, err
}
out[key] = interpolatedValue
}

return out, nil

}

func recursiveInterpolate(
value interface{},
mapping template.Mapping,
) (interface{}, error) {

func recursiveInterpolate(value interface{}, path Path, opts Options) (interface{}, error) {
switch value := value.(type) {

case string:
return template.Substitute(value, mapping)
newValue, err := template.Substitute(value, template.Mapping(opts.LookupValue))
if err != nil || newValue == value {
return value, newPathError(path, err)
}
caster, ok := opts.getCasterForPath(path)
if !ok {
return newValue, nil
}
casted, err := caster(newValue)
return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type"))

case map[string]interface{}:
out := map[string]interface{}{}
for key, elem := range value {
interpolatedElem, err := recursiveInterpolate(elem, mapping)
interpolatedElem, err := recursiveInterpolate(elem, path.Next(key), opts)
if err != nil {
return nil, err
}
Expand All @@ -80,7 +76,7 @@ func recursiveInterpolate(
case []interface{}:
out := make([]interface{}, len(value))
for i, elem := range value {
interpolatedElem, err := recursiveInterpolate(elem, mapping)
interpolatedElem, err := recursiveInterpolate(elem, path.Next(PathMatchList), opts)
if err != nil {
return nil, err
}
Expand All @@ -92,5 +88,71 @@ func recursiveInterpolate(
return value, nil

}
}

func newPathError(path Path, err error) error {
switch err := err.(type) {
case nil:
return nil
case *template.InvalidTemplateError:
return errors.Errorf(
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
path, err.Template)
default:
return errors.Wrapf(err, "error while interpolating %s", path)
}
}

const pathSeparator = "."

// PathMatchAll is a token used as part of a Path to match any key at that level
// in the nested structure
const PathMatchAll = "*"

// PathMatchList is a token used as part of a Path to match items in a list
const PathMatchList = "[]"

// Path is a dotted path of keys to a value in a nested mapping structure. A *
// section in a path will match any key in the mapping structure.
type Path string

// NewPath returns a new Path
func NewPath(items ...string) Path {
return Path(strings.Join(items, pathSeparator))
}

// Next returns a new path by append part to the current path
func (p Path) Next(part string) Path {
return Path(string(p) + pathSeparator + part)
}

func (p Path) parts() []string {
return strings.Split(string(p), pathSeparator)
}

func (p Path) matches(pattern Path) bool {
patternParts := pattern.parts()
parts := p.parts()

if len(patternParts) != len(parts) {
return false
}
for index, part := range parts {
switch patternParts[index] {
case PathMatchAll, part:
continue
default:
return false
}
}
return true
}

func (o Options) getCasterForPath(path Path) (Cast, bool) {
for pattern, caster := range o.TypeCastMapping {
if path.matches(pattern) {
return caster, true
}
}
return nil, false
}
99 changes: 94 additions & 5 deletions cli/compose/interpolation/interpolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package interpolation
import (
"testing"

"strconv"

"github.com/gotestyourself/gotestyourself/env"
"github.com/stretchr/testify/assert"
)

var defaults = map[string]string{
"USER": "jenny",
"FOO": "bar",
"USER": "jenny",
"FOO": "bar",
"count": "5",
}

func defaultMapping(name string) (string, bool) {
Expand Down Expand Up @@ -41,7 +45,7 @@ func TestInterpolate(t *testing.T) {
},
},
}
result, err := Interpolate(services, "service", defaultMapping)
result, err := Interpolate(services, Options{LookupValue: defaultMapping})
assert.NoError(t, err)
assert.Equal(t, expected, result)
}
Expand All @@ -52,6 +56,91 @@ func TestInvalidInterpolation(t *testing.T) {
"image": "${",
},
}
_, err := Interpolate(services, "service", defaultMapping)
assert.EqualError(t, err, `Invalid interpolation format for "image" option in service "servicea": "${". You may need to escape any $ with another $.`)
_, err := Interpolate(services, Options{LookupValue: defaultMapping})
assert.EqualError(t, err, `invalid interpolation format for servicea.image: "${". You may need to escape any $ with another $.`)
}

func TestInterpolateWithDefaults(t *testing.T) {
defer env.Patch(t, "FOO", "BARZ")()

config := map[string]interface{}{
"networks": map[string]interface{}{
"foo": "thing_${FOO}",
},
}
expected := map[string]interface{}{
"networks": map[string]interface{}{
"foo": "thing_BARZ",
},
}
result, err := Interpolate(config, Options{})
assert.NoError(t, err)
assert.Equal(t, expected, result)
}

func TestInterpolateWithCast(t *testing.T) {
config := map[string]interface{}{
"foo": map[string]interface{}{
"replicas": "$count",
},
}
toInt := func(value string) (interface{}, error) {
return strconv.Atoi(value)
}
result, err := Interpolate(config, Options{
LookupValue: defaultMapping,
TypeCastMapping: map[Path]Cast{NewPath(PathMatchAll, "replicas"): toInt},
})
assert.NoError(t, err)
expected := map[string]interface{}{
"foo": map[string]interface{}{
"replicas": 5,
},
}
assert.Equal(t, expected, result)
}

func TestPathMatches(t *testing.T) {
var testcases = []struct {
doc string
path Path
pattern Path
expected bool
}{
{
doc: "pattern too short",
path: NewPath("one", "two", "three"),
pattern: NewPath("one", "two"),
},
{
doc: "pattern too long",
path: NewPath("one", "two"),
pattern: NewPath("one", "two", "three"),
},
{
doc: "pattern mismatch",
path: NewPath("one", "three", "two"),
pattern: NewPath("one", "two", "three"),
},
{
doc: "pattern mismatch with match-all part",
path: NewPath("one", "three", "two"),
pattern: NewPath(PathMatchAll, "two", "three"),
},
{
doc: "pattern match with match-all part",
path: NewPath("one", "two", "three"),
pattern: NewPath("one", "*", "three"),
expected: true,
},
{
doc: "pattern match",
path: NewPath("one", "two", "three"),
pattern: NewPath("one", "two", "three"),
expected: true,
},
}
for _, testcase := range testcases {
assert.Equal(t, testcase.expected, testcase.path.matches(testcase.pattern))
}
}
74 changes: 74 additions & 0 deletions cli/compose/loader/interpolate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package loader

import (
"strconv"
"strings"

interp "github.com/docker/cli/cli/compose/interpolation"
"github.com/pkg/errors"
)

var interpolateTypeCastMapping = map[interp.Path]interp.Cast{
servicePath("configs", interp.PathMatchList, "mode"): toInt,
servicePath("secrets", interp.PathMatchList, "mode"): toInt,
servicePath("healthcheck", "retries"): toInt,
servicePath("healthcheck", "disable"): toBoolean,
servicePath("deploy", "replicas"): toInt,
servicePath("deploy", "update_config", "parallelism"): toInt,
servicePath("deploy", "update_config", "max_failure_ratio"): toFloat,
servicePath("deploy", "restart_policy", "max_attempts"): toInt,
servicePath("ports", interp.PathMatchList, "target"): toInt,
servicePath("ports", interp.PathMatchList, "published"): toInt,
servicePath("ulimits", interp.PathMatchAll): toInt,
servicePath("ulimits", interp.PathMatchAll, "hard"): toInt,
servicePath("ulimits", interp.PathMatchAll, "soft"): toInt,
servicePath("privileged"): toBoolean,
servicePath("read_only"): toBoolean,
servicePath("stdin_open"): toBoolean,
servicePath("tty"): toBoolean,
servicePath("volumes", interp.PathMatchList, "read_only"): toBoolean,
servicePath("volumes", interp.PathMatchList, "volume", "nocopy"): toBoolean,
iPath("networks", interp.PathMatchAll, "external"): toBoolean,
iPath("networks", interp.PathMatchAll, "internal"): toBoolean,
iPath("networks", interp.PathMatchAll, "attachable"): toBoolean,
iPath("volumes", interp.PathMatchAll, "external"): toBoolean,
iPath("secrets", interp.PathMatchAll, "external"): toBoolean,
iPath("configs", interp.PathMatchAll, "external"): toBoolean,
}

func iPath(parts ...string) interp.Path {
return interp.NewPath(parts...)
}

func servicePath(parts ...string) interp.Path {
return iPath(append([]string{"services", interp.PathMatchAll}, parts...)...)
}

func toInt(value string) (interface{}, error) {
return strconv.Atoi(value)
}

func toFloat(value string) (interface{}, error) {
return strconv.ParseFloat(value, 64)
}

// should match http://yaml.org/type/bool.html
func toBoolean(value string) (interface{}, error) {
switch strings.ToLower(value) {
case "y", "yes", "true", "on":
return true, nil
case "n", "no", "false", "off":
return false, nil
default:
return nil, errors.Errorf("invalid boolean: %s", value)
}
}

func interpolateConfig(configDict map[string]interface{}, lookupEnv interp.LookupValue) (map[string]interface{}, error) {
return interp.Interpolate(
configDict,
interp.Options{
LookupValue: lookupEnv,
TypeCastMapping: interpolateTypeCastMapping,
})
}
Loading