forked from DataDog/gohai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.go
46 lines (38 loc) · 1.11 KB
/
make.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
// +build ignore
// Builds Gohai with version information
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)
func commandOutput(name string, args ...string) string {
out, err := exec.Command(name, args...).Output()
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(string(out))
}
func main() {
gobin := "go"
if len(os.Args) > 1 {
gobin = os.Args[1]
}
date := time.Now().Format(time.UnixDate)
commit := commandOutput("git", "rev-parse", "--short", "HEAD")
branch := commandOutput("git", "rev-parse", "--abbrev-ref", "HEAD")
go_version := commandOutput(gobin, "version")
// NB: Starting from Go 1.5, the syntax of these ldflags changes from `-X main.var 'value'` to `-X 'main.var=value'`
// For reference see https://github.com/golang/go/issues/12338
ldflags := fmt.Sprintf("-X main.buildDate '%s' -X main.gitCommit '%s' -X main.gitBranch '%s' -X main.goVersion '%s'", date, commit, branch, go_version)
cmd := exec.Command(gobin, "build", "-a", "-ldflags", ldflags, "gohai.go")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}