mycorrhiza/version/version.go
Umar Getagazov 8850b50624 Get version from the help
I'm going to get executed for this crime
2022-09-14 03:18:02 +03:00

45 lines
866 B
Go

package version
import (
"regexp"
"runtime/debug"
"strconv"
"github.com/bouncepaw/mycorrhiza/help"
)
var tag = "unknown"
var versionRegexp = regexp.MustCompile(`This is documentation for \*\*Mycorrhiza Wiki\*\* (.*).`)
func init() {
if b, err := help.Get("en"); err == nil {
matches := versionRegexp.FindSubmatch(b)
if matches != nil {
tag = "v" + string(matches[1])
}
}
}
func FormatVersion() string {
var commit, dirty string
info, ok := debug.ReadBuildInfo()
if ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
commit = "+" + setting.Value
if len(commit) > 8 {
commit = commit[:8]
}
} else if setting.Key == "vcs.modified" {
modified, err := strconv.ParseBool(setting.Value)
if err == nil && modified {
dirty = "-dirty"
}
}
}
}
return tag + commit + dirty
}