From 6d17695dc30dac8050a5f541f20964f1448faf2b Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期一, 01 十一月 2021 11:38:57 +0800
Subject: [PATCH] flag.Parse must be called after all flags defined

---
 version.go |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 103 insertions(+), 5 deletions(-)

diff --git a/version.go b/version.go
index 73f0341..5a28e5f 100644
--- a/version.go
+++ b/version.go
@@ -1,27 +1,40 @@
-package version
+package vaversion
 
 import (
 	"encoding/json"
+	"errors"
 	"flag"
 	"fmt"
+	"math"
 	"os"
+	"strconv"
+	"strings"
+)
+
+type (
+	VaVersion int64
 )
 
 var (
 	BuildVersion string
 	BuildTime    string
 	CommitSha1   string
+	AppName      string
+
+	ShowVer 	 bool
 )
 
 func init() {
-	var showVer bool
-	flag.BoolVar(&showVer, "v", false, "display build version")
-	flag.Parse()
-	if showVer {
+	flag.BoolVar(&ShowVer, "vbasic", false, "display build version")
+}
+
+func Usage() {
+	if ShowVer {
 		info := make(map[string]interface{})
 		info["version"] = BuildVersion
 		info["build"] = BuildTime
 		info["commit"] = CommitSha1
+		info["name"] = AppName
 
 		b, err := json.Marshal(info)
 		if nil != err {
@@ -33,3 +46,88 @@
 		}
 	}
 }
+
+func (v VaVersion) Valid() bool {
+	if v < 0 {
+		return false
+	}
+
+	majorVersion := v >> 32
+	if majorVersion > math.MaxUint16 {
+		return false
+	}
+
+	return true
+}
+
+func (v VaVersion) Compare(v1 VaVersion) (int, error) {
+	if !v.Valid() {
+		return 0, errors.New("Invalid VaVersion left value")
+	}
+
+	if !v1.Valid() {
+		return 0, errors.New("Invalid VaVersion right value")
+	}
+
+	if v > v1 {
+		return 1, nil
+	}
+
+	if v < v1 {
+		return -1, nil
+	}
+
+	return 0, nil
+}
+
+func (v VaVersion) String() (string, error) {
+	if !v.Valid() {
+		return "", errors.New("Invalid VaVersion value")
+	}
+
+	majorVersion := (v >> 32) & 0xffff
+	minorVersion := (v >> 16) & 0xffff
+	buildNumber := v & 0xffff
+
+	name := fmt.Sprintf("%d.%d.%d", majorVersion, minorVersion, buildNumber)
+
+	return name, nil
+}
+
+func VersionName2VaVersion(version string) (VaVersion, error) {
+	subVersions := strings.Split(version, ".")
+	if len(subVersions) != 3 {
+		return 0, errors.New("Incorrect version format, version formats like 1.12.3")
+	}
+
+	majorVersion, err := strconv.ParseInt(subVersions[0], 10, 64)
+	if nil != err {
+		return 0, errors.New(version + " major part can't be parsed, version formats like 1.12.3")
+	}
+
+	minorVersion, err := strconv.ParseInt(subVersions[1], 10, 64)
+	if nil != err {
+		return 0, errors.New(version + " minor part can't be parsed, version formats like 1.12.3")
+	}
+
+	buildNumber, err := strconv.ParseInt(subVersions[2], 10, 64)
+	if nil != err {
+		return 0, errors.New(version + " build number part can't be parsed, version formats like 1.12.3")
+	}
+
+	if majorVersion > math.MaxUint16 {
+		return 0, errors.New(version + " major version too large, max value is:" + strconv.Itoa(math.MaxUint16))
+	}
+
+	if minorVersion > math.MaxUint16 {
+		return 0, errors.New(version + " minor version too large, max value is:" + strconv.Itoa(math.MaxUint16))
+	}
+
+	if buildNumber > math.MaxUint16 {
+		return 0, errors.New(version + " build number too large, max value is:" + strconv.Itoa(math.MaxUint16))
+	}
+
+	num := (majorVersion << 32) | (minorVersion << 16) | buildNumber
+
+	return VaVersion(num), nil
+}

--
Gitblit v1.8.0