Pārlūkot izejas kodu

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 <vincent@sbr.pm>
Vincent Demeester 8 gadi atpakaļ
vecāks
revīzija
c165a8bfa1
1 mainītis faili ar 7 papildinājumiem un 4 dzēšanām
  1. 7 4
      cli/compose/interpolation/interpolation.go

+ 7 - 4
cli/compose/interpolation/interpolation.go

@@ -1,9 +1,8 @@
 package interpolation
 package interpolation
 
 
 import (
 import (
-	"fmt"
-
 	"github.com/docker/docker/cli/compose/template"
 	"github.com/docker/docker/cli/compose/template"
+	"github.com/pkg/errors"
 )
 )
 
 
 // Interpolate replaces variables in a string with the values from a mapping
 // 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
 			out[name] = nil
 			continue
 			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 {
 		if err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
@@ -37,7 +40,7 @@ func interpolateSectionItem(
 	for key, value := range item {
 	for key, value := range item {
 		interpolatedValue, err := recursiveInterpolate(value, mapping)
 		interpolatedValue, err := recursiveInterpolate(value, mapping)
 		if err != nil {
 		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 $.",
 				"Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.",
 				key, section, name, err.Template,
 				key, section, name, err.Template,
 			)
 			)