-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (55 loc) · 1.11 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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"text/template"
)
func main() {
///
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
///
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `
Usage of %s:
%s [OPTIONS] ARGS...
Options`, os.Args[0], os.Args[0])
flag.PrintDefaults()
}
pkgname := flag.String("pkgname", "main", "package name")
out := flag.String("out", cwd+"/gitrev_gen.go", "output file path")
flag.Parse()
///
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Stdout = new(bytes.Buffer)
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
return
}
revision := fmt.Sprint(cmd.Stdout)
///
tmpl := `package {{.pkgname}}
func GetRevision() (string){
return "{{.revision}}"
}
`
t := template.New("t")
template.Must(t.Parse(tmpl))
dest := new(bytes.Buffer)
t.Execute(dest, map[string]string{
"revision": revision[:len(revision)-1],
"pkgname": *pkgname,
})
ioutil.WriteFile(*out, dest.Bytes(), 0644)
fmt.Println("Output generate file:", *out)
fmt.Println("\tpacakge name:", *pkgname)
fmt.Println("\trevision:", revision)
}