From ff2a4eeb5b95af928cfd92b4eabcb255901b77cb Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Thu, 23 Mar 2017 16:05:24 +0100 Subject: [PATCH] Make sure we error out instead of panic during interpolation Use type assertion to error out if the type isn't the right one instead of panic as before this change. Signed-off-by: Vincent Demeester Upstream-commit: fe19bc6891936fc5bb79e9e774c3d6f5e8880c84 Component: cli --- components/cli/compose/interpolation/interpolation.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/components/cli/compose/interpolation/interpolation.go b/components/cli/compose/interpolation/interpolation.go index 2a89d57482..c8e962b490 100644 --- a/components/cli/compose/interpolation/interpolation.go +++ b/components/cli/compose/interpolation/interpolation.go @@ -1,9 +1,8 @@ package interpolation import ( - "fmt" - "github.com/docker/docker/cli/compose/template" + "github.com/pkg/errors" ) // Interpolate replaces variables in a string with the values from a mapping @@ -15,7 +14,11 @@ func Interpolate(config map[string]interface{}, section string, mapping template out[name] = nil continue } - interpolatedItem, err := interpolateSectionItem(name, item.(map[string]interface{}), section, mapping) + 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 } @@ -37,7 +40,7 @@ func interpolateSectionItem( for key, value := range item { interpolatedValue, err := recursiveInterpolate(value, mapping) if err != nil { - return nil, fmt.Errorf( + 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, )