-
Notifications
You must be signed in to change notification settings - Fork 43
/
version.go
69 lines (62 loc) · 1.2 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Package version resolves current module version.
package version
import (
"runtime/debug"
"strings"
"sync"
"github.com/hashicorp/go-version"
)
var once struct {
version Value
sync.Once
}
// Value describes client module version
type Value struct {
Major int
Minor int
Patch int
Name string
Raw string
}
// Extract version Value from BuildInfo.
func Extract(info *debug.BuildInfo) Value {
var raw string
const pkg = "github.com/ClickHouse/ch-go"
if strings.HasPrefix(info.Main.Path, pkg) {
raw = info.Main.Version
}
for _, d := range info.Deps {
if strings.HasPrefix(d.Path, pkg) {
raw = d.Version
break
}
}
if v, err := version.NewVersion(raw); err == nil {
ver := Value{
Name: v.Prerelease(), // "alpha", "beta.1"
Raw: raw,
}
if s := v.Segments(); len(s) > 2 {
ver.Major, ver.Minor, ver.Patch = s[0], s[1], s[2]
}
return ver
}
return Value{
// Zero-versioned dev version.
Name: "dev",
Raw: "0.0.1-dev",
}
}
// Get optimistically gets current module version.
//
// Does not handle replace directives.
func Get() Value {
once.Do(func() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
once.version = Extract(info)
})
return once.version
}