-
Notifications
You must be signed in to change notification settings - Fork 99
/
version.go
83 lines (73 loc) · 2.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2012 Datadog, Inc.
package version
import (
"encoding/json"
"fmt"
"io"
"runtime"
"github.com/go-logr/logr"
)
var (
// Version Datadog Operator version
Version = "0.0.0"
// BuildTime binary build time
BuildTime = ""
// Commit current git commit
Commit = ""
)
// JSON contains the json definition of the version payload
type JSON struct {
Version string `json:"version,omitempty"`
BuildTime string `json:"build_time,omitempty"`
Commit string `json:"commit,omitempty"`
Go string `json:"go,omitempty"`
Os string `json:"os,omitempty"`
Error string `json:"error,omitempty"`
}
func newVersionJSON() []byte {
bytes, err := json.Marshal(JSON{
Version: Version,
BuildTime: BuildTime,
Commit: Commit,
Go: runtime.Version(),
Os: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
Error: "",
})
if err != nil {
bytes, _ = json.Marshal(JSON{Error: fmt.Sprintf("cannot get version: %v", err)})
}
return bytes
}
// PrintVersionWriter print versions information in to writer interface
func PrintVersionWriter(writer io.Writer, format string) {
switch format {
case "text":
fmt.Fprintf(writer, "Version:\n")
for _, val := range printVersionSlice() {
fmt.Fprintf(writer, "- %s\n", val)
}
case "json":
versionBytes := newVersionJSON()
fmt.Fprint(writer, string(versionBytes))
default:
fmt.Fprintf(writer, "Unknown format: %s", format)
}
}
// PrintVersionLogs print versions information in logs
func PrintVersionLogs(logger logr.Logger) {
for _, val := range printVersionSlice() {
logger.Info(val)
}
}
func printVersionSlice() []string {
return []string{
fmt.Sprintf("Version: %v", Version),
fmt.Sprintf("Build time: %v", BuildTime),
fmt.Sprintf("Git Commit: %v", Commit),
fmt.Sprintf("Go Version: %s", runtime.Version()),
fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH),
}
}