-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathnetio.go
32 lines (24 loc) · 850 Bytes
/
netio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package netio
import (
"net"
"github.com/pkg/errors"
)
//nolint:revive // keeping NetIOInterface makes sense
type NetIOInterface interface {
GetNetworkInterfaceByName(name string) (*net.Interface, error)
GetNetworkInterfaceAddrs(iface *net.Interface) ([]net.Addr, error)
}
// ErrInterfaceNil - errors out when interface is nil
var ErrInterfaceNil = errors.New("Interface is nil")
type NetIO struct{}
func (ns *NetIO) GetNetworkInterfaceByName(name string) (*net.Interface, error) {
iface, err := net.InterfaceByName(name)
return iface, errors.Wrap(err, "GetNetworkInterfaceByName failed")
}
func (ns *NetIO) GetNetworkInterfaceAddrs(iface *net.Interface) ([]net.Addr, error) {
if iface == nil {
return []net.Addr{}, ErrInterfaceNil
}
addrs, err := iface.Addrs()
return addrs, errors.Wrap(err, "GetNetworkInterfaceAddrs failed")
}