Skip to content

Commit

Permalink
Add lines
Browse files Browse the repository at this point in the history
  • Loading branch information
blp1526 committed Mar 2, 2019
1 parent 8680d54 commit 5e2d66d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/blp1526/blkinfo.svg?branch=travis)](https://travis-ci.org/blp1526/blkinfo)
[![Build Status](https://travis-ci.org/blp1526/blkinfo.svg?branch=master)](https://travis-ci.org/blp1526/blkinfo)

# blkinfo

Expand Down
41 changes: 14 additions & 27 deletions blkinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func New(devPath string) (*BlkInfo, error) {
return nil, err
}

bi.Sys.Uevent, err = sysUevent(bi.SysPath)
bi.Sys.Uevent, err = lines(filepath.Join(bi.SysPath, "uevent"))
if err != nil {
return nil, err
}
Expand All @@ -75,7 +75,7 @@ func New(devPath string) (*BlkInfo, error) {

bi.UdevDataPath = udevDataPath(majorMinor)

bi.UdevData, err = udevData(bi.UdevDataPath)
bi.UdevData, err = lines(bi.UdevDataPath)
if err != nil {
return nil, err
}
Expand All @@ -102,6 +102,15 @@ func readFile(path string) (string, error) {
return strings.TrimSpace(string(b)), nil
}

func lines(path string) ([]string, error) {
text, err := readFile(path)
if err != nil {
return []string{}, err
}

return strings.Split(text, "\n"), nil
}

func trimQuotationMarks(s string) string {
for _, q := range []string{`"`, `'`} {
if strings.HasPrefix(s, q) && strings.HasSuffix(s, q) {
Expand Down Expand Up @@ -207,15 +216,6 @@ func paths(realPath string) (sysPath string, parentPath string, childPaths []str
return "", "", []string{}, errors.New("sysPath, parentPath, and childPaths are not found")
}

func sysUevent(sysPath string) ([]string, error) {
sysUevent, err := readFile(filepath.Join(sysPath, "uevent"))
if err != nil {
return []string{}, err
}

return strings.Split(sysUevent, "\n"), nil
}

func majorMinor(sysPath string) (string, error) {
majorMinor, err := readFile(filepath.Join(sysPath, "dev"))
if err != nil {
Expand All @@ -229,30 +229,17 @@ func udevDataPath(majorMinor string) string {
return filepath.Join("/", "run", "udev", "data", "b"+majorMinor)
}

func udevData(udevDataPath string) ([]string, error) {
rawUdevData, err := readFile(udevDataPath)
if err != nil {
return []string{}, err
}

udevData := strings.Split(rawUdevData, "\n")
return udevData, nil
}

// OSRelease shows /etc/os-release.
func (bi *BlkInfo) OSRelease() ([]string, error) {
osRelease := []string{}

if bi.Mountpoint == "" {
return osRelease, errors.New("this device is not mounted")
return []string{}, errors.New("this device is not mounted")
}

osReleasePath := filepath.Join(bi.Mountpoint, "etc", "os-release")
rawOSRelease, err := readFile(osReleasePath)
osRelease, err := lines(osReleasePath)
if err != nil {
return osRelease, err
return []string{}, err
}

osRelease = strings.Split(rawOSRelease, "\n")
return osRelease, nil
}

0 comments on commit 5e2d66d

Please sign in to comment.