fix Beego#4972: bee version failed

This commit is contained in:
Deng Ming 2022-06-10 18:54:40 +08:00
parent 97df75a28c
commit afe1678459
3 changed files with 19 additions and 76 deletions

View File

@ -15,15 +15,15 @@ import (
// RuntimeInfo holds information about the current runtime. // RuntimeInfo holds information about the current runtime.
type RuntimeInfo struct { type RuntimeInfo struct {
GoVersion string GoVersion string
GOOS string GOOS string
GOARCH string GOARCH string
NumCPU int NumCPU int
GOPATH string GOPATH string
GOROOT string GOROOT string
Compiler string Compiler string
BeeVersion string BeeVersion string
Published string Published string
} }
// InitBanner loads the banner and prints it to output // InitBanner loads the banner and prints it to output
@ -52,7 +52,7 @@ func show(out io.Writer, content string) {
} }
err = t.Execute(out, RuntimeInfo{ err = t.Execute(out, RuntimeInfo{
GetGoVersion(), runtime.Version(),
runtime.GOOS, runtime.GOOS,
runtime.GOARCH, runtime.GOARCH,
runtime.NumCPU(), runtime.NumCPU(),

View File

@ -1,23 +1,18 @@
package version package version
import ( import (
"bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io"
"os" "os"
path "path/filepath"
"regexp"
"runtime" "runtime"
"github.com/beego/bee/v2/cmd/commands" "github.com/beego/bee/v2/cmd/commands"
"github.com/beego/bee/v2/config" "github.com/beego/bee/v2/config"
beeLogger "github.com/beego/bee/v2/logger" beeLogger "github.com/beego/bee/v2/logger"
"github.com/beego/bee/v2/logger/colors" "github.com/beego/bee/v2/logger/colors"
"github.com/beego/bee/v2/utils"
) )
const verboseVersionBanner string = `%s%s______ const verboseVersionBanner string = `%s%s______
@ -27,7 +22,6 @@ const verboseVersionBanner string = `%s%s______
| |_/ /| __/| __/ | |_/ /| __/| __/
\____/ \___| \___| v{{ .BeeVersion }}%s \____/ \___| \___| v{{ .BeeVersion }}%s
%s%s %s%s
Beego : {{ .BeegoVersion }}
GoVersion : {{ .GoVersion }} GoVersion : {{ .GoVersion }}
GOOS : {{ .GOOS }} GOOS : {{ .GOOS }}
GOARCH : {{ .GOARCH }} GOARCH : {{ .GOARCH }}
@ -72,15 +66,14 @@ func versionCmd(cmd *commands.Command, args []string) int {
if outputFormat != "" { if outputFormat != "" {
runtimeInfo := RuntimeInfo{ runtimeInfo := RuntimeInfo{
GetGoVersion(), GoVersion: runtime.Version(),
runtime.GOOS, GOOS: runtime.GOOS,
runtime.GOARCH, GOARCH: runtime.GOARCH,
runtime.NumCPU(), NumCPU: runtime.NumCPU(),
os.Getenv("GOPATH"), GOPATH: os.Getenv("GOPATH"),
runtime.GOROOT(), GOROOT: runtime.GOROOT(),
runtime.Compiler, Compiler: runtime.Compiler,
version, BeeVersion: version,
GetBeegoVersion(),
} }
switch outputFormat { switch outputFormat {
case "json": case "json":
@ -115,53 +108,3 @@ func ShowShortVersionBanner() {
output := colors.NewColorWriter(os.Stdout) output := colors.NewColorWriter(os.Stdout)
InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner))) InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner)))
} }
func GetBeegoVersion() string {
re, err := regexp.Compile(`VERSION = "([0-9.]+)"`)
if err != nil {
return ""
}
wgopath := utils.GetGOPATHs()
if len(wgopath) == 0 {
beeLogger.Log.Error("GOPATH environment is empty,may be you use `go module`")
return ""
}
for _, wg := range wgopath {
wg, _ = path.EvalSymlinks(path.Join(wg, "src", "github.com", "astaxie", "beego"))
filename := path.Join(wg, "beego.go")
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
continue
}
beeLogger.Log.Error("Error while getting stats of 'beego.go'")
}
fd, err := os.Open(filename)
if err != nil {
beeLogger.Log.Error("Error while reading 'beego.go'")
continue
}
reader := bufio.NewReader(fd)
for {
byteLine, _, er := reader.ReadLine()
if er != nil && er != io.EOF {
return ""
}
if er == io.EOF {
break
}
line := string(byteLine)
s := re.FindStringSubmatch(line)
if len(s) >= 2 {
return s[1]
}
}
}
return "Beego is not installed. Please do consider installing it first: https://github.com/beego/beego/v2. " +
"If you are using go mod, and you don't install the beego under $GOPATH/src/github.com/astaxie, just ignore this."
}
func GetGoVersion() string {
return runtime.Version()
}

View File

@ -27,7 +27,7 @@ import (
const confVer = 0 const confVer = 0
const ( const (
Version = "2.0.2" Version = "2.0.3"
GitRemotePath = "github.com/beego/bee/v2" GitRemotePath = "github.com/beego/bee/v2"
) )