Factor out the wait-for-interface code
This commit is contained in:
1 parent
8ce6c4aba3
commit
27beebf5d5
3 files changed
+9
-36
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user