Skip to content

Commit 9bde820

Browse files
committed
move code to kvm package
1 parent 12dbe6f commit 9bde820

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ linters:
2222
- linters:
2323
- errcheck
2424
path: _test.go
25+
- linters:
26+
- forbidigo
27+
path: cmd/main.go
2528
paths:
2629
- third_party$
2730
- builtin$

cmd/main.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
67

78
"github.com/jetkvm/kvm"
8-
"github.com/prometheus/common/version"
99
)
1010

11-
func printVersion() {
12-
version.Version = kvm.GetBuiltAppVersion()
13-
app_version := version.Print("JetKVM Application")
14-
fmt.Println(app_version)
15-
16-
nativeVersion, err := kvm.GetNativeVersion()
17-
if err == nil {
18-
fmt.Println("\nJetKVM Native, version", nativeVersion)
19-
}
20-
}
21-
2211
func main() {
2312
versionPtr := flag.Bool("version", false, "print version and exit")
13+
versionJsonPtr := flag.Bool("version-json", false, "print version as json and exit")
2414
flag.Parse()
2515

26-
if *versionPtr {
27-
printVersion()
16+
if *versionPtr || *versionJsonPtr {
17+
versionData, err := kvm.GetVersionData(*versionJsonPtr)
18+
if err != nil {
19+
fmt.Printf("failed to get version data: %v\n", err)
20+
os.Exit(1)
21+
}
22+
fmt.Println(string(versionData))
2823
return
2924
}
3025

native.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net"
1010
"os"
11+
"strings"
1112
"sync"
1213
"time"
1314

@@ -295,7 +296,7 @@ func GetNativeVersion() (string, error) {
295296
if err != nil {
296297
return "", err
297298
}
298-
return string(version), nil
299+
return strings.TrimSpace(string(version)), nil
299300
}
300301

301302
func ensureBinaryUpdated(destPath string) error {

version.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)