libnetwork: use strings.Cut() and minor refactor

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-01 12:50:18 +01:00
parent 87ca9490b0
commit 46f7c92c9a
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 18 additions and 29 deletions

View file

@ -75,20 +75,19 @@ func Build(path, IP, hostname, domainname string, extraContent []Record) error {
content := bytes.NewBuffer(nil)
if IP != "" {
//set main record
// set main record
var mainRec Record
mainRec.IP = IP
// User might have provided a FQDN in hostname or split it across hostname
// and domainname. We want the FQDN and the bare hostname.
fqdn := hostname
if domainname != "" {
fqdn = fmt.Sprintf("%s.%s", fqdn, domainname)
fqdn += "." + domainname
}
parts := strings.SplitN(fqdn, ".", 2)
if len(parts) == 2 {
mainRec.Hosts = fmt.Sprintf("%s %s", fqdn, parts[0])
} else {
mainRec.Hosts = fqdn
mainRec.Hosts = fqdn
if hostName, _, ok := strings.Cut(fqdn, "."); ok {
mainRec.Hosts += " " + hostName
}
if _, err := mainRec.WriteTo(content); err != nil {
return err

View file

@ -30,7 +30,7 @@ const (
// DNSServers A list of DNS servers associated with the endpoint
DNSServers = Prefix + ".endpoint.dnsservers"
//EnableIPv6 constant represents enabling IPV6 at network level
// EnableIPv6 constant represents enabling IPV6 at network level
EnableIPv6 = Prefix + ".enable_ipv6"
// DriverMTU constant represents the MTU size for the network driver
@ -106,27 +106,18 @@ func MakeKVClient(scope string) string {
// Key extracts the key portion of the label
func Key(label string) (key string) {
if kv := strings.SplitN(label, "=", 2); len(kv) > 0 {
key = kv[0]
}
return
key, _, _ = strings.Cut(label, "=")
return key
}
// Value extracts the value portion of the label
func Value(label string) (value string) {
if kv := strings.SplitN(label, "=", 2); len(kv) > 1 {
value = kv[1]
}
return
_, value, _ = strings.Cut(label, "=")
return value
}
// KeyValue decomposes the label in the (key,value) pair
func KeyValue(label string) (key string, value string) {
if kv := strings.SplitN(label, "=", 2); len(kv) > 0 {
key = kv[0]
if len(kv) > 1 {
value = kv[1]
}
}
return
key, value, _ = strings.Cut(label, "=")
return key, value
}

View file

@ -110,19 +110,18 @@ func (sb *sandbox) updateHostsFile(ifaceIPs []string) error {
// User might have provided a FQDN in hostname or split it across hostname
// and domainname. We want the FQDN and the bare hostname.
fqdn := sb.config.hostName
mhost := sb.config.hostName
if sb.config.domainName != "" {
fqdn = fmt.Sprintf("%s.%s", fqdn, sb.config.domainName)
fqdn += "." + sb.config.domainName
}
hosts := fqdn
parts := strings.SplitN(fqdn, ".", 2)
if len(parts) == 2 {
mhost = fmt.Sprintf("%s %s", fqdn, parts[0])
if hostName, _, ok := strings.Cut(fqdn, "."); ok {
hosts += " " + hostName
}
var extraContent []etchosts.Record
for _, ip := range ifaceIPs {
extraContent = append(extraContent, etchosts.Record{Hosts: mhost, IP: ip})
extraContent = append(extraContent, etchosts.Record{Hosts: hosts, IP: ip})
}
sb.addHostsEntries(extraContent)