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