-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
76 lines (60 loc) · 1.48 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
// Command-line code generation for git-derived version information.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
var (
// Name of file to output with Go version code
outputfile = flag.String("o", "", "")
// Display usage if true.
showHelp = flag.Bool("help", false, "")
)
const helpMessage = `
dvid-gen-version calls git to generation Go code with source code version info.
Usage: dvid-gen-version -o version.go
-h, -help (flag) Show help message
`
const code = `package server
func init() {
gitVersion = %q
}
`
func main() {
flag.BoolVar(showHelp, "h", false, "Show help message")
flag.Usage = func() {
fmt.Printf(helpMessage)
}
flag.Parse()
if *showHelp {
flag.Usage()
os.Exit(0)
}
if len(*outputfile) < 4 {
fmt.Printf("The %q is required for this program\n", "-o foo.go")
os.Exit(1)
}
// Make sure we have git
gitPath, err := exec.LookPath("git")
if err != nil {
fmt.Printf("Unable to find git command; alter PATH?\nError: %v\n", err)
os.Exit(1)
}
// Get version string
cmd := exec.Command(gitPath, "describe", "--abbrev=5", "--tags")
out, err := cmd.Output()
if err != nil {
out = []byte("notag")
}
// Inject the version string into Go code that gets written to desired location.
versionID := strings.TrimSpace(string(out))
goCode := fmt.Sprintf(code, versionID)
if err := ioutil.WriteFile(*outputfile, []byte(goCode), 0644); err != nil {
fmt.Printf("Error save go code: %v\n", err)
os.Exit(1)
}
}