github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/compose/loader/interpolate.go (about)

     1  package loader
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	interp "github.com/docker/cli/cli/compose/interpolation"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  var interpolateTypeCastMapping = map[interp.Path]interp.Cast{
    12  	servicePath("configs", interp.PathMatchList, "mode"):             toInt,
    13  	servicePath("secrets", interp.PathMatchList, "mode"):             toInt,
    14  	servicePath("healthcheck", "retries"):                            toInt,
    15  	servicePath("healthcheck", "disable"):                            toBoolean,
    16  	servicePath("deploy", "replicas"):                                toInt,
    17  	servicePath("deploy", "update_config", "parallelism"):            toInt,
    18  	servicePath("deploy", "update_config", "max_failure_ratio"):      toFloat,
    19  	servicePath("deploy", "rollback_config", "parallelism"):          toInt,
    20  	servicePath("deploy", "rollback_config", "max_failure_ratio"):    toFloat,
    21  	servicePath("deploy", "restart_policy", "max_attempts"):          toInt,
    22  	servicePath("ports", interp.PathMatchList, "target"):             toInt,
    23  	servicePath("ports", interp.PathMatchList, "published"):          toInt,
    24  	servicePath("ulimits", interp.PathMatchAll):                      toInt,
    25  	servicePath("ulimits", interp.PathMatchAll, "hard"):              toInt,
    26  	servicePath("ulimits", interp.PathMatchAll, "soft"):              toInt,
    27  	servicePath("privileged"):                                        toBoolean,
    28  	servicePath("read_only"):                                         toBoolean,
    29  	servicePath("stdin_open"):                                        toBoolean,
    30  	servicePath("tty"):                                               toBoolean,
    31  	servicePath("volumes", interp.PathMatchList, "read_only"):        toBoolean,
    32  	servicePath("volumes", interp.PathMatchList, "volume", "nocopy"): toBoolean,
    33  	iPath("networks", interp.PathMatchAll, "external"):               toBoolean,
    34  	iPath("networks", interp.PathMatchAll, "internal"):               toBoolean,
    35  	iPath("networks", interp.PathMatchAll, "attachable"):             toBoolean,
    36  	iPath("volumes", interp.PathMatchAll, "external"):                toBoolean,
    37  	iPath("secrets", interp.PathMatchAll, "external"):                toBoolean,
    38  	iPath("configs", interp.PathMatchAll, "external"):                toBoolean,
    39  }
    40  
    41  func iPath(parts ...string) interp.Path {
    42  	return interp.NewPath(parts...)
    43  }
    44  
    45  func servicePath(parts ...string) interp.Path {
    46  	return iPath(append([]string{"services", interp.PathMatchAll}, parts...)...)
    47  }
    48  
    49  func toInt(value string) (interface{}, error) {
    50  	return strconv.Atoi(value)
    51  }
    52  
    53  func toFloat(value string) (interface{}, error) {
    54  	return strconv.ParseFloat(value, 64)
    55  }
    56  
    57  // should match http://yaml.org/type/bool.html
    58  func toBoolean(value string) (interface{}, error) {
    59  	switch strings.ToLower(value) {
    60  	case "y", "yes", "true", "on":
    61  		return true, nil
    62  	case "n", "no", "false", "off":
    63  		return false, nil
    64  	default:
    65  		return nil, errors.Errorf("invalid boolean: %s", value)
    66  	}
    67  }
    68  
    69  func interpolateConfig(configDict map[string]interface{}, opts interp.Options) (map[string]interface{}, error) {
    70  	return interp.Interpolate(configDict, opts)
    71  }