package parsers import "regexp" var ( flash1Re = regexp.MustCompile(`Compressed\s+(\S+)\s+Code\s+size\s+=\s+(\d+),\s+Version\s+(\S+)\s+(.*?)\s+label:\s+(\S+)\s*\n`) flash2Re = regexp.MustCompile(`Used Configuration Flash Size=(\d+),\s+Max Configuration Flash Size=(\d+)`) flash3Re = regexp.MustCompile(`\n(.*?):\s*?\n\s+Size\s+:\s+(\d+)\s+bytes\s*?\nBytes Used\s+:\s+(\d+)\s+bytes\s*?\nBytes Free\s+:\s+(\d+)\s+bytes`) ) type ( // AdxFlash type defines all the parsed data from the flash output of // an ADX AdxFlash struct { Image []AdxImage `json:"images"` UsedFlashSize string `json:"used flash size"` MaxFlashSize string `json:"max flash size"` FS []AdxFS `json:"filesystems"` } // AdxImage defines the parsed image details AdxImage struct { Name string `json:"name"` Size string `json:"code_size"` Version string `json:"version"` DateTime string `json:"datetime"` Label string `json:"label"` } // AdxFS defines the parsed data for a file-system on the ADX AdxFS struct { Name string `json:"name"` Size string `json:"size"` BytesUsed string `json:"bytes used"` BytesFree string `json:"bytes free"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxFlash) Parse(s *string) { for _, v := range flash1Re.FindAllStringSubmatch(*s, -1) { var i AdxImage i.Name = v[1] i.Size = v[2] i.Version = v[3] i.DateTime = v[4] i.Label = v[5] a.Image = append(a.Image, i) } for _, f := range flash2Re.FindAllStringSubmatch(*s, -1) { a.UsedFlashSize = f[1] a.MaxFlashSize = f[2] } for _, v := range flash3Re.FindAllStringSubmatch(*s, -1) { var fs AdxFS fs.Name = v[1] fs.Size = v[2] fs.BytesUsed = v[3] fs.BytesFree = v[4] a.FS = append(a.FS, fs) } }