This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
79 lines (64 loc) · 1.47 KB
/
main.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
package main
import (
"bytes"
"flag"
"os"
"os/exec"
"text/template"
"time"
"github.com/bountylabs/log"
)
var path string
var repo string
var p string
var short bool
func init() {
flag.StringVar(&path, "o", "version.go", "filename")
flag.StringVar(&repo, "i", ".", "repository")
flag.StringVar(&p, "p", "version", "package")
flag.BoolVar(&short, "s", false, "short git sha")
}
func main() {
flag.Parse()
//get commit hash (short or not)
cmd := func() *exec.Cmd {
if short {
return exec.Command("git", "--git-dir", repo+"/.git", "rev-parse", "--short", "HEAD")
}
return exec.Command("git", "--git-dir", repo+"/.git", "rev-parse", "HEAD")
}()
git, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("get git version err=%v", err)
os.Exit(1)
}
data := struct {
Pkg string
Git string
Build string
Tstamp int64
}{
Pkg: p,
Git: string(bytes.TrimSpace(git)),
Build: os.Getenv("CIRCLE_BUILD_NUM"),
Tstamp: time.Now().UnixNano(),
}
f, err := os.Create(path)
if err != nil {
log.Errorf("create file=%s err=%v", path, err)
os.Exit(1)
}
if err = tmpl.Execute(f, &data); err != nil {
log.Errorf("execute template err=%v", err)
os.Exit(1)
}
if err = f.Close(); err != nil {
log.Errorf("close file=%s err=%v", f.Name(), err)
os.Exit(1)
}
}
var tmpl = template.Must(template.New("gitversion").Parse(`package {{.Pkg}}
var GIT_COMMIT_HASH = "{{.Git}}"
var CIRCLE_BUILD_NUM = "{{.Build}}"
var GENERATED int64 = {{.Tstamp}}
`))