package parsers import ( "regexp" "strings" ) var configSyncRe = regexp.MustCompile(`(.*?)(sender|receiver)(\s+|\n)(config-sync\s+|)ethernet\s+(\S+)\s(mac|vlan-id)\s+(\S+)(\s+vlan-id\s+(\S+)|\n)\n*`) type AdxConfigSync struct { Interface string `json:"interface"` PeerMac string `json:"peer_mac"` Vlan string `json:"vlan"` Mode string `json:"mode"` } // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxConfigSync) Parse(s *string) AdxConfigSync { for _, v := range configSyncRe.FindAllStringSubmatch(*s, -1) { // If the response is indicative of a 12.5+ config-sync config if strings.Contains(v[1], "mode") { //12.5 a.Interface = v[5] a.PeerMac = v[7] a.Vlan = v[9] a.Mode = v[2] } else { //12.4 (with MAC) if strings.Contains(v[6], "mac") { a.Interface = v[5] a.PeerMac = v[7] a.Vlan = v[9] a.Mode = v[2] } else { //12.4 (without MAC) a.Interface = v[5] a.Vlan = v[7] a.Mode = v[2] } } } return *a }