gui, lib/api: Add possibility to feed through extra version information (#8980)

This adds an environment variable STVERSIONEXTRA that, when set, gets
added to the version information in the API and GUI.

The purpose of all this is to be able to communicate something about the
bundling or packaging, through the log & GUI and the end user, to the
potential person supporting it -- i.e., us. :) A wrapper can set this
variable to indicate that Syncthing is being run via `SyncTrayzor`,
`Syncthing-macOS`, etc., and thus indicate to the end user that the GUI
they are looking at is perhaps not the only source of truth and
management for this instance.
This commit is contained in:
Jakob Borg 2023-07-16 17:43:10 +02:00 committed by GitHub
parent b96b23957b
commit df2ac7aaeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 1 deletions

View File

@ -3051,7 +3051,11 @@ angular.module('syncthing.core')
arch += " Container";
}
return $scope.version.version + ', ' + os + ' (' + arch + ')';
var verStr = $scope.version.version;
if ($scope.version.extra) {
verStr += ' (' + $scope.version.extra + ')';
}
return verStr + ', ' + os + ' (' + arch + ')';
};
$scope.versionBase = function () {

View File

@ -713,6 +713,7 @@ func (*service) getSystemVersion(w http.ResponseWriter, _ *http.Request) {
"version": build.Version,
"codename": build.Codename,
"longVersion": build.LongVersion,
"extra": build.Extra,
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"isBeta": build.IsBeta,

View File

@ -35,6 +35,7 @@ var (
IsCandidate bool
IsBeta bool
LongVersion string
Extra string
allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`)
@ -46,6 +47,8 @@ var (
}
)
const versionExtraAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. "
func init() {
if Version != "unknown-dev" {
// If not a generic dev build, version string should come from git describe
@ -75,6 +78,7 @@ func setBuildData() {
IsRelease = exp.MatchString(Version)
IsCandidate = strings.Contains(Version, "-rc.")
IsBeta = strings.Contains(Version, "-")
Extra = filterString(os.Getenv("STVERSIONEXTRA"), versionExtraAllowedChars)
stamp, _ := strconv.Atoi(Stamp)
Date = time.Unix(int64(stamp), 0)
@ -103,7 +107,22 @@ func TagsList() []string {
tags = append(tags, strings.ToLower(envVar))
}
}
if Extra != "" {
tags = append(tags, Extra)
}
sort.Strings(tags)
return tags
}
// filterString returns a copy of s with all characters not in allowedChars
// removed.
func filterString(s, allowedChars string) string {
var res strings.Builder
for _, c := range s {
if strings.ContainsRune(allowedChars, c) {
res.WriteRune(c)
}
}
return res.String()
}

View File

@ -35,3 +35,23 @@ func TestAllowedVersions(t *testing.T) {
}
}
}
func TestFilterString(t *testing.T) {
cases := []struct {
input string
filter string
output string
}{
{"abcba", "abc", "abcba"},
{"abcba", "ab", "abba"},
{"abcba", "c", "c"},
{"abcba", "!", ""},
{"Foo (v1.5)", versionExtraAllowedChars, "Foo v1.5"},
}
for i, c := range cases {
if out := filterString(c.input, c.filter); out != c.output {
t.Errorf("%d: %q != %q", i, out, c.output)
}
}
}