package pkg import ( "regexp" //"fmt" "strings" //"strconv" //"log" "github.rackspace.com/gorax/tools/util" ) var ( rgxNotEmpty = regexp.MustCompile(`(\S+)`) ) type ( //Filesystems holds data for all filesystems Filesystems struct { Info []FilesystemInfo `json:"Info"` } //FilesystemInfo contains data for a single filesystem FilesystemInfo struct { Device string `json:"device"` Label *string `json:"label"` UUID *string `json:"uuid"` SecondaryType *string `json:"secondary_type"` Type string `json:"type"` Mounted *bool `json:"mounted"` } ) //ParseFilesystemInfo will parse output of a blkid command func ParseFilesystemInfo(s string) (d Filesystems, err error) { lines := strings.Split(s, "\n") for _, line := range lines { match := regexp.MustCompile(`(/dev/\S+):(?:\s+LABEL="(\S+)")?(?:\s+UUID="(\S+)")?(?:\s+SEC_TYPE="(\S+)")?(?:\s+TYPE="(\S+)")?`).FindStringSubmatch(line) if len(match) != 6 { continue } d.Info = append(d.Info, FilesystemInfo{ Device: match[1], Label: util.RegexpFindStringPtr(rgxNotEmpty, match[2]), UUID: util.RegexpFindStringPtr(rgxNotEmpty, match[3]), SecondaryType: util.RegexpFindStringPtr(rgxNotEmpty, match[4]), Type: match[5]}) } return d, nil } //SetFilesystemMounted will determine if a filesystem is mounted and update the object appropriately func SetFilesystemMounted(filesystems *Filesystems, dfspace DfSpace) error { var mountedUuids []string for _, df := range dfspace.Df { for _, f := range filesystems.Info { if df.FileSys == f.Device { mountedUuids = append(mountedUuids, *f.UUID) } } } for i, f := range filesystems.Info { t := true filesystems.Info[i].Mounted = &t var isMounted = false for _, uuid := range mountedUuids { if f.UUID == nil { continue } if uuid == *f.UUID { isMounted = true } } if isMounted != true { f := false filesystems.Info[i].Mounted = &f } } return nil }