package parsers import ( "regexp" "github.rackspace.com/gorax/tools/util" ) var ( serversRe = regexp.MustCompile(`server\s+(real|remote-name)\s+(\S+)\s+(\S+)\n((.|\n)*?)!`) sourceNATRe = regexp.MustCompile(`(source-nat)`) ) type ( // AdxServers type aggregates a listing of real and remote server configurations AdxServers struct { Real []*RealServer `json:"real"` Remote []*RemoteServer `json:"remote-name"` } // RealServer type defines attributes about the real-server configuration RealServer struct { Name *string `json:"name"` IpAddr *string `json:"ip_address"` Ports []Port `json:"ports"` SrcNat bool `json:"source_nat"` } // RemoteServer type defines attributes about the remote-server configuration RemoteServer struct { Name *string `json:"name"` IpAddr *string `json:"ip_address"` Ports []*Port `json:"ports"` SrcNat bool `json:"source_nat"` } // Port : I'm not sure what we're doing here... Port struct { } ) // TODO: Finish parsing // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxServers) Parse(s *string) { for _, v := range serversRe.FindAllStringSubmatch(*s, -1) { switch v[1] { case "real": var r RealServer r.parse(v[2], v[3], v[4]) r.SrcNat = sourceNat(v[4]) a.Real = append(a.Real, &r) case "remote-name": var r RemoteServer r.parse(v[2], v[3], v[4]) r.SrcNat = sourceNat(v[4]) a.Remote = append(a.Remote, &r) } } } func (r *RealServer) parse(n, i, c string) { r.Name = &n r.IpAddr = &i } func (r *RemoteServer) parse(n, i, c string) { r.Name = &n r.IpAddr = &i } func sourceNat(s string) bool { return util.RegexpFindStringPtr(sourceNATRe, s) != nil }