|
| 1 | +package kvm |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "html/template" |
| 7 | + "runtime" |
| 8 | + |
| 9 | + "github.com/prometheus/common/version" |
| 10 | +) |
| 11 | + |
| 12 | +var versionInfoTmpl = ` |
| 13 | +JetKVM Application, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) |
| 14 | + build date: {{.buildDate}} |
| 15 | + go version: {{.goVersion}} |
| 16 | + platform: {{.platform}} |
| 17 | +
|
| 18 | +{{if .nativeVersion}} |
| 19 | +JetKVM Native, version {{.nativeVersion}} |
| 20 | +{{end}} |
| 21 | +` |
| 22 | + |
| 23 | +func GetVersionData(isJson bool) ([]byte, error) { |
| 24 | + version.Version = GetBuiltAppVersion() |
| 25 | + |
| 26 | + m := map[string]string{ |
| 27 | + "version": version.Version, |
| 28 | + "revision": version.GetRevision(), |
| 29 | + "branch": version.Branch, |
| 30 | + "buildDate": version.BuildDate, |
| 31 | + "goVersion": version.GoVersion, |
| 32 | + "platform": runtime.GOOS + "/" + runtime.GOARCH, |
| 33 | + } |
| 34 | + |
| 35 | + nativeVersion, err := GetNativeVersion() |
| 36 | + if err == nil { |
| 37 | + m["nativeVersion"] = nativeVersion |
| 38 | + } |
| 39 | + |
| 40 | + if isJson { |
| 41 | + jsonData, err := json.Marshal(m) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + return jsonData, nil |
| 46 | + } |
| 47 | + |
| 48 | + t := template.Must(template.New("version").Parse(versionInfoTmpl)) |
| 49 | + |
| 50 | + var buf bytes.Buffer |
| 51 | + if err := t.ExecuteTemplate(&buf, "version", m); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + return buf.Bytes(), nil |
| 56 | +} |
0 commit comments