lib/build: Add some env vars as synthetic build tags (#6709)

This adds some env vars to the long version string as if they were build
tags. The purpose is to better understand what code was running or not
in the version output, usage reporting and crash reports. In order to
prevent possible privacy issues the actual value of the variable is not
reported, just the fact that it was set to something non-empty.

Example:

	% ./bin/syncthing --version
	syncthing v1.6.1+47-g1eb104f3-buildtags "Fermium Flea" (go1.14.3 darwin-amd64) jb@kvin.kastelo.net 2020-06-03 07:25:46 UTC [stnoupgrade, use_badger]
This commit is contained in:
Jakob Borg 2020-06-03 14:00:28 +01:00 committed by GitHub
parent 98418c9b5c
commit 4f367e4376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ package build
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"regexp" "regexp"
"runtime" "runtime"
"strconv" "strconv"
@ -37,6 +38,14 @@ var (
Tags []string Tags []string
allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`) allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`)
envTags = []string{
"STGUIASSETS",
"STHASHING",
"STNORESTART",
"STNOUPGRADE",
"USE_BADGER",
}
) )
func init() { func init() {
@ -79,6 +88,11 @@ func LongVersionFor(program string) string {
// This string and date format is essentially part of our external API. Never change it. // This string and date format is essentially part of our external API. Never change it.
date := Date.UTC().Format("2006-01-02 15:04:05 MST") date := Date.UTC().Format("2006-01-02 15:04:05 MST")
v := fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date) v := fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date)
for _, envVar := range envTags {
if os.Getenv(envVar) != "" {
Tags = append(Tags, strings.ToLower(envVar))
}
}
if len(Tags) > 0 { if len(Tags) > 0 {
v = fmt.Sprintf("%s [%s]", v, strings.Join(Tags, ", ")) v = fmt.Sprintf("%s [%s]", v, strings.Join(Tags, ", "))
} }