-
Notifications
You must be signed in to change notification settings - Fork 17
/
cnspec.go
98 lines (75 loc) · 1.83 KB
/
cnspec.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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cnspec
import (
"regexp"
)
// Version is set via ldflags
var Version string
// Build version is set via ldflags
var Build string
// Date is set via ldflags
var Date string
/*
versioning follows semver guidelines: https://semver.org/
<valid semver> ::= <version core>
| <version core> "-" <pre-release>
| <version core> "+" <build>
| <version core> "-" <pre-release> "+" <build>
<version core> ::= <major> "." <minor> "." <patch>
<major> ::= <numeric identifier>
<minor> ::= <numeric identifier>
<patch> ::= <numeric identifier>
*/
// GetVersion returns the version of the build
// valid semver version including build version (e.g. 4.10.0+4900), where 4900 is a forward rolling int
func GetVersion() string {
if Version == "" {
return "unstable"
}
return Version
}
var coreSemverRegex = regexp.MustCompile(`^(\d+.\d+.\d+)`)
// GetCoreVersion returns the semver core (i.e. major.minor.patch)
func GetCoreVersion() string {
v := Version
if v != "" {
v = coreSemverRegex.FindString(v)
}
if v == "" {
return "unstable"
}
return v
}
// GetBuild returns the git sha of the build
func GetBuild() string {
b := Build
if len(b) == 0 {
b = "development"
}
return b
}
// GetDate returns the date of this build
func GetDate() string {
d := Date
if len(d) == 0 {
d = "unknown"
}
return d
}
var majorVersionRegex = regexp.MustCompile(`^(\d+)`)
// APIVersion is the major version of the version string (e.g. 4)
func APIVersion() string {
v := Version
if v != "" {
v = majorVersionRegex.FindString(v)
}
if v == "" {
return "unstable"
}
return v
}
// Info on this application with version and build
func Info() string {
return "cnspec " + GetVersion() + " (" + GetBuild() + ", " + GetDate() + ")"
}