cli/compose: use strings.Cut

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-27 17:44:59 +01:00
parent cb19bf9f7d
commit 3bed830a27
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 15 additions and 16 deletions

View File

@ -427,11 +427,11 @@ func uint32Ptr(value uint32) *uint32 {
// convertExtraHosts converts <host>:<ip> mappings to SwarmKit notation: // convertExtraHosts converts <host>:<ip> mappings to SwarmKit notation:
// "IP-address hostname(s)". The original order of mappings is preserved. // "IP-address hostname(s)". The original order of mappings is preserved.
func convertExtraHosts(extraHosts composetypes.HostsList) []string { func convertExtraHosts(extraHosts composetypes.HostsList) []string {
hosts := []string{} hosts := make([]string, 0, len(extraHosts))
for _, hostIP := range extraHosts { for _, hostIP := range extraHosts {
if v := strings.SplitN(hostIP, ":", 2); len(v) == 2 { if hostName, ipAddr, ok := strings.Cut(hostIP, ":"); ok {
// Convert to SwarmKit notation: IP-address hostname(s) // Convert to SwarmKit notation: IP-address hostname(s)
hosts = append(hosts, fmt.Sprintf("%s %s", v[1], v[0])) hosts = append(hosts, ipAddr+" "+hostName)
} }
} }
return hosts return hosts

View File

@ -829,21 +829,20 @@ func transformListOrMapping(listOrMapping interface{}, sep string, allowNil bool
} }
func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) interface{} { func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) interface{} {
switch value := mappingOrList.(type) { switch values := mappingOrList.(type) {
case map[string]interface{}: case map[string]interface{}:
return toMapStringString(value, allowNil) return toMapStringString(values, allowNil)
case ([]interface{}): case []interface{}:
result := make(map[string]interface{}) result := make(map[string]interface{})
for _, value := range value { for _, v := range values {
parts := strings.SplitN(value.(string), sep, 2) key, val, hasValue := strings.Cut(v.(string), sep)
key := parts[0]
switch { switch {
case len(parts) == 1 && allowNil: case !hasValue && allowNil:
result[key] = nil result[key] = nil
case len(parts) == 1 && !allowNil: case !hasValue && !allowNil:
result[key] = "" result[key] = ""
default: default:
result[key] = parts[1] result[key] = val
} }
} }
return result return result

View File

@ -239,9 +239,9 @@ func matchGroups(matches []string, pattern *regexp.Regexp) map[string]string {
// //
// If the separator is not found, return the string itself, followed by an empty string. // If the separator is not found, return the string itself, followed by an empty string.
func partition(s, sep string) (string, string) { func partition(s, sep string) (string, string) {
if strings.Contains(s, sep) { k, v, ok := strings.Cut(s, sep)
parts := strings.SplitN(s, sep, 2) if !ok {
return parts[0], parts[1]
}
return s, "" return s, ""
} }
return k, v
}