From c59bdd18d521ebb3c8dedf03a6adfa56dd6245c7 Mon Sep 17 00:00:00 2001 From: Zeling Feng Date: Mon, 9 Nov 2020 13:14:23 -0800 Subject: parameterize regexp in netdevs.ParseDevices PiperOrigin-RevId: 341470647 --- test/packetimpact/netdevs/netdevs.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'test/packetimpact/netdevs/netdevs.go') diff --git a/test/packetimpact/netdevs/netdevs.go b/test/packetimpact/netdevs/netdevs.go index 006988896..25dcfbf60 100644 --- a/test/packetimpact/netdevs/netdevs.go +++ b/test/packetimpact/netdevs/netdevs.go @@ -43,14 +43,10 @@ var ( inet6Line = regexp.MustCompile(`^\s*inet6 ([0-9a-fA-F:/]+)`) ) -// ParseDevices parses the output from `ip addr show` into a map from device -// name to information about the device. -// -// Note: if multiple IPv6 addresses are assigned to a device, the last address -// displayed by `ip addr show` will be used. This is fine for packetimpact -// because we will always only have at most one IPv6 address assigned to each -// device. -func ParseDevices(cmdOutput string) (map[string]DeviceInfo, error) { +// ParseDevicesWithRegex will parse the output with the given regexps to produce +// a map from device name to device information. It is assumed that deviceLine +// contains both a name and an ID. +func ParseDevicesWithRegex(cmdOutput string, deviceLine, linkLine, inetLine, inet6Line *regexp.Regexp) (map[string]DeviceInfo, error) { var currentDevice string var currentInfo DeviceInfo deviceInfos := make(map[string]DeviceInfo) @@ -93,6 +89,17 @@ func ParseDevices(cmdOutput string) (map[string]DeviceInfo, error) { return deviceInfos, nil } +// ParseDevices parses the output from `ip addr show` into a map from device +// name to information about the device. +// +// Note: if multiple IPv6 addresses are assigned to a device, the last address +// displayed by `ip addr show` will be used. This is fine for packetimpact +// because we will always only have at most one IPv6 address assigned to each +// device. +func ParseDevices(cmdOutput string) (map[string]DeviceInfo, error) { + return ParseDevicesWithRegex(cmdOutput, deviceLine, linkLine, inetLine, inet6Line) +} + // MACToIP converts the MAC address to an IPv6 link local address as described // in RFC 4291 page 20: https://tools.ietf.org/html/rfc4291#page-20 func MACToIP(mac net.HardwareAddr) net.IP { -- cgit v1.2.3