Skip to content

Commit

Permalink
refactor: use return fast to make code readable
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <[email protected]>
  • Loading branch information
allencloud committed Oct 28, 2018
1 parent 555e253 commit fedc003
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions pkg/netutils/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,27 @@ func isLoopbackOrPointToPoint(intf *net.Interface) bool {
// getMatchingGlobalIP returns the first valid global unicast address of the given
// 'family' from the list of 'addrs'.
func getMatchingGlobalIP(addrs []net.Addr, family AddressFamily) (net.IP, error) {
if len(addrs) > 0 {
for i := range addrs {
glog.V(4).Infof("Checking addr %s.", addrs[i].String())
ip, _, err := net.ParseCIDR(addrs[i].String())
if err != nil {
return nil, err
}
if memberOf(ip, family) {
if ip.IsGlobalUnicast() {
glog.V(4).Infof("IP found %v", ip)
return ip, nil
}
glog.V(4).Infof("Non-global unicast address found %v", ip)
} else {
glog.V(4).Infof("%v is not an IPv%d address", ip, int(family))
}
if len(addrs) == 0 {
return nil, nil
}

for i := range addrs {
glog.V(4).Infof("Checking addr %s.", addrs[i].String())
ip, _, err := net.ParseCIDR(addrs[i].String())
if err != nil {
return nil, err
}
if memberOf(ip, family) {
if ip.IsGlobalUnicast() {
glog.V(4).Infof("IP found %v", ip)
return ip, nil
}
glog.V(4).Infof("Non-global unicast address found %v", ip)
} else {
glog.V(4).Infof("%v is not an IPv%d address", ip, int(family))
}
}

return nil, nil
}

Expand All @@ -200,21 +202,26 @@ func getIPFromInterface(intfName string, forFamily AddressFamily, nw networkInte
if err != nil {
return nil, err
}
if isInterfaceUp(intf) {
addrs, err := nw.Addrs(intf)
if err != nil {
return nil, err
}
glog.V(4).Infof("Interface %q has %d addresses :%v.", intfName, len(addrs), addrs)
matchingIP, err := getMatchingGlobalIP(addrs, forFamily)
if err != nil {
return nil, err
}
if matchingIP != nil {
glog.V(4).Infof("Found valid IPv%d address %v for interface %q.", int(forFamily), matchingIP, intfName)
return matchingIP, nil
}

if !isInterfaceUp(intf) {
return nil, nil
}

addrs, err := nw.Addrs(intf)
if err != nil {
return nil, err
}

glog.V(4).Infof("Interface %q has %d addresses :%v.", intfName, len(addrs), addrs)
matchingIP, err := getMatchingGlobalIP(addrs, forFamily)
if err != nil {
return nil, err
}
if matchingIP != nil {
glog.V(4).Infof("Found valid IPv%d address %v for interface %q.", int(forFamily), matchingIP, intfName)
return matchingIP, nil
}

return nil, nil
}

Expand Down

0 comments on commit fedc003

Please sign in to comment.