feat: show the version of Sonda
- 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
This commit is contained in:
parent
69c85a7b52
commit
00e5254fde
3 changed files with 337 additions and 10 deletions
41
cmd/sondad/buildinfo.go
Normal file
41
cmd/sondad/buildinfo.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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}
|
||||
}
|
||||
43
cmd/sondad/buildinfo_test.go
Normal file
43
cmd/sondad/buildinfo_test.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func settings(kv ...string) *debug.BuildInfo {
|
||||
b := &debug.BuildInfo{}
|
||||
for i := 0; i+1 < len(kv); i += 2 {
|
||||
b.Settings = append(b.Settings, debug.BuildSetting{Key: kv[i], Value: kv[i+1]})
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func TestRevisionIsTheShortCommit(t *testing.T) {
|
||||
rev, modified := revision(settings("vcs", "git", "vcs.revision", "69c85a7e1d0f4b2c8a9e3f5d6b7a8c9d0e1f2a3b", "vcs.modified", "false"))
|
||||
if rev != "69c85a7" || modified {
|
||||
t.Errorf("got %q, %v", rev, modified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevisionSaysWhenTheTreeWasChanged(t *testing.T) {
|
||||
rev, modified := revision(settings("vcs.revision", "69c85a7e1d0f", "vcs.modified", "true"))
|
||||
if rev != "69c85a7" || !modified {
|
||||
t.Errorf("got %q, %v", rev, modified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevisionIsEmptyWithoutVersionControlInfo(t *testing.T) {
|
||||
if rev, modified := revision(settings()); rev != "" || modified {
|
||||
t.Errorf("got %q, %v", rev, modified)
|
||||
}
|
||||
if rev, modified := revision(nil); rev != "" || modified {
|
||||
t.Errorf("nil: got %q, %v", rev, modified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortRevisionIsKeptWhole(t *testing.T) {
|
||||
if rev, _ := revision(settings("vcs.revision", "abc")); rev != "abc" {
|
||||
t.Errorf("got %q", rev)
|
||||
}
|
||||
}
|
||||
|
|
@ -299,6 +299,7 @@ func run() error {
|
|||
|
||||
router := httpapi.NewRouter(httpapi.Deps{
|
||||
Logger: logger,
|
||||
Build: build(),
|
||||
Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()),
|
||||
Tokens: tokens,
|
||||
Listening: listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()),
|
||||
|
|
|
|||
Loading…
Reference in a new issue