forked from SiaFoundation/hostd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
129 lines (111 loc) · 2.84 KB
/
gen.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//go:build ignore
// This script generates meta.go which contains version info for the hostd binary. It can be run with `go generate`.
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
"text/template"
"time"
)
const logFormat = `{%n "commit": "%H",%n "shortCommit": "%h",%n "timestamp": "%cD",%n "tag": "%(describe:tags=true)"%n}`
type (
gitTime time.Time
gitMeta struct {
Commit string `json:"commit"`
ShortCommit string `json:"shortCommit"`
Timestamp gitTime `json:"timestamp"`
Tag string `json:"tag"`
}
)
var buildTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
// This file was generated by go generate at {{ .RunTime }}.
package build
const (
commit = "{{ .Commit }}"
version = "{{ .Version }}"
buildTime = {{ .UnixTimestamp }}
)
`))
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *gitTime) UnmarshalJSON(buf []byte) error {
timeFormats := []string{
time.RFC1123Z,
"Mon, 2 Jan 2006 15:04:05 -0700",
"2006-01-02 15:04:05 -0700",
time.UnixDate,
time.ANSIC,
time.RFC3339,
time.RFC1123,
}
for _, format := range timeFormats {
parsed, err := time.Parse(format, strings.Trim(string(buf), `"`))
if err == nil {
*t = gitTime(parsed)
return nil
}
}
return errors.New("failed to parse time")
}
func getGitMeta() (meta gitMeta, _ error) {
cmd := exec.Command("git", "log", "-1", "--pretty=format:"+logFormat+"")
buf, err := cmd.Output()
if err != nil {
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 {
return gitMeta{}, fmt.Errorf("command failed: %w", errors.New(string(err.Stderr)))
}
return gitMeta{}, fmt.Errorf("failed to execute command: %w", err)
} else if err := json.Unmarshal(buf, &meta); err != nil {
return gitMeta{}, fmt.Errorf("failed to unmarshal json: %w", err)
}
return
}
func validSemVer(tag string) bool {
if len(tag) == 0 || tag[0] != 'v' {
return false
}
var major, minor, patch uint8
_, err := fmt.Sscanf(tag, "v%d.%d.%d", &major, &minor, &patch)
if err != nil {
return false
} else if major == 0 && minor == 0 && patch == 0 {
return false
}
return true
}
func main() {
meta, err := getGitMeta()
if err != nil {
log.Fatalln(err)
}
commit := meta.ShortCommit
version := meta.Tag
if !validSemVer(meta.Tag) {
// no version, use commit and current time for development
version = commit
meta.Timestamp = gitTime(time.Now())
}
f, err := os.Create("meta.go")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
err = buildTemplate.Execute(f, struct {
Commit string
Version string
UnixTimestamp int64
RunTime string
}{
Commit: commit,
Version: version,
UnixTimestamp: time.Time(meta.Timestamp).Unix(),
RunTime: time.Now().Format(time.RFC3339),
})
if err != nil {
log.Fatalln(err)
}
}