generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathversion.go
51 lines (42 loc) · 1.41 KB
/
version.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
package ftl
import (
"fmt"
"regexp"
"strconv"
"time"
"github.com/alecthomas/types/must"
"golang.org/x/mod/semver"
)
// IsRelease returns true if the version is a release version.
func IsRelease(v string) bool {
return regexp.MustCompile(`^\d+\.\d+\.\d+(-\d+-g[0-9a-f]+)?$`).MatchString(v)
}
// BaseVersion returns the version without any SHA suffix.
// For example, "0.423.0-5-g43798ff01" becomes "0.423.0"
func BaseVersion(v string) string {
if idx := regexp.MustCompile(`-\d+-g[0-9a-f]+$`).FindStringIndex(v); idx != nil {
return v[:idx[0]]
}
return v
}
// IsVersionAtLeastMin returns true if any of the following are true:
// - minVersion is not defined (i.e. is emptystring)
// - v or minVersion is not a release version
// - v > minVersion when both v and minVersion are release versions
func IsVersionAtLeastMin(v string, minVersion string) bool {
if minVersion == "" {
return true
}
if !IsRelease(v) || !IsRelease(minVersion) {
return true
}
return semver.Compare("v"+v, "v"+minVersion) >= 0
}
// Version of FTL binary (set by linker).
var Version = "dev"
// FormattedVersion includes the version and timestamp.
var FormattedVersion = fmt.Sprintf("%s (%s)", Version, Timestamp.Format("2006-01-02"))
// Timestamp of FTL binary in seconds since epoch (set by linker).
var timestamp = "0"
// Timestamp parsed from "timestamp".
var Timestamp = time.Unix(must.Get(strconv.ParseInt(timestamp, 0, 64)), 0)