2020-10-12 23:45:44 +08:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime/debug"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-10-13 00:55:10 +08:00
|
|
|
// NOTE: main() updates these.
|
2020-10-12 23:45:44 +08:00
|
|
|
var (
|
|
|
|
SHA = ""
|
2020-10-19 02:04:50 +08:00
|
|
|
Semver = ""
|
2020-10-12 23:45:44 +08:00
|
|
|
BuildTime = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
var versionCache string
|
|
|
|
|
2020-10-19 02:04:50 +08:00
|
|
|
// Banner returns the version banner.
|
|
|
|
func Banner() string {
|
2020-10-12 23:45:44 +08:00
|
|
|
if versionCache != "" {
|
|
|
|
return versionCache
|
|
|
|
}
|
|
|
|
|
|
|
|
var version string
|
|
|
|
if SHA != "" {
|
2020-10-19 02:04:50 +08:00
|
|
|
version = fmt.Sprintf("%s (%s)", Semver, SHA)
|
2020-10-12 23:45:44 +08:00
|
|
|
} else {
|
2020-10-19 02:04:50 +08:00
|
|
|
version = fmt.Sprintf("%s-dev", Semver) // no SHA. '0.x.y-dev' indicates it is run from source without build script.
|
2020-10-12 23:45:44 +08:00
|
|
|
}
|
|
|
|
if info, ok := debug.ReadBuildInfo(); !ok && info == nil {
|
|
|
|
version += " (non-modules)"
|
|
|
|
}
|
|
|
|
if BuildTime != "" {
|
|
|
|
i, err := strconv.ParseInt(BuildTime, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
tm := time.Unix(i, 0)
|
|
|
|
version += fmt.Sprintf(" built %s", tm.Format(time.RFC822))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
versionCache = version
|
|
|
|
return version
|
|
|
|
}
|