package pkg import ( "regexp" "strings" ) type ( ShowRoutes struct { Routes []*ShowRoute `json:"routes"` } ShowRoute struct { Name string `json:"name"` Destination string `json:"destination"` Type string `json:"type"` NextHop string `json:"next_hop"` Origin string `json:"orgin"` } ) func ParseShowRoutes(s string) *ShowRoutes { r := new(ShowRoutes) r.Parse(s) return r } func (r *ShowRoutes) Parse(s string) { for _, l := range strings.Split(s, "\n") { i := regexp.MustCompile(`(\S+)\s+(\S+)\s+(gw|interface)\s+(\S+)\s+(conn.*$|dyn.*$|stat.*$)`).FindStringSubmatch(l) if len(i) != 0 { r.Routes = append(r.Routes, &ShowRoute{ Name: i[1], Destination: i[2], Type: i[3], NextHop: i[4], Origin: i[5], }) } else { i = regexp.MustCompile(`^(\S+)[ ]+(\S+)[ ]+(\S+)[ ]+(\S+)`).FindStringSubmatch(l) if len(i) == 0 { continue } r.Routes = append(r.Routes, &ShowRoute{ Name: i[1], Destination: i[1], Type: i[2], NextHop: i[3], Origin: i[4], }) } // nh := SPLITTWORE.FindStringSubmatch(i[4]) // if len(nh) != 0 { // if i[3] == "blackhole" { // R.Origin = util.RegexpString(matchWordRegex, &i[4]) // continue // } // R.NextHop = nh[1] // R.Origin = nh[2] // } // r.Routes = append(r.Routes, R) } }