Review feedback
This commit is contained in:
1 parent
e4458372e8
commit
fa6777004f
1 file changed
+16
-3
+16
-3
@@ -64,7 +64,6 @@ func checkErr(err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepend the addresses to /etc/hosts, removing the docker address
|
|
||||||
func updateHosts(iface *net.Interface) {
|
func updateHosts(iface *net.Interface) {
|
||||||
addrs, err := iface.Addrs()
|
addrs, err := iface.Addrs()
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
@@ -117,7 +116,7 @@ func parseHosts() map[string][]string {
|
|||||||
|
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
if len(fields) > 0 {
|
if len(fields) > 0 {
|
||||||
ips[fields[0]] = fields[1:]
|
ips[fields[0]] = append(ips[fields[0]], fields[1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checkErr(scanner.Err())
|
checkErr(scanner.Err())
|
||||||
@@ -132,10 +131,24 @@ func writeHosts(contents map[string][]string) {
|
|||||||
sort.Strings(ips)
|
sort.Strings(ips)
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
fmt.Fprintln(buf, "# modified by weave")
|
||||||
for _, ip := range ips {
|
for _, ip := range ips {
|
||||||
if addrs := contents[ip]; len(addrs) > 0 {
|
if addrs := contents[ip]; len(addrs) > 0 {
|
||||||
fmt.Fprintf(buf, "%s\t%s\n", ip, strings.Join(addrs, " "))
|
fmt.Fprintf(buf, "%s\t%s\n", ip, strings.Join(uniqueStrs(addrs), " "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checkErr(ioutil.WriteFile("/etc/hosts", buf.Bytes(), 644))
|
checkErr(ioutil.WriteFile("/etc/hosts", buf.Bytes(), 644))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func uniqueStrs(s []string) []string {
|
||||||
|
m := map[string]struct{}{}
|
||||||
|
result := []string{}
|
||||||
|
for _, str := range s {
|
||||||
|
if _, ok := m[str]; !ok {
|
||||||
|
m[str] = struct{}{}
|
||||||
|
result = append(result, str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
Reference in new issue
Block a user