2020-04-29 21:32:39 +00:00
|
|
|
// Package version executes and returns the version string
|
|
|
|
// for the currently running process.
|
2019-01-10 04:19:33 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-01 05:47:30 +00:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
2021-04-30 20:37:38 +00:00
|
|
|
"strconv"
|
2019-11-01 14:48:24 +00:00
|
|
|
"strings"
|
2019-11-01 05:47:30 +00:00
|
|
|
"time"
|
2019-01-10 04:19:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The value of these vars are set through linker options.
|
|
|
|
var gitCommit = "Local build"
|
|
|
|
var buildDate = "Moments ago"
|
2021-04-30 20:37:38 +00:00
|
|
|
var buildDateUnix = "0"
|
2020-06-11 05:45:31 +00:00
|
|
|
var gitTag = "Unknown"
|
2019-01-10 04:19:33 +00:00
|
|
|
|
2021-01-25 21:27:30 +00:00
|
|
|
// Version returns the version string of this build.
|
|
|
|
func Version() string {
|
2020-06-11 15:34:34 +00:00
|
|
|
if buildDate == "{DATE}" {
|
|
|
|
now := time.Now().Format(time.RFC3339)
|
|
|
|
buildDate = now
|
|
|
|
}
|
2021-04-30 20:37:38 +00:00
|
|
|
if buildDateUnix == "{DATE_UNIX}" {
|
|
|
|
buildDateUnix = strconv.Itoa(int(time.Now().Unix()))
|
|
|
|
}
|
2021-01-25 21:27:30 +00:00
|
|
|
return fmt.Sprintf("%s. Built at: %s", BuildData(), buildDate)
|
2020-06-11 15:34:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 21:27:30 +00:00
|
|
|
// SemanticVersion returns the Major.Minor.Patch version of this build.
|
|
|
|
func SemanticVersion() string {
|
2021-01-05 21:06:51 +00:00
|
|
|
return gitTag
|
|
|
|
}
|
|
|
|
|
2021-01-25 21:27:30 +00:00
|
|
|
// BuildData returns the git tag and commit of the current build.
|
|
|
|
func BuildData() string {
|
2019-11-01 05:47:30 +00:00
|
|
|
// if doing a local build, these values are not interpolated
|
|
|
|
if gitCommit == "{STABLE_GIT_COMMIT}" {
|
|
|
|
commit, err := exec.Command("git", "rev-parse", "HEAD").Output()
|
|
|
|
if err != nil {
|
2019-11-06 18:41:06 +00:00
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
gitCommit = strings.TrimRight(string(commit), "\r\n")
|
2019-11-01 05:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-11 15:34:34 +00:00
|
|
|
return fmt.Sprintf("Prysm/%s/%s", gitTag, gitCommit)
|
2019-01-10 04:19:33 +00:00
|
|
|
}
|