Skip to content

Commit ae1468f

Browse files
authored
Merge pull request #227 from safing/maint/improve-version-info
Improve version info
2 parents a90357b + e7611f0 commit ae1468f

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

api/router.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
134134
}()
135135

136136
// Add security headers.
137-
w.Header().Set("Referrer-Policy", "no-referrer")
137+
w.Header().Set("Referrer-Policy", "same-origin")
138138
w.Header().Set("X-Content-Type-Options", "nosniff")
139139
w.Header().Set("X-Frame-Options", "deny")
140140
w.Header().Set("X-XSS-Protection", "1; mode=block")

info/version.go

+41-23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var (
1414
name string
1515
version = "dev build"
1616
buildSource = "[source unknown]"
17+
buildTime = "[build time unknown]"
1718
license = "[license unknown]"
1819

1920
info *Info
@@ -25,19 +26,25 @@ type Info struct {
2526
Name string
2627
Version string
2728
License string
28-
Commit string
29-
Time string
30-
Source string
31-
Dirty bool
29+
30+
Source string
31+
BuildTime string
32+
33+
Commit string
34+
CommitTime string
35+
Dirty bool
3236

3337
debug.BuildInfo
3438
}
3539

3640
// Set sets meta information via the main routine. This should be the first thing your program calls.
37-
func Set(setName string, setVersion string, setLicenseName string, compareVersionToTag bool) {
41+
func Set(setName string, setVersion string, setLicenseName string) {
3842
name = setName
39-
version = setVersion
4043
license = setLicenseName
44+
45+
if setVersion != "" {
46+
version = setVersion
47+
}
4148
}
4249

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

5259
info = &Info{
53-
Name: name,
54-
Version: version,
55-
License: license,
56-
BuildInfo: *buildInfo,
57-
Source: buildSource,
58-
Commit: buildSettings["vcs.revision"],
59-
Time: buildSettings["vcs.time"],
60-
Dirty: buildSettings["vcs.modified"] == "true",
60+
Name: name,
61+
Version: version,
62+
License: license,
63+
Source: buildSource,
64+
BuildTime: buildTime,
65+
Commit: buildSettings["vcs.revision"],
66+
CommitTime: buildSettings["vcs.time"],
67+
Dirty: buildSettings["vcs.modified"] == "true",
68+
BuildInfo: *buildInfo,
69+
}
70+
71+
if info.Commit == "" {
72+
info.Commit = "[commit unknown]"
73+
}
74+
if info.CommitTime == "" {
75+
info.CommitTime = "[commit time unknown]"
6176
}
6277
})
6378

@@ -78,14 +93,21 @@ func Version() string {
7893
// FullVersion returns the full and detailed version string.
7994
func FullVersion() string {
8095
info := GetInfo()
81-
8296
builder := new(strings.Builder)
8397

84-
builder.WriteString(fmt.Sprintf("%s\nversion %s\n", info.Name, Version()))
98+
// Name and version.
99+
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version()))
100+
101+
// Build info.
102+
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
103+
builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime))
104+
105+
// Commit info.
85106
builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit))
86-
builder.WriteString(fmt.Sprintf("built with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
87-
builder.WriteString(fmt.Sprintf(" on %s\n", info.Time))
88-
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, info.Source))
107+
builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
108+
builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))
109+
110+
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.", license))
89111

90112
return builder.String()
91113
}
@@ -102,10 +124,6 @@ func CheckVersion() error {
102124
if name == "[NAME]" || license == "[license unknown]" {
103125
return errors.New("must call SetInfo() before calling CheckVersion()")
104126
}
105-
106-
if version == "[version unknown]" {
107-
return errors.New("please build using the supplied build script.\n$ ./build {main.go|...}")
108-
}
109127
}
110128

111129
return nil

metrics/metrics_info.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func registerInfoMetric() error {
1717
map[string]string{
1818
"version": checkUnknown(meta.Version),
1919
"commit": checkUnknown(meta.Commit),
20-
"build_date": checkUnknown(meta.Time),
20+
"build_date": checkUnknown(meta.BuildTime),
2121
"build_source": checkUnknown(meta.Source),
2222
"go_os": runtime.GOOS,
2323
"go_arch": runtime.GOARCH,

portbase.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func main() {
1212
// Set Info
13-
info.Set("Portbase", "0.0.1", "GPLv3", false)
13+
info.Set("Portbase", "0.0.1", "GPLv3")
1414

1515
// Run
1616
os.Exit(run.Run())

0 commit comments

Comments
 (0)