-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.go
60 lines (46 loc) · 1.39 KB
/
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
//go:build exclude
package main
import (
"flag"
"os"
"os/exec"
"strings"
)
const INFO_TEMPLATE = "pkg/info/info.tmpl"
const INFO_DESTINATION = "pkg/info/info.go"
func revision() string {
output, e := exec.Command("git", "log", "-n1", "--pretty=%h").Output()
if e != nil {
panic(e)
}
return strings.TrimSpace(string(output[:]))
}
func version() string {
output, _ := exec.Command("git", "describe", "--abbrev=0", "--tags").Output()
result := strings.TrimSpace(string(output[:]))
if result == "" {
return "dev"
}
return result
}
func main() {
revision := flag.String("revision", revision(), "Revision to identify the build, usually a git sha")
version := flag.String("version", version(), "Version to identified last published version of the package, usually a git tag")
name := flag.String("name", "keydex", "Name of this executable")
flag.Parse()
buffer, err := os.ReadFile(INFO_TEMPLATE)
if err != nil {
panic(err)
}
// This is needed for local development where env.VERSION might be nil
versionWithDefault := *version
if versionWithDefault == "" {
versionWithDefault = "dev"
}
result := strings.ReplaceAll(string(buffer[:]), "_REVISION_", *revision)
result = strings.ReplaceAll(result, "_VERSION_", versionWithDefault)
result = strings.ReplaceAll(result, "_NAME_", *name)
if err := os.WriteFile(INFO_DESTINATION, []byte(result), 0666); err != nil {
panic(err)
}
}