package parsers import ( "regexp" "github.rackspace.com/gorax/tools/util" ) // used with "sh int ve 10" var ( veIntRe = regexp.MustCompile(`(?:(\S+)\sis\s(up|down),\sline\sprotocol\sis\s(up|down).*)\n(?:(\s.*)\n)+`) veHardwareRe = regexp.MustCompile(`Hardware\s+is\s+(.*?),`) veMACAddrRe = regexp.MustCompile(`address\s+is\s+(\S+)`) veBIAMACAddrRe = regexp.MustCompile(`\(bia\s+(\S+?)\)`) veIPAddrRe = regexp.MustCompile(`Internet\s+address\s+is\s+(\S+)\/\d+`) veCIDRRe = regexp.MustCompile(`Internet\s+address\s+is\s+\S+\/(\d+)`) veMTURe = regexp.MustCompile(`MTU\s+(\d+)\s+bytes`) ) type ( // AdxVEInterfaces is an aggregate type to contain a listing of all the // virtual-interfaces on an ADX AdxVEInterfaces struct { Int []AdxVEInterface `json:"interfaces"` } // AdxVEInterface is the parsed representation of a single virtual-interface AdxVEInterface struct { Name string `json:"name"` State string `json:"state"` LineProtocol string `json:"line protocol"` Hardware string `json:"hardware"` MacAddress string `json:"mac address"` BiaMacAddress string `json:"bia mac address"` IPAddress string `json:"ip address"` CIDRNetmask string `json:"cidr netmask"` MTU string `json:"mtu"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxVEInterfaces) Parse(s *string) { for _, v := range veIntRe.FindAllStringSubmatch(*s, -1) { var i AdxVEInterface i.Name = v[1] i.State = v[2] i.LineProtocol = v[3] i.parse(v[0]) a.Int = append(a.Int, i) } } func (i *AdxVEInterface) parse(s string) { i.Hardware = util.RegexpFindString(veHardwareRe, s) i.MacAddress = util.RegexpFindString(veMACAddrRe, s) i.BiaMacAddress = util.RegexpFindString(veBIAMACAddrRe, s) i.IPAddress = util.RegexpFindString(veIPAddrRe, s) i.CIDRNetmask = util.RegexpFindString(veCIDRRe, s) i.MTU = util.RegexpFindString(veMTURe, s) }