-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_build_info.go
65 lines (50 loc) · 1.27 KB
/
generate_build_info.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
// +build ignore
package main
import (
"fmt"
"html/template"
"log"
"os"
"os/exec"
"strings"
"time"
)
const infoFile = "build_info.go"
var buildTemplate = template.Must(template.New("build_info").Parse(
`// Code generated by 'go generate' -- DO NOT EDIT.
package main
const (
gitVersion = "{{ .GitVersion }}"
buildDate = "{{ .BuildDate }}"
)
`))
func main() {
log.Print("generating build info...")
info := &struct {
GitVersion string
BuildDate string
}{}
log.Print("checking git...")
cmd := exec.Command("git", "describe", "--tags", "--dirty")
b, err := cmd.Output()
if err != nil {
log.Fatalf("error fetching git info: %+v", err)
}
gitOutput := strings.TrimSpace(fmt.Sprintf("%s", b))
log.Printf("successfully retrieved git info: %q", gitOutput)
info.GitVersion = gitOutput
info.BuildDate = time.Now().Format(time.RFC3339)
log.Printf("creating/truncating %q file...", infoFile)
file, err := os.Create(infoFile)
if err != nil {
log.Fatalf("error creating %q: %+v", infoFile, err)
}
defer file.Close()
// buf := &bytes.Buffer{}
log.Printf("writing %q content...", infoFile)
err = buildTemplate.Execute(file, info)
if err != nil {
log.Fatalf("error writing content to %q: %+v", infoFile, err)
}
log.Printf("successfully wrote %q", infoFile)
}