-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildinfo.go
91 lines (75 loc) · 2.14 KB
/
buildinfo.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
package buildinfo
import (
"fmt"
"regexp"
semver "github.com/hashicorp/go-version"
)
var version string
var repository string
var revision string
var builder string
var dateTime string
// BuildInfo structure
type BuildInfo struct {
Version string `json:"version"`
Repository string `json:"repository"`
Revision string `json:"revision"`
Builder string `json:"builder"`
DateTime string `json:"dateTime"`
}
var buildInfo BuildInfo
func init() {
parseAndConstruct()
}
func parseAndConstruct() {
if err := parseRepository(); err != nil {
repository = err.Error()
}
if err := parseRevision(); err != nil {
revision = err.Error()
}
if err := parseVersion(); err != nil {
version = err.Error()
}
if err := parseDateTime(); err != nil {
dateTime = err.Error()
}
buildInfo = BuildInfo{version, repository, revision, builder, dateTime}
}
// GetBuildInfo returns the current buildInfo as set by the ldflags
func GetBuildInfo() BuildInfo {
return buildInfo
}
// currently support https repositories
const repositorylMatch = "((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.@\\:/\\-~]+)(\\.git)?(/)?"
// currently needs to be a sha1 (ala git)
const revisionMatch = "^[0-9a-f]{5,40}$"
// variant of the iso-8601 standard (i.e. without the separators)
const dateTimeMatch = "^[0-9]{14}"
var repositoryRegex = regexp.MustCompile(repositorylMatch)
var revisionRegex = regexp.MustCompile(revisionMatch)
var dateTimeRegex = regexp.MustCompile(dateTimeMatch)
func parseRepository() error {
if !repositoryRegex.MatchString(repository) {
return fmt.Errorf("Repository %s does not match regex %s", repository, repositorylMatch)
}
return nil
}
func parseRevision() error {
if !revisionRegex.MatchString(revision) {
return fmt.Errorf("Revision %s does not match regex %s", revision, revisionMatch)
}
return nil
}
func parseVersion() error {
if _, err := semver.NewVersion(version); err != nil {
return fmt.Errorf("Version %s is not a semantic version", version)
}
return nil
}
func parseDateTime() error {
if !dateTimeRegex.MatchString(dateTime) {
return fmt.Errorf("dateTime %s does not match regex %s", dateTime, dateTimeRegex)
}
return nil
}