buildinfo: compile time variables

These expose entrypoints for -ldflag injections at compile time,
containing metadata about build env + SCM
This commit is contained in:
Paul Stemmet 2022-12-09 12:38:58 +00:00
parent 26485ab1e8
commit 16ab40f44d
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
1 changed files with 46 additions and 0 deletions

46
buildinfo/build.go Normal file
View File

@ -0,0 +1,46 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package build
import (
"strings"
"golang.org/x/exp/slices"
)
var (
// Git tag of this binary
Version = "v0.0.0+devel"
// Git hash of this binary
CommitHash = "0000000000000000000000000000000000000000"
// Enabled features of this binary
Features = ""
knownFeatures = map[string]bool{}
)
// Get the short Git hash
func ShortHash() string {
return CommitHash[:7]
}
// Get list of features enabled
func FeaturesList() []string {
out := []string{}
active := strings.Split(Features, ",")
for feature, _ := range knownFeatures {
sy := "-"
if slices.Contains(active, feature) {
sy = "+"
}
out = append(out, sy+feature)
}
slices.Sort(out)
return out
}