From ab848247c985fe861617cb664b67d49cf2ef4d0d Mon Sep 17 00:00:00 2001 From: Umar Getagazov Date: Wed, 14 Sep 2022 10:38:36 +0300 Subject: [PATCH] Change version package API --- flag.go | 2 +- main.go | 2 +- misc/about.go | 2 +- version/version.go | 26 ++++++++++++++------------ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/flag.go b/flag.go index a6901c9..8253b84 100644 --- a/flag.go +++ b/flag.go @@ -42,7 +42,7 @@ func parseCliArgs() { flag.Parse() if versionFlag { - fmt.Println("Mycorrhiza Wiki", version.FormatVersion()) + fmt.Println("Mycorrhiza Wiki", version.Long) os.Exit(0) } diff --git a/main.go b/main.go index 5fcf83f..f098052 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ func main() { log.Fatal(err) } - log.Println("Running Mycorrhiza Wiki", version.FormatVersion()) + log.Println("Running Mycorrhiza Wiki", version.Short) if err := os.Chdir(files.HyphaeDir()); err != nil { log.Fatal(err) } diff --git a/misc/about.go b/misc/about.go index 35b80f9..cd348f6 100644 --- a/misc/about.go +++ b/misc/about.go @@ -98,7 +98,7 @@ func AboutHTML(lc *l18n.Localizer) string { log.Fatalln(err) } data := aboutData - data.Version = version.FormatVersion() + data.Version = version.Short data.Admins = user.ListUsersWithGroup("admin") data.UserCount = user.Count() data.RegistrationLimit = cfg.RegistrationLimit diff --git a/version/version.go b/version/version.go index 84dcd39..4276d58 100644 --- a/version/version.go +++ b/version/version.go @@ -8,37 +8,39 @@ import ( "github.com/bouncepaw/mycorrhiza/help" ) -var tag = "unknown" +// Long is the full version string, including VCS information, that looks like +// x.y.z+hash-dirty. +var Long string + +// Short is the human-friendly x.y.z part of the long version string. +var Short string + 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]) + Short = string(matches[1]) } } -} -func FormatVersion() string { - var commit, dirty string + Long = Short 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] + val := setting.Value + if len(val) > 7 { + val = val[:7] } + Long += "+" + val } else if setting.Key == "vcs.modified" { modified, err := strconv.ParseBool(setting.Value) if err == nil && modified { - dirty = "-dirty" + Long += "-dirty" } } } } - - return tag + commit + dirty }