From 9bde820321746c751b04d172e3c8e6f1a17eb7be Mon Sep 17 00:00:00 2001 From: Siyuan Miao Date: Tue, 20 May 2025 15:06:27 +0200 Subject: [PATCH] move code to kvm package --- .golangci.yml | 3 +++ cmd/main.go | 23 +++++++++------------ native.go | 3 ++- version.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 version.go diff --git a/.golangci.yml b/.golangci.yml index ccd3c1a..5ba8c5d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,9 @@ linters: - linters: - errcheck path: _test.go + - linters: + - forbidigo + path: cmd/main.go paths: - third_party$ - builtin$ diff --git a/cmd/main.go b/cmd/main.go index 0aae156..2292bd9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,28 +3,23 @@ package main import ( "flag" "fmt" + "os" "github.com/jetkvm/kvm" - "github.com/prometheus/common/version" ) -func printVersion() { - version.Version = kvm.GetBuiltAppVersion() - app_version := version.Print("JetKVM Application") - fmt.Println(app_version) - - nativeVersion, err := kvm.GetNativeVersion() - if err == nil { - fmt.Println("\nJetKVM Native, version", nativeVersion) - } -} - func main() { versionPtr := flag.Bool("version", false, "print version and exit") + versionJsonPtr := flag.Bool("version-json", false, "print version as json and exit") flag.Parse() - if *versionPtr { - printVersion() + if *versionPtr || *versionJsonPtr { + versionData, err := kvm.GetVersionData(*versionJsonPtr) + if err != nil { + fmt.Printf("failed to get version data: %v\n", err) + os.Exit(1) + } + fmt.Println(string(versionData)) return } diff --git a/native.go b/native.go index a285953..9993aff 100644 --- a/native.go +++ b/native.go @@ -8,6 +8,7 @@ import ( "io" "net" "os" + "strings" "sync" "time" @@ -295,7 +296,7 @@ func GetNativeVersion() (string, error) { if err != nil { return "", err } - return string(version), nil + return strings.TrimSpace(string(version)), nil } func ensureBinaryUpdated(destPath string) error { diff --git a/version.go b/version.go new file mode 100644 index 0000000..ad774b6 --- /dev/null +++ b/version.go @@ -0,0 +1,56 @@ +package kvm + +import ( + "bytes" + "encoding/json" + "html/template" + "runtime" + + "github.com/prometheus/common/version" +) + +var versionInfoTmpl = ` +JetKVM Application, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) + build date: {{.buildDate}} + go version: {{.goVersion}} + platform: {{.platform}} + +{{if .nativeVersion}} +JetKVM Native, version {{.nativeVersion}} +{{end}} +` + +func GetVersionData(isJson bool) ([]byte, error) { + version.Version = GetBuiltAppVersion() + + m := map[string]string{ + "version": version.Version, + "revision": version.GetRevision(), + "branch": version.Branch, + "buildDate": version.BuildDate, + "goVersion": version.GoVersion, + "platform": runtime.GOOS + "/" + runtime.GOARCH, + } + + nativeVersion, err := GetNativeVersion() + if err == nil { + m["nativeVersion"] = nativeVersion + } + + if isJson { + jsonData, err := json.Marshal(m) + if err != nil { + return nil, err + } + return jsonData, nil + } + + t := template.Must(template.New("version").Parse(versionInfoTmpl)) + + var buf bytes.Buffer + if err := t.ExecuteTemplate(&buf, "version", m); err != nil { + return nil, err + } + + return buf.Bytes(), nil +}