package parsers import ( "regexp" "github.rackspace.com/gorax/tools/util" ) var ( dirRe = regexp.MustCompile(`(.*?):.*?\n((.|\n)*?bytes)(?:\n\n|\nSSH|$)`) fsRe = regexp.MustCompile(`(\S+)\s+(\d+)\s+\[(.*?)\]\s+(.*?)\n`) fsSizeRe = regexp.MustCompile(`Size\s+:\s+(\d+)`) fsBytesUsedRe = regexp.MustCompile(`Bytes\s+Used\s+:\s+(\d+)`) fsBytesFreeRe = regexp.MustCompile(`Bytes\s+Free\s+:\s+(\d+)`) ) type ( // AdxDir type defines all the results from the parsed output of a // directory listing. AdxDir struct { FS []AdxFileSystem `json:"filesystems"` } // AdxFileSystem type shows parsed details about the selected file-system AdxFileSystem struct { Name string `json:"name"` Size string `json:"size"` BytesUsed string `json:"bytes used"` BytesFree string `json:"bytes free"` File []AdxFile `json:"files"` } // AdxFile type is the actual parsed data about a single file within // the directory AdxFile struct { Name string `json:"name"` Size string `json:"size"` Type string `json:"type"` Date string `json:"date"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxDir) Parse(s *string) { for _, v := range dirRe.FindAllStringSubmatch(*s, -1) { var i AdxFileSystem i.Name = v[1] i.parse(v[2]) a.FS = append(a.FS, i) } } func (i *AdxFileSystem) parse(s string) { for _, v := range fsRe.FindAllStringSubmatch(s, -1) { i.File = append(i.File, AdxFile{Name: v[1], Size: v[2], Type: v[3], Date: v[4]}) } i.Size = util.RegexpFindString(fsSizeRe, s) i.BytesUsed = util.RegexpFindString(fsBytesUsedRe, s) i.BytesFree = util.RegexpFindString(fsBytesFreeRe, s) }