Refactor getVersion(), address code review comments

This commit is contained in:
Alexander Neumann 2015-08-26 20:13:00 +02:00
parent f3c64d0740
commit 39a82d951b

View file

@ -200,35 +200,41 @@ func test(gopath string, args ...string) error {
return cmd.Run() return cmd.Run()
} }
// getVersion returns the version string from the file VERSION in the current
// directory.
func getVersionFromFile() string {
buf, err := ioutil.ReadFile("VERSION")
if err != nil {
verbosePrintf("error reading file VERSION: %v\n", err)
return ""
}
return strings.TrimSpace(string(buf))
}
// getVersion returns a version string which is a combination of the contents // getVersion returns a version string which is a combination of the contents
// of the file VERSION in the current directory and the version from git (if // of the file VERSION in the current directory and the version from git (if
// available). // available).
func getVersion() string { func getVersion() string {
buf, err := ioutil.ReadFile("VERSION") versionFile := getVersionFromFile()
if err != nil { versionGit := getVersionFromGit()
verbosePrintf("error reading file VERSION: %v\n", err)
}
versionFile := strings.TrimSpace(string(buf))
verbosePrintf("version from file 'VERSION' is %q\n", versionFile)
versionGit := gitVersion() verbosePrintf("version from file 'VERSION' is %q, version from git %q\n",
verbosePrintf("version from file 'VERSION' is %q\n", versionGit) versionFile, versionGit)
if versionFile == "" && versionGit == "" { switch {
return "unknown" case versionFile == "":
return versionGit
case versionGit == "":
return versionFile
} }
version := versionFile return fmt.Sprintf("%s (%s)", versionFile, versionGit)
if versionGit != "" {
version += " (" + versionGit + ")"
}
return version
} }
// gitVersion returns a version string that identifies the currently checked // getVersionFromGit returns a version string that identifies the currently
// out git commit. // checked out git commit.
func gitVersion() string { func getVersionFromGit() string {
cmd := exec.Command("git", "describe", cmd := exec.Command("git", "describe",
"--long", "--tags", "--dirty", "--always") "--long", "--tags", "--dirty", "--always")
out, err := cmd.Output() out, err := cmd.Output()