From f2ed46d6ce247733d8cff657a89731f0cf9d301b Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Fri, 24 Sep 2021 23:28:09 +0800 Subject: [PATCH] style: move coolq.Version into internal/base --- .goreleaser.yml | 2 +- coolq/api.go | 21 ++++----------------- internal/base/version.go | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 internal/base/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index a04497f..76b73ac 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -27,7 +27,7 @@ builds: flags: - -trimpath ldflags: - - -s -w -X github.com/Mrs4s/go-cqhttp/coolq.Version=v{{.Version}} + - -s -w -X github.com/Mrs4s/go-cqhttp/internal/base.Version=v{{.Version}} - id: win env: - CGO_ENABLED=0 diff --git a/coolq/api.go b/coolq/api.go index 5714708..4ba4ad6 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -10,7 +10,6 @@ import ( "path" "path/filepath" "runtime" - "runtime/debug" "strconv" "strings" "time" @@ -23,21 +22,9 @@ import ( "github.com/tidwall/gjson" "github.com/Mrs4s/go-cqhttp/global" + "github.com/Mrs4s/go-cqhttp/internal/base" ) -// Version go-cqhttp的版本信息,在编译时使用ldflags进行覆盖 -var Version = "unknown" - -func init() { - if Version != "unknown" { - return - } - info, ok := debug.ReadBuildInfo() - if ok { - Version = info.Main.Version - } -} - // CQGetLoginInfo 获取登录号信息 // // https://git.io/Jtz1I @@ -1388,8 +1375,8 @@ func (bot *CQBot) CQGetVersionInfo() global.MSG { wd, _ := os.Getwd() return OK(global.MSG{ "app_name": "go-cqhttp", - "app_version": Version, - "app_full_name": fmt.Sprintf("go-cqhttp-%s_%s_%s-%s", Version, runtime.GOOS, runtime.GOARCH, runtime.Version()), + "app_version": base.Version, + "app_full_name": fmt.Sprintf("go-cqhttp-%s_%s_%s-%s", base.Version, runtime.GOOS, runtime.GOARCH, runtime.Version()), "protocol_version": "v11", "coolq_directory": wd, "coolq_edition": "pro", @@ -1399,7 +1386,7 @@ func (bot *CQBot) CQGetVersionInfo() global.MSG { "plugin_build_configuration": "release", "runtime_version": runtime.Version(), "runtime_os": runtime.GOOS, - "version": Version, + "version": base.Version, "protocol": func() int { switch client.SystemDeviceInfo.Protocol { case client.IPad: diff --git a/internal/base/version.go b/internal/base/version.go new file mode 100644 index 0000000..3527431 --- /dev/null +++ b/internal/base/version.go @@ -0,0 +1,16 @@ +package base + +import "runtime/debug" + +// Version go-cqhttp的版本信息,在编译时使用ldflags进行覆盖 +var Version = "unknown" + +func init() { + if Version != "unknown" { + return + } + info, ok := debug.ReadBuildInfo() + if ok { + Version = info.Main.Version + } +}