- GET /version (signed-in users only) answers the version and, when the binary knows it, the commit it was built from and whether the files had uncommitted changes - the commit is read from the build info Go records in a git checkout, so no build flag is needed - the side menu shows the version under the privacy line, and Settings has an About section with it - the menu's own requests (friends, version) are not counted as requests of an admin page in the Shell tests
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"runtime/debug"
|
|
|
|
httpapi "codeberg.org/squidd/sonda/internal/api/http"
|
|
)
|
|
|
|
// shortRevision is how many characters of the commit the app shows.
|
|
const shortRevision = 7
|
|
|
|
// revision reads the commit the binary was built from, and whether the files had changes that were not committed.
|
|
// Go records both in a build made inside a git checkout; a build without them (a copy of the sources, or
|
|
// -buildvcs=false) has no revision.
|
|
func revision(info *debug.BuildInfo) (rev string, modified bool) {
|
|
if info == nil {
|
|
return "", false
|
|
}
|
|
for _, s := range info.Settings {
|
|
switch s.Key {
|
|
case "vcs.revision":
|
|
rev = s.Value
|
|
if len(rev) > shortRevision {
|
|
rev = rev[:shortRevision]
|
|
}
|
|
case "vcs.modified":
|
|
modified = s.Value == "true"
|
|
}
|
|
}
|
|
if rev == "" {
|
|
modified = false
|
|
}
|
|
return rev, modified
|
|
}
|
|
|
|
// build says which Sonda this is, for the signed-in users to see.
|
|
func build() httpapi.Build {
|
|
info, _ := debug.ReadBuildInfo()
|
|
rev, modified := revision(info)
|
|
return httpapi.Build{Version: version, Revision: rev, Modified: modified}
|
|
}
|