Skip to content

Commit

Permalink
osprepare: make gofmt / vet / lint clean
Browse files Browse the repository at this point in the history
A few minor cleanups for go idioms and unexporting local-only stuff.

Signed-off-by: Tim Pepper <[email protected]>
  • Loading branch information
Tim Pepper committed Aug 23, 2016
1 parent 245b16f commit 77df18d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 63 deletions.
2 changes: 1 addition & 1 deletion osprepare/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type distro interface {
// getDistro will return a distro based on what
// is read from GetOsRelease
func getDistro() distro {
osRelease := GetOsRelease()
osRelease := getOSRelease()

if osRelease == nil {
return nil
Expand Down
5 changes: 3 additions & 2 deletions osprepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type PackageRequirement struct {
// )
type PackageRequirements map[string][]*PackageRequirement

// Required for absolutely core functionality across all Ciao components
// BootstrapRequirements lists required dependencies for absolutely core
// functionality across all Ciao components
var BootstrapRequirements = PackageRequirements{
"ubuntu": {
{"/usr/bin/cephfs", "ceph-fs-common"},
Expand Down Expand Up @@ -92,7 +93,7 @@ func PrepareOsDeps(reqs PackageRequirements) {

if distro == nil {
fmt.Fprintf(os.Stderr, "Running on an unsupported distro\n")
if rel := GetOsRelease(); rel != nil {
if rel := getOSRelease(); rel != nil {
fmt.Fprintf(os.Stderr, "Unsupported distro: %s %s\n", rel.Name, rel.Version)
} else {
fmt.Fprintln(os.Stderr, "No os-release found on this host")
Expand Down
30 changes: 15 additions & 15 deletions osprepare/os_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
)

type OsRelease struct {
type osRelease struct {
Name string
ID string
PrettyName string
Expand All @@ -33,10 +33,10 @@ type OsRelease struct {

// Parse the given path and attempt to return a valid
// OsRelease for it
func ParseReleaseFile(path string) *OsRelease {
func parseReleaseFile(path string) *osRelease {
fi, err := os.Open(path)
var os_rel OsRelease
os_rel.mapping = make(map[string]string)
var r osRelease
r.mapping = make(map[string]string)

if err != nil {
return nil
Expand All @@ -57,40 +57,40 @@ func ParseReleaseFile(path string) *OsRelease {
value = strings.Replace(value, "'", "", -1)

if key == "name" {
os_rel.Name = value
r.Name = value
} else if key == "id" {
os_rel.ID = value
r.ID = value
} else if key == "pretty_name" {
os_rel.PrettyName = value
r.PrettyName = value
} else if key == "version" {
os_rel.Version = value
r.Version = value
} else if key == "version_id" {
os_rel.VersionID = value
r.VersionID = value
}

// Store it for use by Distro
os_rel.mapping[key] = value
r.mapping[key] = value
}
return &os_rel
return &r
}

// Try all known paths to get the right OsRelease instance
func GetOsRelease() *OsRelease {
func getOSRelease() *osRelease {
paths := []string{
"/etc/os-release",
"/usr/lib/os-release",
"/usr/lib64/os-release",
}

for _, item := range paths {
if os_rel := ParseReleaseFile(item); os_rel != nil {
return os_rel
if r := parseReleaseFile(item); r != nil {
return r
}
}
return nil
}

func (o *OsRelease) GetValue(key string) string {
func (o *osRelease) GetValue(key string) string {
if val, succ := o.mapping[strings.ToLower(key)]; succ {
return val
}
Expand Down
14 changes: 7 additions & 7 deletions osprepare/os_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ import (
)

const (
NON_EXISTENT_FILE = "/nonexistentpath/this/file/doesnt/exists"
nonExistentFile = "/nonexistentpath/this/file/doesnt/exists"
)

func TestGetOsRelease(t *testing.T) {
d := getDistro()
if d == nil {
t.Skip("Unknown distro, cannot test")
}
os_rel := GetOsRelease()
if os_rel == nil {
r := getOSRelease()
if r == nil {
t.Fatal("Could not get os-release file for known distro")
}
if d.getID() == "clearlinux" && !strings.Contains(os_rel.ID, "clear") {
if d.getID() == "clearlinux" && !strings.Contains(r.ID, "clear") {
t.Fatal("Invalid os-release for clearlinux")
} else if d.getID() == "ubuntu" && !strings.Contains(os_rel.ID, "ubuntu") {
} else if d.getID() == "ubuntu" && !strings.Contains(r.ID, "ubuntu") {
t.Fatal("Invalid os-release for Ubuntu")
} else if d.getID() == "fedora" && !strings.Contains(os_rel.ID, "fedora") {
} else if d.getID() == "fedora" && !strings.Contains(r.ID, "fedora") {
t.Fatal("Invalid os-release for Fedora")
}
}

func TestParseReleaseFileNonExistent(t *testing.T) {
if res := ParseReleaseFile(NON_EXISTENT_FILE); res != nil {
if res := parseReleaseFile(nonExistentFile); res != nil {
t.Fatalf("Expected nil, got %v\n", res)
}
}
70 changes: 37 additions & 33 deletions osprepare/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"
)

func get_command_output(command string) string {
func getCommandOutput(command string) string {
splits := strings.Split(command, " ")
c := exec.Command(splits[0], splits[1:]...)
c.Env = os.Environ()
Expand All @@ -33,73 +33,77 @@ func get_command_output(command string) string {
c.Env = append(c.Env, "LANG=C")
c.Stderr = os.Stderr

if out, err := c.Output(); err != nil {
out, err := c.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to run %s: %s\n", splits[0], err)
return ""
} else {
return string(out)
}

return string(out)
}

func GetDockerVersion() string {
ret := get_command_output("docker --version")
func getDockerVersion() string {
ret := getCommandOutput("docker --version")
var version string

if n, _ := fmt.Sscanf(ret, "Docker version %s, build", &version); n != 1 {
return ""
} else {
if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

func GetQemuVersion() string {
ret := get_command_output("qemu-system-x86_64 --version")
func getQemuVersion() string {
ret := getCommandOutput("qemu-system-x86_64 --version")
var version string

if n, _ := fmt.Sscanf(ret, "QEMU emulator version %s, Copyright (c)", &version); n != 1 {
return ""
} else {
if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

// Determine if the given current version is less than the test version
// Note: Can only compare equal version schemas (i.e. same level of dots)
func VersionLessThan(current_version string, test_version string) bool {
cur_splits := strings.Split(current_version, ".")
test_splits := strings.Split(test_version, ".")
func versionLessThan(currentVer string, testVer string) bool {
curSplits := strings.Split(currentVer, ".")
testSplits := strings.Split(testVer, ".")

max := len(curSplits)

max_range := len(cur_splits)
if l2 := len(test_splits); l2 < max_range {
max_range = l2
if l2 := len(testSplits); l2 < max {
max = l2
}

cur_isplits := make([]int, max_range)
cur_tsplits := make([]int, max_range)
iSplits := make([]int, max)
tSplits := make([]int, max)

for i := 0; i < max_range; i++ {
cur_isplits[i], _ = strconv.Atoi(cur_splits[i])
cur_tsplits[i], _ = strconv.Atoi(test_splits[i])
for i := 0; i < max; i++ {
iSplits[i], _ = strconv.Atoi(curSplits[i])
tSplits[i], _ = strconv.Atoi(testSplits[i])
}

for i := 0; i < max_range; i++ {
for i := 0; i < max; i++ {
if i == 0 {
if cur_isplits[i] < cur_tsplits[i] {
if iSplits[i] < tSplits[i] {
return true
}
} else {
match := true
for j := 0; j < i; j++ {
if cur_isplits[j] != cur_tsplits[j] {
if iSplits[j] != tSplits[j] {
match = false
break
}
}
if match && cur_isplits[i] < cur_tsplits[i] {
if match && iSplits[i] < tSplits[i] {
return true
}
}
Expand Down
10 changes: 5 additions & 5 deletions osprepare/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGetDocker(t *testing.T) {
if pathExists("/usr/bin/docker") == false {
t.Skip("Docker not installed, cannot validate version get")
}
if vers := GetDockerVersion(); vers == "" {
if vers := getDockerVersion(); vers == "" {
t.Fatal("Cannot determine docker version")
}
}
Expand All @@ -33,7 +33,7 @@ func TestGetQemu(t *testing.T) {
if pathExists("/usr/bin/qemu-system-x86_64") == false {
t.Skip("Qemu not installed, cannot validate version get")
}
if vers := GetQemuVersion(); vers == "" {
if vers := getQemuVersion(); vers == "" {
t.Fatal("Cannot determine qemu version")
}
}
Expand All @@ -42,7 +42,7 @@ func TestGetQemu(t *testing.T) {
// false when given same version to tests. e.g: VersionLessThan("1.11.0", "1.11.0")
// this tests is expected to pass
func TestVersionLessThanEqualVersion(t *testing.T) {
if res := VersionLessThan(MinQemuVersion, MinQemuVersion); res != false {
if res := versionLessThan(MinQemuVersion, MinQemuVersion); res != false {
t.Fatalf("expected false, got %v\n", res)
}
}
Expand All @@ -51,7 +51,7 @@ func TestVersionLessThanEqualVersion(t *testing.T) {
// false when given greater version. e.g: VersionLessThan("1.11.0", "0.0.1")
// this tests is expected to pass
func TestVersionLessThanGreaterVersion(t *testing.T) {
if res := VersionLessThan(MinQemuVersion, "0.0.1"); res != false {
if res := versionLessThan(MinQemuVersion, "0.0.1"); res != false {
t.Fatalf("expected false, got %v\n", res)
}
}
Expand All @@ -60,7 +60,7 @@ func TestVersionLessThanGreaterVersion(t *testing.T) {
// true when given lower version. e.g: VersionLessThan("0.0.1", "99.9.9")
// this tests is expected to pass
func TestVersionLessThanLowerVersion(t *testing.T) {
if res := VersionLessThan("0.0.1", MinQemuVersion); res != true {
if res := versionLessThan("0.0.1", MinQemuVersion); res != true {
t.Fatalf("expected true, got %v\n", res)
}
}

0 comments on commit 77df18d

Please sign in to comment.