/
ogenversion.go
110 lines (100 loc) · 2.22 KB
/
ogenversion.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
103
104
105
106
107
108
109
110
// Package ogenversion provides the version of the ogen tool.
package ogenversion
import (
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
)
var getOnce struct {
info Info
ok bool
once sync.Once
}
func getOgenVersion(m *debug.Module) (string, bool) {
if m == nil || m.Path != "github.com/ogen-go/ogen" {
return "", false
}
return m.Version, true
}
func getInfo() (Info, bool) {
getOnce.once.Do(func() {
bi, ok := debug.ReadBuildInfo()
getOnce.ok = ok
if !ok {
return
}
var ogenIsDep bool
if v, ok := getOgenVersion(&bi.Main); ok {
getOnce.info.Version = v
} else {
ogenIsDep = true
for _, m := range bi.Deps {
if v, ok := getOgenVersion(m); ok {
getOnce.info.Version = v
break
}
}
}
getOnce.info.GoVersion = bi.GoVersion
if !ogenIsDep {
// ogen is the main module, so we can use buildvcs data.
for _, s := range bi.Settings {
switch s.Key {
case "vcs.revision":
getOnce.info.Commit = s.Value
case "vcs.time":
if t, err := time.Parse(time.RFC3339Nano, s.Value); err == nil {
getOnce.info.Time = t
}
}
}
}
})
return getOnce.info, getOnce.ok
}
// Info is the ogen build information.
type Info struct {
// Version is the version of the ogen tool.
Version string
// GoVersion is the version of the Go that produced this binary.
GoVersion string
// Commit is the current commit hash.
Commit string
// Time is the time of the build.
Time time.Time
}
// GetInfo returns the ogen build information.
func GetInfo() (Info, bool) {
return getInfo()
}
// String returns string representation of the build information.
func (i Info) String() string {
var s strings.Builder
s.WriteString("ogen version ")
if v := i.Version; v != "" {
s.WriteString(v)
} else {
s.WriteString("unknown")
}
if commit := i.Commit; commit != "" {
s.WriteByte('-')
s.WriteString(commit)
}
if t, v := i.Time, i.GoVersion; v != "" || !t.IsZero() {
s.WriteString(" (built")
if v != "" {
s.WriteString(" with ")
s.WriteString(v)
}
if !t.IsZero() {
s.WriteString(" at ")
s.WriteString(t.Format(time.RFC1123))
}
s.WriteByte(')')
}
const osArch = " " + runtime.GOOS + "/" + runtime.GOARCH
s.WriteString(osArch)
return s.String()
}