-
Notifications
You must be signed in to change notification settings - Fork 118
/
release.go
102 lines (87 loc) · 2.35 KB
/
release.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Package version provides helpers for working with versions and build info.
package version
import (
"encoding/json"
"strconv"
"strings"
"time"
)
// https://goreleaser.com/templates/
var (
// Component name, e.g. "pmm-managed" or "mongodb_exporter".
// {{ .ProjectName }} for GoReleaser.
ProjectName string
// Component version, e.g. "2.1.2-beta1" for pmm-managed or "0.6.3" for mongodb_exporter.
// {{ .Version }} for GoReleaser.
Version string
// PMM version. Empty for non-PMM builds.
// For example, "2.1.2-beta1" for mongodb_exporter when built with PMM Client, empty otherwise.
PMMVersion string
// Build UNIX timestamp, e.g. "1545226908".
// {{ .Timestamp }} for GoReleaser.
Timestamp string
// Full git commit hash, e.g. "6559a94ab33831deeda04193f74413b735edb1a1".
// {{ .FullCommit }} for GoReleaser.
FullCommit string
// Git branch name, e.g. "master".
Branch string
)
// ShortInfo returns project name and short version as one line.
func ShortInfo() string {
if ProjectName == "" {
return ""
}
res := ProjectName + " v" + Version
if PMMVersion != "" && PMMVersion != Version {
res += " (PMM v" + PMMVersion + ")"
}
return res
}
// Time returns parsed Timestamp.
func Time() (time.Time, error) {
sec, err := strconv.ParseInt(Timestamp, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(sec, 0).UTC(), nil
}
// timestampFormatted returns timestamp in format "YYYY-MM-DD HH:mm:ss (UTC)"
func timestampFormatted() string {
timestamp := Timestamp
if t, err := Time(); err == nil {
timestamp = t.Format("2006-01-02 15:04:05 (UTC)")
}
return timestamp
}
// FullInfo returns multi-line version information.
func FullInfo() string {
res := []string{
"ProjectName: " + ProjectName,
"Version: " + Version,
"PMMVersion: " + PMMVersion,
"Timestamp: " + timestampFormatted(),
"FullCommit: " + FullCommit,
}
if Branch != "" {
res = append(res, "Branch: "+Branch)
}
return strings.Join(res, "\n")
}
// FullInfoJson returns version information in JSON format.
func FullInfoJson() string {
resMap := map[string]string{
"ProjectName": ProjectName,
"Version": Version,
"PMMVersion": PMMVersion,
"Timestamp": timestampFormatted(),
"FullCommit": FullCommit,
}
if Branch != "" {
resMap["Branch"] = Branch
}
resJson, err := json.Marshal(resMap)
if err != nil {
panic(err)
}
return string(resJson)
}