Factor out the wait-for-interface code

This commit is contained in:
Michael Bridgen committed 2014-11-03 17:47:16 +00:00
1 parent 8ce6c4aba3
commit 27beebf5d5
3 files changed
+9 -36

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
package net
import (
"fmt"
"net"
"time"
)
func EnsureInterface(ifaceName string, wait int) (iface *net.Interface, err error) {
if iface, err = findInterface(ifaceName); err == nil || wait == 0 {
return
}
for ; err != nil && wait > 0; wait -= 1 {
time.Sleep(1 * time.Second)
iface, err = findInterface(ifaceName)
}
return
}
func findInterface(ifaceName string) (iface *net.Interface, err error) {
if iface, err = net.InterfaceByName(ifaceName); err != nil {
return iface, fmt.Errorf("Unable to find interface %s", ifaceName)
}
if 0 == (net.FlagUp & iface.Flags) {
return iface, fmt.Errorf("Interface %s is not up", ifaceName)
}
return
}