2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

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