forked from cloudfoundry/stembuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
61 lines (52 loc) · 1.37 KB
/
gen.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
// The following directive is necessary to make the package coherent:
// +build ignore
// This is used to generate the go format of the version/version file
// The version/version file is updated by our Concourse Pipelines
package main
import (
"bufio"
"fmt"
"os"
"text/template"
"time"
)
func main() {
packageTemplate := template.Must(template.New("").
Parse(`// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
package version
var Version = "{{ .Version }}"
`))
version, err := os.Open("version/version")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error opening version file: %s", err)
os.Exit(1)
}
//noinspection GoUnhandledErrorResult
defer version.Close()
r := bufio.NewReader(version)
line, _, err := r.ReadLine()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "could not read version file: %s", err)
os.Exit(1)
}
goVersion, err := os.Create("version/version.go")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error creating version.go file: %s", err)
os.Exit(1)
}
//noinspection GoUnhandledErrorResult
defer goVersion.Close()
err = packageTemplate.Execute(goVersion, struct {
Timestamp time.Time
Version string
}{
Timestamp: time.Now(),
Version: string(line[:]),
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to populate version.go file: %s", err)
os.Exit(1)
}
}