Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve version info #227

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
}()

// 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-Frame-Options", "deny")
w.Header().Set("X-XSS-Protection", "1; mode=block")
Expand Down
64 changes: 41 additions & 23 deletions info/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
name string
version = "dev build"
buildSource = "[source unknown]"
buildTime = "[build time unknown]"
license = "[license unknown]"

info *Info
Expand All @@ -25,19 +26,25 @@ type Info struct {
Name string
Version string
License string
Commit string
Time string
Source string
Dirty bool

Source string
BuildTime string

Commit string
CommitTime string
Dirty bool

debug.BuildInfo
}

// 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
version = setVersion
license = setLicenseName

if setVersion != "" {
version = setVersion
}
}

// GetInfo returns all the meta information about the program.
Expand All @@ -50,14 +57,22 @@ func GetInfo() *Info {
}

info = &Info{
Name: name,
Version: version,
License: license,
BuildInfo: *buildInfo,
Source: buildSource,
Commit: buildSettings["vcs.revision"],
Time: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true",
Name: name,
Version: version,
License: license,
Source: buildSource,
BuildTime: buildTime,
Commit: buildSettings["vcs.revision"],
CommitTime: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true",
BuildInfo: *buildInfo,
}

if info.Commit == "" {
info.Commit = "[commit unknown]"
}
if info.CommitTime == "" {
info.CommitTime = "[commit time unknown]"
}
})

Expand All @@ -78,14 +93,21 @@ func Version() string {
// FullVersion returns the full and detailed version string.
func FullVersion() string {
info := GetInfo()

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("built with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
builder.WriteString(fmt.Sprintf(" on %s\n", info.Time))
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, info.Source))
builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))

builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.", license))

return builder.String()
}
Expand All @@ -102,10 +124,6 @@ func CheckVersion() error {
if name == "[NAME]" || license == "[license unknown]" {
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
Expand Down
2 changes: 1 addition & 1 deletion metrics/metrics_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func registerInfoMetric() error {
map[string]string{
"version": checkUnknown(meta.Version),
"commit": checkUnknown(meta.Commit),
"build_date": checkUnknown(meta.Time),
"build_date": checkUnknown(meta.BuildTime),
"build_source": checkUnknown(meta.Source),
"go_os": runtime.GOOS,
"go_arch": runtime.GOARCH,
Expand Down
Loading