-
Notifications
You must be signed in to change notification settings - Fork 48
/
vcs.go
52 lines (47 loc) · 1.47 KB
/
vcs.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
// Copyright 2022 The CortexTheseus Authors
// This file is part of the CortexTheseus library.
//
// The CortexTheseus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The CortexTheseus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>.
package version
import (
"runtime/debug"
"time"
)
// In go 1.18 and beyond, the go tool embeds VCS information into the build.
const (
govcsTimeLayout = "2006-01-02T15:04:05Z"
ourTimeLayout = "20060102"
)
// buildInfoVCS returns VCS information of the build.
func buildInfoVCS(info *debug.BuildInfo) (s VCSInfo, ok bool) {
for _, v := range info.Settings {
switch v.Key {
case "vcs.revision":
s.Commit = v.Value
case "vcs.modified":
if v.Value == "true" {
s.Dirty = true
}
case "vcs.time":
t, err := time.Parse(govcsTimeLayout, v.Value)
if err == nil {
s.Date = t.Format(ourTimeLayout)
}
}
}
if s.Commit != "" && s.Date != "" {
ok = true
}
return
}