Skip to content

Commit df2ac7a

Browse files
authored
gui, lib/api: Add possibility to feed through extra version information (syncthing#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.
1 parent b96b239 commit df2ac7a

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

gui/default/syncthing/core/syncthingController.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,11 @@ angular.module('syncthing.core')
30513051
arch += " Container";
30523052
}
30533053

3054-
return $scope.version.version + ', ' + os + ' (' + arch + ')';
3054+
var verStr = $scope.version.version;
3055+
if ($scope.version.extra) {
3056+
verStr += ' (' + $scope.version.extra + ')';
3057+
}
3058+
return verStr + ', ' + os + ' (' + arch + ')';
30553059
};
30563060

30573061
$scope.versionBase = function () {

lib/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ func (*service) getSystemVersion(w http.ResponseWriter, _ *http.Request) {
713713
"version": build.Version,
714714
"codename": build.Codename,
715715
"longVersion": build.LongVersion,
716+
"extra": build.Extra,
716717
"os": runtime.GOOS,
717718
"arch": runtime.GOARCH,
718719
"isBeta": build.IsBeta,

lib/build/build.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
IsCandidate bool
3636
IsBeta bool
3737
LongVersion string
38+
Extra string
3839

3940
allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`)
4041

@@ -46,6 +47,8 @@ var (
4647
}
4748
)
4849

50+
const versionExtraAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. "
51+
4952
func init() {
5053
if Version != "unknown-dev" {
5154
// If not a generic dev build, version string should come from git describe
@@ -75,6 +78,7 @@ func setBuildData() {
7578
IsRelease = exp.MatchString(Version)
7679
IsCandidate = strings.Contains(Version, "-rc.")
7780
IsBeta = strings.Contains(Version, "-")
81+
Extra = filterString(os.Getenv("STVERSIONEXTRA"), versionExtraAllowedChars)
7882

7983
stamp, _ := strconv.Atoi(Stamp)
8084
Date = time.Unix(int64(stamp), 0)
@@ -103,7 +107,22 @@ func TagsList() []string {
103107
tags = append(tags, strings.ToLower(envVar))
104108
}
105109
}
110+
if Extra != "" {
111+
tags = append(tags, Extra)
112+
}
106113

107114
sort.Strings(tags)
108115
return tags
109116
}
117+
118+
// filterString returns a copy of s with all characters not in allowedChars
119+
// removed.
120+
func filterString(s, allowedChars string) string {
121+
var res strings.Builder
122+
for _, c := range s {
123+
if strings.ContainsRune(allowedChars, c) {
124+
res.WriteRune(c)
125+
}
126+
}
127+
return res.String()
128+
}

lib/build/build_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,23 @@ func TestAllowedVersions(t *testing.T) {
3535
}
3636
}
3737
}
38+
39+
func TestFilterString(t *testing.T) {
40+
cases := []struct {
41+
input string
42+
filter string
43+
output string
44+
}{
45+
{"abcba", "abc", "abcba"},
46+
{"abcba", "ab", "abba"},
47+
{"abcba", "c", "c"},
48+
{"abcba", "!", ""},
49+
{"Foo (v1.5)", versionExtraAllowedChars, "Foo v1.5"},
50+
}
51+
52+
for i, c := range cases {
53+
if out := filterString(c.input, c.filter); out != c.output {
54+
t.Errorf("%d: %q != %q", i, out, c.output)
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)