package parsers import ( "fmt" "regexp" "strconv" "strings" "github.rackspace.com/gorax/tools/util" ) type ( // AdxArp is an aggregate type containing a list // of parsed arp entries on the device AdxArp struct { Arps []Arp `json:"arp"` } // Arp type is the parsed elements provided in an ADX // ARP entry Arp struct { Number int `json:"number"` IpAddr string `json:"ip_address"` MacAddr string `json:"mac_address"` Type string `json:"type"` Age int `json:"age"` Port string `json:"port"` } ) var arpRe = regexp.MustCompile(`(?i)([0-9]+)\s+(\S+)\s+(\S+)\s+(\S+)\s+([0-9]+)\s+(\S+)`) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxArp) Parse(s *string) { for _, i := range arpRe.FindAllStringSubmatch(*s, -1) { a.Arps = append(a.Arps, Arp{ Number: util.Atoi(i[1]), IpAddr: i[2], MacAddr: i[3], Type: i[4], Age: util.Atoi(i[5]), Port: func() string { if strings.HasPrefix(i[6], "mgmt") { return i[6] } if n, err := strconv.Atoi(i[6]); err == nil { return fmt.Sprintf("ethernet%d", n) } return "" }(), }) } }