generated from cloudoperators/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
59 lines (50 loc) · 1.2 KB
/
version.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0
package version
import (
"bytes"
"fmt"
"os"
"runtime"
"strings"
"text/template"
flag "github.com/spf13/pflag"
)
var (
GitBranch,
GitCommit,
GitState,
BuildDate string
GoVersion = runtime.Version()
versionRequested = false
versionInfoTmpl = `
{{.program}}, revision: {{.revision}}, branch: {{.branch}}, state: {{.state}}
build date: {{.buildDate}}
go version: {{.goVersion}}
`
)
func init() {
flag.BoolVar(&versionRequested, "version", false, "Display version and exit")
}
func ShowVersionAndExit(programName string) {
if versionRequested {
fmt.Println(strings.TrimSpace(GetVersionTemplate(programName)))
os.Exit(0)
}
}
func GetVersionTemplate(programName string) string {
m := map[string]string{
"program": programName,
"revision": GitCommit,
"state": GitState,
"branch": GitBranch,
"buildDate": BuildDate,
"goVersion": GoVersion,
}
t := template.Must(template.New("version").Parse(versionInfoTmpl))
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "version", m); err != nil {
panic(err)
}
return buf.String()
}