Merge pull request #227 from safing/maint/improve-version-info

Improve version info
This commit is contained in:
Daniel Hååvi 2024-04-10 14:01:44 +02:00 committed by GitHub
commit ae1468fea1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 44 additions and 26 deletions

View file

@ -134,7 +134,7 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
}() }()
// Add security headers. // Add security headers.
w.Header().Set("Referrer-Policy", "no-referrer") w.Header().Set("Referrer-Policy", "same-origin")
w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "deny") w.Header().Set("X-Frame-Options", "deny")
w.Header().Set("X-XSS-Protection", "1; mode=block") w.Header().Set("X-XSS-Protection", "1; mode=block")

View file

@ -14,6 +14,7 @@ var (
name string name string
version = "dev build" version = "dev build"
buildSource = "[source unknown]" buildSource = "[source unknown]"
buildTime = "[build time unknown]"
license = "[license unknown]" license = "[license unknown]"
info *Info info *Info
@ -25,19 +26,25 @@ type Info struct {
Name string Name string
Version string Version string
License string License string
Commit string
Time string
Source string Source string
BuildTime string
Commit string
CommitTime string
Dirty bool Dirty bool
debug.BuildInfo debug.BuildInfo
} }
// Set sets meta information via the main routine. This should be the first thing your program calls. // Set sets meta information via the main routine. This should be the first thing your program calls.
func Set(setName string, setVersion string, setLicenseName string, compareVersionToTag bool) { func Set(setName string, setVersion string, setLicenseName string) {
name = setName name = setName
version = setVersion
license = setLicenseName license = setLicenseName
if setVersion != "" {
version = setVersion
}
} }
// GetInfo returns all the meta information about the program. // GetInfo returns all the meta information about the program.
@ -53,11 +60,19 @@ func GetInfo() *Info {
Name: name, Name: name,
Version: version, Version: version,
License: license, License: license,
BuildInfo: *buildInfo,
Source: buildSource, Source: buildSource,
BuildTime: buildTime,
Commit: buildSettings["vcs.revision"], Commit: buildSettings["vcs.revision"],
Time: buildSettings["vcs.time"], CommitTime: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true", Dirty: buildSettings["vcs.modified"] == "true",
BuildInfo: *buildInfo,
}
if info.Commit == "" {
info.Commit = "[commit unknown]"
}
if info.CommitTime == "" {
info.CommitTime = "[commit time unknown]"
} }
}) })
@ -78,14 +93,21 @@ func Version() string {
// FullVersion returns the full and detailed version string. // FullVersion returns the full and detailed version string.
func FullVersion() string { func FullVersion() string {
info := GetInfo() info := GetInfo()
builder := new(strings.Builder) builder := new(strings.Builder)
builder.WriteString(fmt.Sprintf("%s\nversion %s\n", info.Name, Version())) // Name and version.
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version()))
// Build info.
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime))
// Commit info.
builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit)) builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit))
builder.WriteString(fmt.Sprintf("built with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)) builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
builder.WriteString(fmt.Sprintf(" on %s\n", info.Time)) builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, info.Source))
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.", license))
return builder.String() return builder.String()
} }
@ -102,10 +124,6 @@ func CheckVersion() error {
if name == "[NAME]" || license == "[license unknown]" { if name == "[NAME]" || license == "[license unknown]" {
return errors.New("must call SetInfo() before calling CheckVersion()") return errors.New("must call SetInfo() before calling CheckVersion()")
} }
if version == "[version unknown]" {
return errors.New("please build using the supplied build script.\n$ ./build {main.go|...}")
}
} }
return nil return nil

View file

@ -17,7 +17,7 @@ func registerInfoMetric() error {
map[string]string{ map[string]string{
"version": checkUnknown(meta.Version), "version": checkUnknown(meta.Version),
"commit": checkUnknown(meta.Commit), "commit": checkUnknown(meta.Commit),
"build_date": checkUnknown(meta.Time), "build_date": checkUnknown(meta.BuildTime),
"build_source": checkUnknown(meta.Source), "build_source": checkUnknown(meta.Source),
"go_os": runtime.GOOS, "go_os": runtime.GOOS,
"go_arch": runtime.GOARCH, "go_arch": runtime.GOARCH,

View file

@ -10,7 +10,7 @@ import (
func main() { func main() {
// Set Info // Set Info
info.Set("Portbase", "0.0.1", "GPLv3", false) info.Set("Portbase", "0.0.1", "GPLv3")
// Run // Run
os.Exit(run.Run()) os.Exit(run.Run())