2017-10-03 18:03:20 -04:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
interp "github.com/docker/cli/cli/compose/interpolation"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-10-04 16:51:48 -04:00
|
|
|
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,
|
2019-06-26 09:22:08 -04:00
|
|
|
servicePath("deploy", "rollback_config", "parallelism"): toInt,
|
|
|
|
servicePath("deploy", "rollback_config", "max_failure_ratio"): toFloat,
|
2017-10-04 16:51:48 -04:00
|
|
|
servicePath("deploy", "restart_policy", "max_attempts"): toInt,
|
2019-11-04 08:21:47 -05:00
|
|
|
servicePath("deploy", "placement", "max_replicas_per_node"): toInt,
|
2017-10-04 16:51:48 -04:00
|
|
|
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,
|
2017-10-03 18:03:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func iPath(parts ...string) interp.Path {
|
2017-10-04 16:51:48 -04:00
|
|
|
return interp.NewPath(parts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func servicePath(parts ...string) interp.Path {
|
|
|
|
return iPath(append([]string{"services", interp.PathMatchAll}, parts...)...)
|
2017-10-03 18:03:20 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 12:04:36 -05:00
|
|
|
func toInt(value string) (any, error) {
|
2017-10-03 18:03:20 -04:00
|
|
|
return strconv.Atoi(value)
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:04:36 -05:00
|
|
|
func toFloat(value string) (any, error) {
|
2017-10-03 18:03:20 -04:00
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should match http://yaml.org/type/bool.html
|
2023-11-20 12:04:36 -05:00
|
|
|
func toBoolean(value string) (any, error) {
|
2017-10-03 18:03:20 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:04:36 -05:00
|
|
|
func interpolateConfig(configDict map[string]any, opts interp.Options) (map[string]any, error) {
|
2018-06-25 11:15:26 -04:00
|
|
|
return interp.Interpolate(configDict, opts)
|
2017-10-03 18:03:20 -04:00
|
|
|
}
|